Sinceversion 3.3, axon introduced a new component for scheduling: theDeadlineManager. 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@AggregateIdentifier.
We’ll create thisShowaggregate using thePlanShowcommand andShowPlannedevent, 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@CommandHandlerfor 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 theTicketsReservedevent, with the generatedreservationDeadlineIdas 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 aMap<String, List<Reservation>> reservationsfield:
AReservationcontains thereservationId,amountof tickets reserved and the associateddeadlineId. 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@DeadlineHandlerwill receive the payload as argument. In our case, that’s thereservationId. TheReservationExpiredevent is applied which in turn uses the storedReservationto 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 ourGitHub 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! 👏👏👏