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 use the @Asynchronous annotation, we have to turn its returning value into a Future instance of something (in our case, User).

We have 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());
}
}

Finally, we create our calling endpoint:

    @GET
public void asyncService(@Suspended AsyncResponse response){
try {
Future<User< result = userBean.getUser();

while(!result.isDone()){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
System.err.println(ex.getMessage());
}
}

response.resume(Response.ok(result.get()).build());
} catch (InterruptedException | ExecutionException ex) {
System.err.println(ex.getMessage());
}
}

As getUser returns Future, we can work with an async status check. Once this is done, we write the results in the response (also asynchronous).