2. An event has to be fired by an injected javax.enterprise.event.Event class. It can be injected into an EJB 3.1:
@Named("messenger")
@Stateless
public class HelloMessenger {
@Inject Event<HelloEvent> events;
public void hello(){
events.fire(new HelloEvent("from bean " + System.currentTimeMillis()));
}
}
3. The event can be consumed by another Session Bean / managed bean. You only have to use the @Observes annotation:
@Stateless
public class HelloListener {
public void listenToHello(@Observes HelloEvent helloEvent){
System.out.println("HelloEvent: " + helloEvent);
}
}
4. You can access the EJB 3.1 directly - without any backing beans. You only have to use the name (messenger) from the @Named annotation in the JSF 2.0 page and bind the action to the method (hello()) name:
<h:body>
<h:form>
<h:commandButton value="Fire!" action="#{messenger.hello}"/>
</h:form>
[...]
The project (CDIEJB3Events) was implemented in few minutes with NetBeans 6.8, tested with Glassfish v3 and pushed into: http://kenai.com/projects/javaee-patterns/. The entire WAR (JSF 2.0 , EJB 3.1, CDI) is 8 kB small. The initial deployment took (897 ms):
INFO: Initializing Mojarra 2.0.2 (FCS b10) for context '/CDIEJB3Events'
INFO: Loading application CDIEJB3Events at /CDIEJB3Events
INFO: CDIEJB3Events was successfully deployed in 897 milliseconds.