How to create Rest API and add time delay to process?

Viewed 1318

I am very new to Springboot. I have created the following rest API. If this API receives multiple requests at the same time, i want it to handle one by one without loosing any requests. I want this service call to be made with delay of 20 seconds if multiple requests are made.

@RequestMapping({"/create"})
public Response processCreation(@RequestBody(required=false) RequestDTO requestDTO) throws Exception {
    Response response = new Response();
    response=interceptorService.interceptData(requestDTO);
    return response;
}
1 Answers

I think a better approach is handling this use case in your code level (not through some springboot configuration).

Once you have a request, take all of the relevant parameters and add them to some kind of a queue. You will have a thread / thread pool that will read from that queue, process the request and send the response.

The request thread will do nothing except scheduling a new task to the queue. The processing will be made by the dedicated threadpool / thread.

Then you can also add that 20-seconds delay you want.

Related