-
Notifications
You must be signed in to change notification settings - Fork 11
Events
Both Seam 2 and CDI beans may produce and consume events in order to communicate with other beans. Unlike method invocation, events allow for decoupled architecture with no compile-time dependency.
In Seam 2, the type of an event is represented by a string value. Observer methods may observer one or more event types. An observer method is notified when an event of a matching type is fired. An event can carry additional objects which are passed as method parameters to the observer method. Synchronous delivery, as well as other types such as asynchronous, timed and transaction-bound delivery modes are supported – these are summarized below.
Unlike Seam 2, the process of observer method resolution is type-safe in CDI. A CDI event is represented by a payload (any Java object) and a set of qualifiers. The Java types of the event payload together with qualifiers to determine which observer methods are notified of the event (See observer resolution rules).
Seam 2 | CDI | |
---|---|---|
Raising event |
|
|
Observing an event |
|
|
The CDI specification itself supports both synchronous delivery of events as well as transactional observer methods which are invoked in specific points in a life-cycle of a transaction. Unlike Seam 2, where an entire event can be bound to a specific transaction phase, it’s the observer method which declares the transaction phase in case of CDI. As a result, observer method invocations for the same event may be executed in different phases of a transaction, which is not possible with Seam 2.
Asynchronous and timed delivery of events is not covered by the CDI specification. These types of event delivery can be implemented either using facilities provided by other parts of the Java EE platform (EJB) or by portable extensions.
Delivery mode | Seam 2 – Raising events (observing is always same as above) |
CDI – Observing events (raising is always same as above) |
---|---|---|
Synchronous |
|
|
Transaction success phase |
|
|
Transaction completion phase |
|
|
Asynchronous |
|
No direct alternative. Can be implemented using EJB @Asynchronous methods.
|
Timed |
|
No direct alternative. Can be implemented using EJB TimerService. |