Async improvements

In JAXRS 2.0, you can build an asynchronous RESTful APIs as the following.

@GET
public void getAsync(final @Suspended AsyncResponse res) {
    res.setTimeoutHandler(
            (ar) -> {
                ar.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE)
                        .entity("Operation timed out --- please try again.").build());
            }
    );
    res.setTimeout(1000, TimeUnit.MILLISECONDS);
    executor.submit(() -> {
        //do long run operations.
        try {
            LOG.log(Level.INFO, " execute long run task in AsyncResource");
            //Thread.sleep(new Random().nextInt(1005));
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            LOG.log(Level.SEVERE, "error :" + ex.getMessage());
        }
        res.resume(Response.ok("asynchronous resource").build());
    });
}

Inject @Suspended AsyncResponse res as the method parameter, call res.resume() to return response result asynchronously.

Or combined with EJB @Asynchronous.

JAXRS 2.1 allow you return a CompletionStage directly.

Grab the source codes from my GitHub account, and have a try.

Last updated

Was this helpful?