Specifying and handling asynchronous timeouts

In synchronous execution, default timeouts usually exist to avoid blocking a thread eternally. Even if there's no timeout, there's always a thread waiting for a result or an exception that indicates something is going on. This is not the case in asynchronous execution because no thread is waiting. If no callback or exception is triggered, there's nothing to indicate that a request is still in progress. Adding a timeout is essential to ensure a request is completed at some point, and we don't lose track of it.

In an asynchronous JAX-RS method, setting a timeout is as easy as calling the setTimeout method on the AsyncResponse object. We call this method as soon as possible at the beginning of an asynchronous method to start the timeout scheduler. If the specified time elapses since the setTimeout method was called, the REST response is completed with an exception even without calling the resume method, as follows:

public void getLocationsWithTemperature(@Suspended AsyncResponse asyncResponse) {
asyncResponse.setTimeout(5, TimeUnit.MINUTES);
...
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.27.178