Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous REST API with Jersey

We are using Jersey for a REST webservice (deployed on tomcat).

I know that

  • Tomcat's HTTP connector has a thread pool for reading HTTP requests and forwarding to Jersey.
  • Forwarding to Jersey is synchronous.

I read here that Jersey provides AsyncService to maximise concurrency.

Question: Why does Jersey's async design force developer to offload REST processing to separate thread/runnable, while Jersey can do this by itself with a dedicated/private executor pool?

Is this for better control for developer or am I missing something here?

like image 377
Chethan Avatar asked Nov 07 '16 17:11

Chethan


People also ask

CAN REST API be asynchronous?

REST clients can be implemented either synchronously or asynchronously. Both MicroProfile Rest Client and JAX-RS can enable asynchronous clients.

What is Jersey in RESTful web services?

Jersey is Sun's production quality reference implementation for JSR 311: JAX-RS: The Java API for RESTful Web Services. Jersey implements support for the annotations defined in JSR-311, making it easy for developers to build RESTful web services with Java and the Java JVM.


1 Answers

Why does Jersey's async design force developer to offload REST processing to separate thread/runnable, while Jersey can do this by itself with a dedicated/private executor pool?

You're not forced to. Jersey has the @ManagedAsync that you can put on your methods. This does exactly what you are asking

ManagedAsync

Indicates that the resource method to which the annotation has been applied should be executed on a separate thread managed by an internal Jersey executor service.

@GET
@ManagedAsync
public void longGet(@Suspended final AsyncResponse ar) {
    
}

See Also:

  • Server Asynchronous Managed Example
like image 97
Paul Samsotha Avatar answered Oct 12 '22 13:10

Paul Samsotha