REST with JAX-RS - Handling long running operations REST with JAX-RS - Handling long running operations multithreading multithreading

REST with JAX-RS - Handling long running operations


I also know that that creating your own threads in a Java EE container is generally bad practice!

Although the above is true in most cases, in this one you don't have a choice. At least don't create your Thread instances yourself. Let an ExecutorService do it for you. If anything, make this ExecutorService have a big enough pool and share it with all your components.


I think that Jersey Async docs exhaust the topic quite well. Here is a brief snippet:

@Path("/async/longRunning")public class MyResource {   @GET   public void longRunningOp(@Suspended final AsyncResponse ar) {       executor.submit(            new Runnable() {                public void run() {                    executeLongRunningOp();                    ar.resume("Hello async world!");                } });  }}

When it comes to the following quotation from the docs:

Note that the use of server-side asynchronous processing model will not improve the request processing time perceived by the client.(...)

I thing you have misunderstood it a bit. What the author of the docs tried to express here is that asynchroneous processing will not speed up things just by itself. But the response can be returned immediately using for instance the following:

return Response.status(Status.ACCEPTED).build();


I would make a method which immediately returns response with process id and time when it will be completed. A calculation starts in background and it is cached for some time after completion. Then client tries to retrieve information with specific id and gets related response.