Humberd
Humberd

Reputation: 2951

Is REST controller multithreaded?

I've been doing this tutorial about how to return async callable object. It works as intended. But while the first request sleeps for 5 seconds I get the second request, controller waits for the previous request to finnish before handling the second one.

How to make controller handle immediately every request and make sleeping in a background?

@Edit

Example: Imagine a situation, that my controller needs to make a request to external api and based on its response it should send his own response. External api call takes lets say 2 seconds. I want users of my application to wait only that 2,5 seconds and not be placed in queue, because the controller can handle only one request at a time.

Upvotes: 7

Views: 20952

Answers (2)

Vasu
Vasu

Reputation: 22452

Is REST controller multithreaded?

REST controller is multithreaded as the DisptcherServlet handles multiple requests from the clients concurrently and serves using the respective controller methods. You can refer the request handling flow here

How to make controller handle immediately every request and make sleeping in a background?

You can do that by returning Callable<String> in the Spring controller method as shown below:

@Controller
public class MyController {

    @RequestMapping(value="/sleep")
    public Callable<String> myControllerMethod() {
        Callable<String> asyncTask = () -> { try {
            System.out.println(" WAITING STARTED:"+new Date());
            Thread.sleep(5000);
            System.out.println(" WAITING COMPLETED:"+new Date());
            return "Return";//Send the result back to View Return.jsp
        } catch(InterruptedException iexe) {
            //log exception
            return "ReturnFail";
        }}; 
    return asyncTask;
  }

Output:

WAITING STARTED: Thu Nov 24 21:03:12 GMT 2016

WAITING COMPLETED: Thu Nov 24 21:03:17 GMT 2016

After this, the view will be returned "Return.jsp" page.

Here, the controller method will be running in a separate thread (releasing the actual servlet thread) and once the task is completed the Result will be sent back again to the client (View etc..).

P.S.: You need to add @EnableAsync as part of your application configuration, you can look here on this.

Upvotes: 16

Fernando
Fernando

Reputation: 406

What you want to do is what it is supposed to be done in the first example of the tutorial you linked:

@RequestMapping(value = "/block", method = RequestMethod.GET, produces = "text/html")
public String executeSlowTask() {
    logger.info("Request received");
    Thread.sleep(5000);
    logger.info("Servlet thread released");

    return result;
}

This blocks the calling thread and waits till it is done. If you call from a different HTTP session, it will be another thread, so it will be a new 5 seconds wait (not affected by the first one).

Threads are linked to HTTP calls as long as there are threads available in the pool (configuration of the tomcat server).

It is not the case that the controller will block all subsequent calls while busy. It is multithreaded.

Upvotes: 3

Related Questions