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")
public void asyncGetWithCallback(@Suspended final AsyncResponse asyncResponse) {
asyncResponse.register(new CompletionCallback() {
@Override
public void onComplete(Throwable throwable) {
if (throwable == null) {
numberOfSuccessResponses++;
} else {
numberOfFailures...