Since version 3.3, axon introduced a new component for scheduling: the DeadlineManager. Traditionally, scheduling events in Axon is done using the EventScheduler, applying an event after a certain amount of time. Should you just want to schedule a deadline and decide at that point whether or not you need to publish an event, now you can. In this blog post, we’ll explore a simple use case where we hold a reservation for a certain amount of time, and cancel it if the time expires before the reservation is confirmed.
On April 23, 25 and 26, Drake will give a concert at the Ziggo Dome in Amsterdam. The Ziggo Dome has a capacity of 17,000 people. We can model these shows into an aggregate and start selling some tickets! To start with, a Show will have a name, time and available tickets. Oh, and of course a showId as
We’ll create this
Show aggregate using the
PlanShow command and
ShowPlanned event, containing the fields listed above.
In order to buy and subsequently pay for tickets, users should be able to reserve them for a limited amount time so they have the chance to get through the payment process. Here is where we will use Axon’s DeadlineManager to keep track of this reservation. We will schedule the deadline as soon as the users makes the reservation: at the
@CommandHandler for ReserveTickets:
Here, we check if we have enough tickets available, and if that is the case we start the clock for 15 minutes, and apply the
TicketsReserved event, with the generated
reservationDeadlineId as event MetaData, so we can keep track of it. We can use this later to cancel the deadline. In the
@EventHandler, we will decrease the available tickets and keep track of the reservation, in a
Map<String, List<Reservation>> reservations field:
Reservation contains the
amount of tickets reserved and the associated
deadlineId. We put it in a map so we can refer to it later when either the reservation expires or gets confirmed.
After 15 minutes, the reservation expires and the tickets should be released back in the pool of available tickets. The
@DeadlineHandler will receive the payload as argument. In our case, that’s the
ReservationExpired event is applied which in turn uses the stored
Reservation to release the reserved tickets back in the pool:
Confirming the reservation
When a user pays for the tickets, the reservation gets confirmed. When this happens, the deadline is canceled and the tickets are definitely gone. We can now write other
@EventHandlers to email the tickets to the person that made the reservation. 🎫
The DeadlineManager is a relatively new addition to the Axon framework and a nice alternative to the classic EventScheduler. An advantage in my eyes is that compared to the EventScheduler which always applies an event, the DeadlineManager does not, leaving you to decide what events to publish when a deadline is reached. Since the history of events is your single source of truth, it is a good thing to be in control.👍
Please check out the full code example in our GitHub repository! It also features a simple in-memory configuration for Spring Boot with some demo commands. If you can wait 15 minutes, you’ll see the DeadlineManager in action! You can also refer to the JUnit test for more examples.
Hopefully, you’ve enjoyed this post about the Axon DeadlineManager. If you did, do not hesitate to clap! 👏👏👏