Java Reference
In-Depth Information
Note
To be precise, in our example, we are using a conditional observer that is denoted by the
expression notifyObserver = Reception.IF_EXISTS . This means that in
practice, the observer method is only called if an instance of the component already
exists. If not specified, the default option ( ALWAYS ) will be that the observer method is
always called. (If an instance doesn't exist, it will be created.)
In the newest CDI version, it is possible to get additional information about the fired event
in the observer by adding an EventMetadata parameter to the observer's method.
Whenever a change in our list of seats occurs, we will use the
javax.enterprise.event.Event object to notify the observer about the changes.
This will be done in our singleton bean, which gets injected with the seat's event [1] , and
notifies the observer by firing the event when a seat is booked [2] :
package com.packtpub.wflydevelopment.chapter4.boundary;
import javax.enterprise.event.Event;
@Singleton
@Startup
@AccessTimeout(value = 5, unit = TimeUnit.MINUTES)
public class TheatreBox {
@Inject [1]
private Event<Seat> seatEvent;
@Lock(WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat); [2]
}
// Rest of the code stays the same, as in the previous
chapter
}
Search WWH ::




Custom Search