Information Technology Reference
In-Depth Information
requestCreated(@javax.enterprise.event.Observes
NewMessageEvent e ){
…
}
public void displayNotification( String text ) {
notification.fire( new NotificationEvent( text ) );
}
…
}
As you can see, all it took to listen to event firings was the creation of a method that had a
parameter with the
@Observes
annotation. In the previous code fragment, the method is
called
requestCreated
.
Meanwhile, we need two things to fire events:
• A
javax.enterprise.event.Event
object
• A
fire
method needs to be invoked with a new event instance
In the previous code fragment, the
fire
method is invoked from inside the
dis-
playNotification
method. Using the
Event
object is quite simple. Thanks to CDI
and Errai, we don't need to do anything else than to inject the instance with the
@Inject
annotation, and then let the framework take care of creating the actual object and setting it
to any component.