REST server callbacks
As the asynchronous operations in some cases can take a long time and often don't end inside a single method invocation of a resource, the JAX-RS specifications provide utilities to register callbacks to invoke on suspended asynchronous response state changes. We can register two types of callbacks in RESTEasy:
- Completion callbacks executed at the end of the requests or when they fail
- Connection callbacks executed when a client connection is closed or lost
Here a sample of registration of a completion callback:
@GET @Path("/withCallback") publicvoid asyncGetWithCallback(@Suspendedfinal AsyncResponse asyncResponse) { asyncResponse.register(newCompletionCallback() { @Override publicvoidonComplete(Throwable throwable) { if (throwable == null) { numberOfSuccessResponses++; } else { numberOfFailures++; lastException = throwable; } } }); new Thread(new Runnable() { @Override publicvoid run() { String result = ... asyncResponse.resume(result); } }).start...