How it works...

First, we ask the application server to create a Event source for the User POJO:

@Injectprivate Event<User> event;

This means that it will listen to any events fired against any User object. So what we need to do is create a method to deal with it:

public void onFireEvent(@ObservesAsync User user){    response.resume(Response.ok(user).build());}

So now this method is the proper listener. The @ObserversAsync annotation guarantees it. So once an async event is fired, it will do whatever we asked (or coded).

Then, we created a simple asynchronous endpoint to fire it:

@GETpublic void asyncService(@Suspended AsyncResponse response){    long id = new Date().getTime();    this.response = response; event.fireAsync(new User(id, "User " + id)); ...

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.