Scrivio Interview library
InternalsFoundations6 min read · updated 12 November 2026

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?

publishEvent() wraps a plain object in a PayloadApplicationEvent and hands it to the context's ApplicationEventMulticaster, by default a SimpleApplicationEventMulticaster. The multicaster looks up the listeners that match the event type, using a cache keyed by event type and source type, and calls each one in turn on the same thread. If a listener throws, the exception travels back through publishEvent() to OrderService, unless an ErrorHandler is set on the multicaster, and because everything runs on one thread the listener shares the publisher's transaction.

Why this question is asked

Knowing that events are synchronous is the start. Knowing the steps behind publishEvent() explains a whole set of production behaviours: why a listener failure can roll back an order, why a listener sees data that was never committed, and where to plug in error handling or an executor. Interviewers ask this internals question to see whether we can reason from the mechanism to the consequences. In this answer, we will follow one publishEvent() call step by step, look at how listeners are found and cached, and then see what happens when a listener throws, including inside a @Transactional method.

1A Quick Recap of the Parts Involved

Three parts work together. The ApplicationContext is the object we call publishEvent() on, through the ApplicationEventPublisher interface. The ApplicationEventMulticaster is a helper the context owns; its job is to know all listeners and deliver events to them. A listener is anything that implements ApplicationListener, and every @EventListener method is turned into one at startup by an adapter class called ApplicationListenerMethodAdapter.

Unless we define a bean named applicationEventMulticaster ourselves, the context creates a SimpleApplicationEventMulticaster with no task executor and no error handler. Those two empty settings decide most of the behaviour below.

2Step by Step Through publishEvent()

Let us follow publisher.publishEvent(new OrderPlaced(id)) from OrderService.

First, the context checks the type of the object. OrderPlaced is not an ApplicationEvent, so the context wraps it in a PayloadApplicationEvent and records its type, so that listeners can be matched on OrderPlaced rather than on the wrapper. Second, if the context is still starting and its multicaster is not ready, the event is kept in a list of early events and delivered once the multicaster exists; at normal runtime this step is skipped. Third, the context calls multicastEvent(event, type) on the multicaster. Fourth, if the context has a parent context, as in some web setups with a parent and child context, the event is published to the parent as well, so the parent's listeners also see it.

Inside the multicaster, two things happen: it finds the listeners for this event, and it invokes each of them. We look at both next.

Step throughOne publishEvent() call
OrderServiceContextMulticasterListener
publishEvent(new OrderPlaced(id))
wrap in PayloadApplicationEvent
multicastEvent(event, type)
find listeners (cache by event and source type)
invoke on the same thread
returns, or throws
returns, or rethrows
returns, or the listener's exception

8 messages in order. Play them, or step through them one at a time.

With no executor, every listener runs inside the caller's publishEvent() call.

3Finding the Matching Listeners

To decide whether a listener wants an event, the multicaster asks it whether it supports the event type and the source type. For an @EventListener method, the supported type comes from the method's parameter, including generics. Checking every listener for every event would be slow in an application with hundreds of listeners, so the result is cached. The cache key combines the event type and the source type, so the second OrderPlaced event goes straight to the cached list.

The list is sorted by @Order or the Ordered interface, so listeners with an order run in that order, and listeners without one come after the ordered ones. An @EventListener with a condition attribute is still in the list; its condition is evaluated each time just before it is invoked.

4Invoking Each Listener

For each listener in the list, the multicaster checks whether it has a task executor. It has none by default, so it calls the listener directly, one after another, on the thread that called publishEvent(). For an @EventListener, the adapter gets the target bean from the context and calls the method by reflection.

Because this is a plain call on the same thread, everything bound to the thread is shared. That includes the transaction: if OrderService.place() is @Transactional, the listener runs inside that transaction, and a repository call in the listener joins it and sees the order that was saved but not yet committed. It also includes the security context and logging context, such as MDC values.

Java
@Transactional
public void place(long id) {
orders.save(new Order(id)); // INSERT, not yet committed
publisher.publishEvent(new OrderPlaced(id));
}
@EventListener
void on(OrderPlaced e) {
log.info("{}", orders.existsById(e.id())); // true: same transaction sees the insert
log.info("{}", TransactionSynchronizationManager.isActualTransactionActive()); // true
}

