Asynchronous processing
JAXRS supports asynchronous processing in both server and client APIs:
- Server API: Asynchronous processing allows the resource method to inform the JAXRS runtime that a response is not ready yet and will be provided at a future time. This is done by temporarily suspending the connection and then resuming it once the response becomes available. For a resource method to utilize async processing it must inject an
AsyncResponse
instance using the special@Suspended
annotation. The resource method code may perform a long-running operation as desired, and then finally publish the response using theresume
method of theAsyncResponse
instance.AsyncResponse
also allows for specifying timeouts and callbacks:
@GET public void timeConsumingActivity( @Suspended final AsyncResponse ar) { executor.execute(() -> { //Executor to run job in background longRunningCode(); ar.resume("async response here"); });...