Java Reference
In-Depth Information
theatreBox.getSeatList().get(seatId);
// Business checks
if (seat.isBooked()) {
throw new SeatBookedException("Seat
Already booked!");
}
if (seat.getPrice() > money) {
throw new NotEnoughMoneyException("You
don't have enough money to buy this ticket!");
}
theatreBox.buyTicket(seat);
money = money - seat.getPrice();
logger.info("Seat "+seatId+ " booked.");
return "Seat "+seatId+ " booked.";
}
}
As you can see, this bean bears an @PostConstruct annotation for initializing a
session variable (money) that will be used to check that the customer has enough
money to buy the ticket.
Besides this, the ultimate purpose of our SFSB is to contact the buyTicket method
of our singleton after having performed some business checks.
If the business checks do not pass, the application will issue some exceptions. This
is the case, for example, if the seat has already been booked or if the customer
hasn't got enough money to buy the ticket. In order to keep our conversation going,
it's important that our exceptions be an extension of the generic Exception class.
public class SeatBookedException extends
Exception {
. . . .
}
If we'd rather use a runtime exception (for example, EJBException ), the bean in-
stance will be discarded, resulting in our communication being dropped between the
Search WWH ::




Custom Search