Chethan Chethan - 1 month ago 15
reST (reStructuredText) Question

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?

Answer

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: