Java Reference
In-Depth Information
import javax.transaction.UserTransaction;
...
public class TransactionServlet extends HttpServlet {
@Resource UserTransaction transaction;
...
}
Using Events in CDI Applications
Events allow beans to communicate without any compile-time dependency. One bean can
define an event, another bean can fire the event, and yet another bean can handle the event.
The beans can be in separate packages and even in separate tiers of the application.
Defining Events
An event consists of the following:
• The event object, a Java object
• Zero or more qualifier types, the event qualifiers
an event using three properties, which have setter and getter methods:
public String paymentType;
public BigDecimal value;
public Date datetime;
public PaymentEvent() {
}
The example also defines qualifiers that distinguish between two kinds of
Pay-
mentEvent
. Every event also has the default qualifier
@Any
.
Using Observer Methods to Handle Events
An event handler uses an
observer method
to consume events.
Each observer method takes as a parameter an event of a specific event type that is an-
notated with the
@Observes
annotation and with any qualifiers for that event type. The
observer method is notified of an event if the event object matches the event type and if
all the qualifiers of the event match the observer method event qualifiers.
The observer method can take other parameters in addition to the event parameter. The
additional parameters are injection points and can declare qualifiers.