First, we ask the application server to create an Event source for the User POJO:
@Inject
private Event<User< event;
This means that it listens to any events fired against any User object. So, we need to create a method to deal with it:
public void onFireEvent(@ObservesAsync User user){
response.resume(Response.ok(user).build());
}
So, this method is now the correct listener; the @ObserversAsync annotation guarantees this. Once an asynchronous event is fired, it does whatever we ask (or code).
Then, we create a simple asynchronous endpoint to fire it:
@GET
public void asyncService(@Suspended AsyncResponse response){
long id = new Date().getTime();
this.response = response;
event.fireAsync(new User(id, "User " + id));
}