Skip to content
LightGuard edited this page Jan 25, 2012 · 2 revisions

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
Events.instance().raiseEvent(“foo”);
@Inject 
private Event<Foo> event;

// in code
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 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
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()
  .raiseTransactionCompletionEvent(“foo”);
No direct alternative. Can be implemented using EJB
@Asynchronous methods.
@Stateless
public clazz FooObserver {
  @Asynchronous
  public void observe(@Observes Foo foo) { }
}
Timed
Events.instance()
  .raiseTimedEvent(“foo”, new Schedule(1000l));
No direct alternative. Can be implemented using EJB TimerService.
Clone this wiki locally