36.8. 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 observe 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, as summarized in the table.

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 determine which observer methods are notified of the event. For more information, see observer resolution rules.

Table 36.3. Corresponding Seam 2 and CDI Event Processes

Process Seam 2 CDI

Raising event
Events.instance().raiseEvent(“foo”);
@Inject private Event<Foo> event;
event.fire(new Foo());

Observing an event
@Observer("foo")
public void observe() { }
public void observe(@Observes Foo foo) {}

The CDI specification itself supports both synchronous delivery of events as well as transactional observer methods, which are invoked at specific points in the life-cycle of a transaction. Unlike in Seam 2 where an entire event can be bound to a specific transaction phase, it is the observer method that declares the transaction phase in the 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.

Table 36.4. Corresponding Seam 2 and CDI Event Delivery Modes

Delivery mode Seam 2 - Raising events (observing is always the same as shown above) CDI - Observing events (raising is always the same as shown above)

Synchronous
Events.instance().raiseEvent(“foo”);
public void observe(@Observes Foo foo) {}

Transaction success phase
Events.instance().raiseTransactionSuccessEvent(“foo”);
public void observe(
  @Observes(during = TransactionPhase.AFTER_SUCCESS)
  Foo foo) { }

Transaction completion phase
Events.instance().raiseTransactionCompletionEvent(“foo”);
public void observe(
  @Observes(during = TransactionPhase.AFTER_COMPLETION)
  Foo foo) { }

Asynchronous
Events.instance().raiseAsynchronousEvent(“foo”);

No direct alternative. Can be implemented using EJB @Asynchronous annotated methods.
@Stateless
public class FooObserver {
 @Asynchronous
 public void observe(@Observes Foo foo) { }
}

Timed
Events.instance().raiseTimedEvent(“foo”, new Schedule(1000l));

No direct alternative. Scheduling can be implemented using EJB @TimerService or with the help of the DeltaSpike Scheduler module.