Java Reference
In-Depth Information
private TheatreBox theatreBox;
@Resource
private TimerService timerService; [1]
@Schedule(hour = "*", minute = "*/1", persistent =
false) [2]
public void automaticCustomer() throws
NoSuchSeatException {
final Optional<Seat> seatOptional = findFreeSeat();
if (!seatOptional.isPresent()) {
cancelTimers();
logger.info("Scheduler gone!");
return; // No more seats
}
final Seat seat = seatOptional.get();
try {
theatreBox.buyTicket(seat.getId()); [3]
} catch (SeatBookedException e) {
// do nothing, user booked this seat in the
meantime
}
logger.info("Somebody just booked seat number " +
seat.getId());
}
private Optional<Seat> findFreeSeat() {
final Collection<Seat> list = theatreBox.getSeats();
return list.stream()
.filter(seat -> !seat.isBooked())
.findFirst();
}
private void cancelTimers() { [4]
for (Timer timer : timerService.getTimers()) {
timer.cancel();
}