Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST with JAX-RS - Handling long running operations

I have a REST service implemented with JAX-RS. Some of the operations take a long time to complete, potentially 15-30 minutes. For these cases, my inclination is to dispatch a background thread to process the long running operation and then respond immediately with HTTP status 202 ACCEPTED. The response would contain a location header with a url that clients can use to poll for progress.

This approach requires the creation of threads to handle long running operations, such that 202 ACCEPTED can be returned immediately. I also know that that creating your own threads in a Java EE container is generally bad practice!

My questions are as follows:

  1. Do folks agree that this is a correct approach?
  2. Assuming it is correct, Can folks recommend a 'good practice' solution that enables me to dispatch a long running operation in the background and return immediately?

Also, to avoid managing my own threads, I looked into the JAX-RS asynchronous server api. Unfortunately, although this improves server throughput, it will not allow me to respond immediately with ACCEPTED.

Jersey states the following:

Note that the use of server-side asynchronous processing model will not improve the 
request processing time perceived by the client. It will however increase the
throughput of the server, by releasing the initial request processing thread back to
the I/O container while the request may still be waiting in a queue for processing or    
the processing may still be running on another dedicated thread. The released I/O  
container thread can be used to accept and process new incoming request connections.

Any help is appreciated. Thanks!

like image 685
cmd Avatar asked Sep 13 '13 13:09

cmd


People also ask

Which annotation is used for creating a RESTful web service using JAX-RS?

The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.

What is the difference between JAX-RS and spring REST?

JAX-RS is only a specification and it needs a compatible implementation to be used. On the other hand, Spring MVC is a complete framework with REST capabilities. Like JAX-RS, it also provides us with useful annotations to abstract from low-level details.

What are the implementations of JAX-RS?

JAX-RS is a standard defined in Java Specification Request 311 (JSR-311) and Jersey / RESTEasy are implementations of it.


1 Answers

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.

like image 139
Sotirios Delimanolis Avatar answered Sep 22 '22 02:09

Sotirios Delimanolis