How it works...

Let's first check the getUser method from the session bean:

    @Asynchronous    public Future<User> getUser(){        long id = new Date().getTime();        User user = new User(id, "User " + id);        return new AsyncResult(user);    }

Once we user the @Asynchronous annotation, we have to turn its returning value to a Future instance of something (in our case, User).

We also created a void method to show you how to create a non-blocking code with session beans:

    @Asynchronous    public void doSomeSlowStuff(User user){        try {            TimeUnit.SECONDS.sleep(5);        } catch (InterruptedException ex) {            System.err.println(ex.getMessage());        }    }

And finally, we created our calling endpoint:

    @GET    public void asyncService(@Suspended AsyncResponse response){        try { Future<User> ...

Get Java EE 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.