简体   繁体   中英

why do we need async rest web services when it is already running asynchronously?

I have created a simple rest web service like this. It's not asynchronous.

@Path("/rest")
class rest{
     @GET
     @Path("/asyncTest")
     public String AsyncTest(){
          //Some long operation
          Thread.sleep(60000);
          return "Success";
     }
}

And My client is a Browser.

If I hit this web service in 3 different browsers this is what I observed

Chrome Browser  --> Hit request 12:00 AM --> Response 12:01 AM
Firefox Browser --> Hit request 12:00 AM --> Response 12:01 AM
Opera Browser   --> Hit Request 12:00 AM --> Response 12:01 AM

If my simple web service is not working as async I supposed to get the following results right ?

Chrome Browser  --> Hit request 12:00 AM --> Response 12:01 AM
Firefox Browser --> Hit request 12:00 AM --> Response 12:02 AM
Opera Browser   --> Hit Request 12:00 AM --> Response 12:03 AM

But that is not happening. In this case why do I have to use Async Web Services ?

This is not asynchronous execution but the concurrent execution. To understand we can take an example of servlet execution. Servlet contains has a pool of threads to process the requests so if any request comes, it will use the available thread from pool to process the request and after returning the response that thread will be back to pool. During this processing that thread will not be available to take other requests and container will use other available threads to process the requests. If all thread are busy then new requests will wait to get some thread available to take it and this work as synchronous execution.
When it comes to asynchronous execution we divide the threads between request thread and worker thread. Request thread will take the request and hand over to worker threads then worker thread will process it asynchronously and return the response. This way request thread will be available immediately and it can accept more request compare to synchronous execution. However async execution also need to care as there could be some situation when server may error in out of memory. Please refer the below URL to get more details and how to implement it. https://www.thetechnojournals.com/2019/10/asynchronous-rest-service.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM