Spring Boot services · Chapter 37 of 41
Events and scheduling
By the end of this chapter you can announce what happened with an event, react to it only once it is really saved, and run a job on a schedule. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 5 tasks
- Spring Boot
- In the all labs plan
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.
Why you need this
A new order has to reach the kitchen board, text the customer and, from next month, add Anjali's loyalty points. If place() calls them all itself, OrderService grows a dependency for every new idea. Worse, last week a customer at New Delhi got "Order confirmed" for an order that had failed and rolled back: the SMS went out before the commit.
Ravi has a second problem: orders nobody collects stay PLACED for ever. He wants them cancelled after half an hour, without anyone pressing a button, and nothing you have written so far runs on its own.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
OrderService.java: announce a new order with an event
Make OrderService.place(station, total) publish one OrderPlaced event after it saves the order, carrying the saved order's id, the station and the total: place("SBC", 120) publishes exactly one OrderPlaced(id of the saved order, "SBC", 120).
Keep saving the order stamped with the clock's time, and return the saved order.
- Task 2
OrderListeners.java: a kitchen ticket for every new order
Make OrderListeners.toKitchen run for every OrderPlaced event, straight away, and put one ticket on the kitchen board: the station, a space, a hash sign and the order id, so order 7 at SBC gives "SBC #7".
Two orders give two tickets, in the order they were placed. Needs task 1.
- Task 3 · interview question
Interview: send the SMS only after the order commits
Make OrderListeners.confirmBySms send "Order <id> confirmed at <station>" through the SMS outbox (order 7 at SBC sends "Order 7 confirmed at SBC"), but only once the transaction that placed the order has committed: nothing is sent while that transaction is still open, and nothing ever for an order whose transaction rolls back.
The kitchen ticket from task 2 still goes out at once, for both orders. Needs tasks 1 and 2. Be ready to explain why a plain event listener is wrong for the SMS.
- Task 4
StaleOrderJob.java: cancel the orders that waited too long
Write StaleOrderJob.cancelStale() so it cancels every PLACED order placed more than 30 minutes (MAX_WAIT) before the injected Clock's current instant, and returns how many it cancelled.
With the clock at 10:00, an order placed at 09:15 is cancelled, one placed at 09:50 stays PLACED, and an order that is already CANCELLED is not counted, so it returns 1; run again straight away, it returns 0. Take the time from the clock field, never from the system time.
- Task 5
StaleOrderJob.java: run the job on a schedule from a property
Switch on scheduling in TapriApplication, and make StaleOrderJob.cancelStale a scheduled method whose cron comes from the property tapri.stale-orders.cron, defaulting to every five minutes (0 /5 * * *) when the property is not set.
The Check sets the property to "-", which switches the job off, so nothing may be scheduled then.
What you will learn
- Events: a bean publishes an object, and every listener for that type is called.
- Transactional listeners: a listener that waits for the commit, and never runs after a rollback (the interview question).
- @Scheduled: a method Spring calls on a timer or a cron.
- A Clock bean: time a test can fix.
More in Production
Build "Events and scheduling" yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.