To communicate between managed beans, JBoss Seam 2.x and Java EE 7 CDI provide managed beans with the ability to generate event objects and capture event objects.
In JBoss Seam 2.x, the type of event is a string representation. An event listener can listen to multiple types of events, and any event generation that matches a type will notify the event listener.
In Java EE 7 CDI, an event is an object that includes a payload (also a Java object) and a set of descriptors. Event listeners are method-safe, where an event listener can only listen to a specific event. The corresponding event listener is notified only if the payload and descriptor of the event match.
The relationship between events and listeners is as follows:
|
|
JBoss Seam 2 |
CDI 1.1 |
|
Generate events |
Events.instance (). RaiseEvent ("foo"); |
Private event<foo> Event;
Event.fire (New Foo ()); |
|
Monitoring Events |
@Observer ("foo")
public void Observe () {} |
public void observe (@Observes foo foo) {} |
For the propagation mechanism of events, JBoss Seam 2.x supports synchronous event propagation, event propagation in transactions, asynchronous event propagation, and timed event propagation. Java EE 7 CDI supports synchronous event propagation and event propagation in transactions. JavaEE7 CDI does not support asynchronous event propagation and timed event propagation, but similar functionality can be achieved through other specifications for Java EE, such as EJBS.
For the propagation of events in a transaction, all event objects in JBoss Seam 2.x can only be executed at the transaction stage specified when the event was emitted. In JavaEE7 CDI, when an event object is emitted, it is not bound to the transaction stage, but instead binds the event object to any stage of the transaction, based on the declaration of the Listener method. That is, the same event object, which is different from the listener method declaration, may be executed at different transaction stages.
Event propagation is shown in the following example:
| Event propagation Mode |
Seam2?—? Raising events (observing is always same) |
Cdi?1.1-? Observingevents (Raising is always same) |
| Synchronous |
Events.instance (). RaiseEvent ("foo"); |
Publicvoid 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
|
No Direct Alternative in CDI.
Can be implemented using EJB&NBSP;
@Asynchronous &NBSP; annotatedmethods.
@Stateless
public class Fooobserver {
@Asynchronous
public void observe (@Observes foo foo) {
} } |
| Timed |
Events.instance ()
. raisetimedevent ("foo", New Schedule (1000l)); |
No Direct Alternative in CDI. Scheduling can implemented using EJB TimerService orwith The help of Deltaspike ' s schedulermodule. |
Supplemental: JBoss Seam 2.x supports asynchronous calls to component methods through the dispatcher mechanism, such as Timerservice or Quartzscheduler.
The JAVAEECDI itself does not support asynchronous method invocations, and only relies on the listener method of the EJB's @asynchronous annotation to implement the asynchronous invocation.
Migrating from JBoss Seam 2.x to Java EE 7 four: event mechanism