Application events
17 questions in 5 subtopics.
ApplicationEvent and publishing
How do you publish an application event in Spring today, and do you still need to extendApplicationEvent? Show a minimal publisher and the type the listener receives.When OrderService calls publisher.publishEvent(new OrderPlaced(id)) inside a method, what happens step by step until the listener returns, and what happens if the listener throws?A team uses application events to decouple an order module from email, analytics and loyalty modules in one Spring Boot service. When are in-process events a good fit, and when should they be replaced by a message broker or an outbox?Over a decade your organisation moved from synchronous calls to in-process events to brokers and event streaming. What lessons would you pass on about when an event-driven design pays off and when it creates long-term cost?@EventListener
Read this listener:@EventListener(condition = "#event.amount > 1000")
public AuditEntry onPayment(PaymentReceived event) {
return new AuditEntry(event.id());
}
AuditEntry?A @Component with an @EventListener method for ApplicationStartingEvent never logs anything, while its listener for ApplicationReadyEvent works. Why?How does Spring turn @EventListener methods into listeners, and why can a listener on a prototype-scoped or lazily created bean behave unexpectedly?@TransactionalEventListener
What does@TransactionalEventListener do, what are its phases, and what happens if the event is published outside a transaction?An AFTER_COMMIT @TransactionalEventListener saves an AuditRecord with a Spring Data repository. No error is logged, but the row never appears in the database. Explain the cause and the fix.Several teams use @TransactionalEventListener(AFTER_COMMIT) to send Kafka messages after database commits, and incidents show occasional lost messages. What standard would you set for reliable post-commit publishing across services?Teams want to use in-process Spring events as the first step towards splitting a monolith into services. How would you judge this strategy over several years?Asynchronous events
How do you make an@EventListener run asynchronously, and what changes for the publisher compared with a synchronous listener?A developer sets a TaskExecutor on the bean named applicationEventMulticaster so that all events are asynchronous. What breaks, and what would you recommend instead?Several services use @Async event listeners for side effects such as emails and search indexing, and work is lost on every deploy. What standard would you set for asynchronous side effects across services?Event ordering
Two@EventListener methods handle the same OrderPlaced event. How do you control which runs first, and what ordering do you get without any configuration?In a modular monolith, events between modules have grown to dozens of listeners with implicit ordering assumptions and hidden coupling. How would you bring this under control without banning events?Event ordering assumptions have caused subtle bugs in several of your systems, in-process and across brokers. What principles would you set for teams designing event flows for the long term?No question matches these choices.