concept event in category ejb

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.
CDI is a powerful solution to the problem. It provides EJB (as well as all other APIs and components in the Java EE environment) best-of-breed, next-generation, generic dependency injection and context management services. CDI features include injection, automatic context management, scoping, qualifiers, component naming, producers, disposers, registry/lookup, stereotypes, interceptors, decorators, and events. Unlike many older dependency injection solutions, CDI is completely type-safe, compact, futuristic, and annotation-driven. We’ll cover CDI in detail in chapter 12.
One of the dangers with large feature-rich applications is tight coupling. There’s the danger that as the application grows it’ll increasingly become a tangled mess. Injection will be performed just so a listener can be registered. Registration of listeners is fraught with many challenges, because this is server-side development where objects may be serialized and new sessions initiated. Although injection simplifies the process of acquiring a reference to an object, it isn’t quite enough. What you want to be able to do is declare that a bean listens for certain events and then lets the framework handle the event routing. This is accomplished by placing an annotation on the method that receives the event, and the container handles the rest. This would be analogous in Swing to marking a method that processes an ActionListenerEvent and having the listener registration performed automatically.
Dependency injection is extremely useful for acquiring references to other beans. But CDI has a slight twist on dependency injection with support for injecting events. This enables events to be delivered to other beans without any compile-time dependencies. The genius of this approach is that you can use an annotation to mark a method as being a recipient of the event and you’re finished. The event will be delivered to your bean without any further work. You don’t need to implement an interface or register your class as a listener to receive events. The container takes care of everything. Raising an event is similarly as easy; the object responsible for dispatching events is also injected into the class that needs to send the event.
The @Observes annotation is placed on one or more parameters of the method that’s to receive an event. The type of the parameter is the type of the event. For example, if the type of the event were an ActionEvent, then the method would receive all ActionEvents that are fired. Attaching qualifiers to the method injection points provides additional specificity beyond just the type. Additional parameters are treated as beans that the container will either look up or instantiate.
To fire an event, the bean that will send the event uses an Event object that’s typed for the object that’s to be fired. It’s injected using the @Inject annotation and can optionally possess qualifier annotations that will narrow the list of potential targets for the event. The Event object has a fire method that’s used to send an event.
To better understand how events work in CDI, turn your attention to an example from ActionBazaar. In ActionBazaar, when a new item is listed, multiple beans need to be notified. The beans that need to be notified may change over time and a new module might be added to the application—obviously you don’t want to edit the BidController each time. The ItemController therefore fires an event, which other beans, including LandingController and TwitterNotifier, are listening to and waiting to act upon. Using CDI’s form of the observer pattern results in code with low coupling—ItemController doesn’t have a reference to these other beans, and the other beans that are listening don’t need to acquire a reference to ItemController to receive events. Figure 12.4 illustrates the event-delivery process.
Let’s start with the addItem method in ItemController. The relevant code for firing events is shown in the following listing.
In this listing the ItemController broadcasts new items. The event object is first injected into the bean
. It’s qualified with the @NewItem
qualifier so that you can target listeners who are specifically interested in a new item that has just been listed. The event object itself makes use of Java’s Generics to ensure that you only call fire with the right type of object. Firing is relatively straightforward—you just call fire on the event object, passing in the item
. The code for processing an event is shown in the next listing.
. It’s qualified with the @NewItem
qualifier so that you can target listeners who are specifically interested in a new item that has just been listed. The event object itself makes use of Java’s Generics to ensure that you only call fire with the right type of object. Firing is relatively straightforward—you just call fire on the event object, passing in the item
. The code for processing an event is shown in the next listing.