All the magic of this recipe relies on the AsyncService class, so we will focus on that.
First, we ask the server for an instance of an executor:
Resource(name = "LocalManagedScheduledExecutorService")
private ManagedScheduledExecutorService executor;
However, we are not asking for just any executor—we want an executor that's specific to scheduling:
ScheduledFuture<User> result = executor.schedule(new AsyncTask(),
5, TimeUnit.SECONDS);
So, we schedule our task to be executed in 5 seconds. Note that we also don't use a regular Future instance, but rather ScheduledFuture:
while (!result.isDone()) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
System.err.println(ex.getMessage());
}
}
The following code snippet shows you how we write the results to the response:
response.resume(Response.ok(result.get()).build());