Questo contenuto non è disponibile nella lingua selezionata.
Chapter 30. Asynchronous Job Service
The RESTEasy Asynchronous Job Service is an implementation of the Asynchronous Job pattern defined in O'Reilly's Restful Web Services. It is designed to add asynchronicity to a synchronous protocol.
30.1. Using Async Jobs Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
While HTTP is a synchronous protocol, it is capable of dealing with asynchronous invocations. The HTTP 1.1 response code
202 (Accepted) means that the server has received and accepted the response for processing, but that processing is not yet complete. The RESTEasy Asynchronous Job Service is based on this type of response.
POST http://example.com/myservice?asynch=true
POST http://example.com/myservice?asynch=true
For example, if you make the above post with the
asynch query parameter set to true, RESTEasy returns a 202 (Accepted) response code and runs the invocation in the background. It also returns a Location header with a URL pointing to the location of the background method's response.
HTTP/1.1 202 Accepted Location: http://example.com/asynch/jobs/3332334
HTTP/1.1 202 Accepted
Location: http://example.com/asynch/jobs/3332334
The URI will have the form of:
/asynch/jobs/{job-id}?wait={millisconds}|nowait=true
/asynch/jobs/{job-id}?wait={millisconds}|nowait=true
You can perform
GET, POST and DELETE operations on this job URL. GET returns the response of the JAX-RS resource method, if the job has completed. If the job has not completed, this GET returns a response code of 202 (Accepted). Invoking GET does not remove the job, so it can be called multiple times. When RESTEasy's job queue becomes full, it will evict the least recently used job from memory. You can clean the queue manually by calling DELETE on the URI. POST reads the JOB response and removes the JOB when it has completed.
Both
GET and POST let you specify a mazimum wait time in milliseconds — a wait query parameter. For example:
POST http://example.com/asynch/jobs/122?wait=3000
POST http://example.com/asynch/jobs/122?wait=3000
If you do not specify a
wait parameter, the GET or POST will not wait at all if the job is not complete.
Note
While you can invoke
GET, DELETE, and PUT methods asynchronously, this breaks the HTTP 1.1 contract of these methods. These invocations may not change the state of the resource if invoked more than once, but they do change the state of the server. Try to invoke POST methods asynchronously.
Important
RESTEasy role-based security (annotations) does not work with the Asynchronous Job Service. You must use XML declarative security within your
web.xml file. It is currently impossible to implement role-based security portably. In the future, we may have specific JBoss integration, but will not support other environments.