5What Happens When the Listener Throws

Now suppose the listener throws an exception. With no error handler, the multicaster does not catch it, so it propagates out of multicastEvent(), out of publishEvent(), and into OrderService.place(). Any listeners that had not run yet are skipped. If the exception is a checked exception, the adapter wraps it in an UndeclaredThrowableException; a RuntimeException passes through unchanged.

Java
@EventListener
void on(OrderPlaced e) {
throw new IllegalStateException("loyalty service down");
}
// orderService.place(42):
// IllegalStateException: loyalty service down (thrown from publishEvent)
// the @Transactional proxy sees a RuntimeException and rolls back: order 42 is not saved

This answers the follow-up about a @Transactional publisher. Because the exception leaves place() as a RuntimeException, the transaction proxy rolls the whole transaction back, so a failing side effect, such as a loyalty update, undoes the order itself. Sometimes that is what we want, when the listener enforces part of the business rule. Often it is not, and then we either catch inside the listener, use @TransactionalEventListener so the side effect runs after commit, or set an ErrorHandler on the multicaster, which catches listener exceptions and handles them, for example by logging, so the publisher continues. Note that an ErrorHandler on the multicaster applies to every listener in the application, so it is a global decision.

6Follow-up questions

How would an exception in the listener affect a @Transactional publisher?

The exception propagates out of publishEvent() into the transactional method. If it is a RuntimeException, or an Error, the transaction proxy marks the transaction for rollback, so the work done by the publisher is undone too. Even if the publisher catches the exception, the transaction may already be marked rollback-only if the listener called another transactional bean that failed, and the commit then fails with UnexpectedRollbackException.

If I configure a task executor on the multicaster, what changes?

Listeners that support async execution are then run on the executor's threads, so publishEvent() returns immediately, exceptions no longer reach the publisher, and listeners no longer share its transaction. Transactional event listeners opt out of this since Spring 6.1, because they must register with the publisher's transaction. It is a global change and is usually better replaced by @Async on specific listeners.

Does the listener lookup cache cause problems when listeners are added at runtime?

No. When a listener is added or removed through the context, the multicaster clears its cache, so the next event recomputes the matching list.

7Conclusion

In this answer, we followed publishEvent() step by step. The context wraps a plain object in a PayloadApplicationEvent, buffers it if it is still starting, hands it to the SimpleApplicationEventMulticaster and also publishes to any parent context. The multicaster finds matching listeners through a cache keyed by event and source type, sorted by order, and calls each on the same thread. So a listener shares the publisher's transaction and sees uncommitted state, and its exception propagates back to the caller and can roll the transaction back, unless an ErrorHandler is set on the multicaster.

What to say in the interviewAbout 80 seconds aloud

Open with

When OrderService calls publishEvent with an OrderPlaced record, the application context first wraps it in a PayloadApplicationEvent, because the event system works with ApplicationEvent internally, and it remembers the payload type.

Then, if they want more

If the context were still starting, the event would be buffered as an early event, but at runtime it goes straight to the ApplicationEventMulticaster, which by default is a SimpleApplicationEventMulticaster. If there is a parent context, the event is published there too.

The multicaster finds the listeners that support this event type and source type; that lookup is cached by those two types, and the list is sorted by @Order. Then it invokes each listener in turn. By default it has no task executor, so each listener runs on the publishing thread, inside the publishEvent call.

For @EventListener methods, an adapter fetches the bean and calls the method by reflection, evaluating any condition first. Because it is the same thread, the listener joins the publisher's transaction and sees uncommitted data.

If a listener throws, the multicaster does not catch it unless an ErrorHandler is configured, so the exception propagates out of publishEvent into OrderService, the remaining listeners are skipped, and if OrderService is transactional, a runtime exception rolls back the whole transaction, including the order.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "When OrderService calls publisher.publishEvent(new OrderPlaced(id)) inside a method...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/f1fdd3c3b6/