Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

How do you make an @EventListener run asynchronously, and what changes for the publisher compared with a synchronous listener?

We add @Async to the listener method and enable async processing with @EnableAsync on a configuration class. publishEvent() then returns immediately and the listener runs later on a task executor thread. As a result, the listener's exceptions no longer reach the publisher (for a void method they go to the AsyncUncaughtExceptionHandler), it does not take part in the publisher's transaction, ThreadLocal context such as MDC or the security context is not copied unless a TaskDecorator does it, and its return value is not published as an event.

Why this question is asked

A synchronous listener makes the publisher wait. If confirming an order sends an email that takes two seconds, the customer waits two seconds longer for the checkout page. Making the listener asynchronous is a one-line change, which is exactly why it is worth understanding: that line changes error handling, transactions and logging context too. Interviewers ask this to see whether we know the consequences, not only the annotation. In this answer, we will make a listener asynchronous step by step, then go through each thing that changes for the publisher, and finish with propagating MDC values.

1Making the Listener Asynchronous

Two pieces are needed. @EnableAsync on a configuration class turns on Spring's async support, which wraps beans that have @Async methods in a proxy. @Async on the listener method tells that proxy to hand the call to an executor instead of running it on the caller's thread.

Java
@Configuration
@EnableAsync
class AsyncConfig {}
@Component
class ConfirmationEmail {
@Async
@EventListener
public void on(OrderPlaced e) {
log.info("sending on {}", Thread.currentThread().getName());
mail.send(e.orderId()); // takes about 2 seconds
}
}
// publisher:
long t0 = System.currentTimeMillis();
publisher.publishEvent(new OrderPlaced(42));
log.info("publish took {} ms", System.currentTimeMillis() - t0);
// publish took 1 ms
// sending on task-1
Step throughA synchronous and an @Async listener
PublisherMulticasterAsync proxyExecutor thread
publishEvent(OrderPlaced)
on(OrderPlaced)
submit the call
returns at once
publishEvent returns
on(OrderPlaced) runs, sends the email

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

The multicaster still calls the listener synchronously; the async proxy is what moves the work.

Notice the thread name. In Spring Boot, @Async uses the auto-configured executor, a ThreadPoolTaskExecutor whose threads are named task-1, task-2 and so on, configurable with spring.task.execution properties. Without Boot, or with several executors, we name one with @Async("emailExecutor"). Also notice that publishEvent() now returns in about a millisecond instead of two seconds.

2What Changes: Exceptions

With a synchronous listener, an exception propagates back through publishEvent() to the publisher. With @Async, by the time the listener throws, the publisher has long since moved on. For a void method, Spring passes the exception to an AsyncUncaughtExceptionHandler, which by default just logs it. For a method returning a Future, the exception is stored in the future, which nobody reads in the event case.

Java
@Configuration
@EnableAsync
class AsyncConfig implements AsyncConfigurer {
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (ex, method, params) -> {
log.error("async listener {} failed", method.getName(), ex);
failures.increment(); // a metric we alert on
};
}
}

So the publisher can no longer react to a failure, and a failing listener can no longer roll back the publisher's transaction. That is often the point, but it means the listener needs its own error handling and, for important work, a durable retry mechanism.

3What Changes: Transactions and Thread Context

A transaction in Spring is bound to the thread. The executor thread has no transaction, so the listener does not see the publisher's uncommitted data and cannot join its transaction. If the listener uses repositories, each call starts its own transaction, or we put @Transactional on the listener to group its work. There is also a race: if the publisher is still inside its transaction, the listener may run before the commit and not find the order at all. Combining @Async with @TransactionalEventListener, so the listener is triggered after commit and then runs on the executor, avoids that.

Other ThreadLocal state is lost the same way. The MDC, the logging context that puts values like a request id on every log line, is empty on the executor thread. The Spring Security context is empty too, unless we configure delegation. Locale and request attributes are also gone. So logs from the listener lose their request id, and code that checks the current user finds nobody.

4What Changes: Return Values

A plain @EventListener that returns a value has it published as a new event. With @Async on a method declared to return a plain type, the proxy returns to the multicaster before the method has run, so there is no value to publish, and Spring's documentation states that this feature is not supported for asynchronous listeners. If an async listener needs to trigger a follow-up event, it injects ApplicationEventPublisher and publishes it explicitly.

5Follow-up questions

How would you propagate MDC values to async listeners?

Register a TaskDecorator on the executor. The decorator runs on the submitting thread, captures MDC.getCopyOfContextMap(), and returns a Runnable that sets that map on the executor thread before running the task and clears it afterwards. In Spring Boot, a TaskDecorator bean is applied to the auto-configured executor. With Micrometer tracing, the context-propagation library offers a ContextPropagatingTaskDecorator that carries tracing and MDC context in the same way; check your version.

Should I put @Async on every listener that does I/O?

Only on those where the publisher should not wait and where losing the work on a crash is acceptable, because queued tasks live in memory. Work that must happen belongs in an outbox or event publication registry, not only in an executor queue.

What executor settings matter for async listeners?

A bounded queue and a sensible maximum pool size, so a burst of events cannot exhaust memory, and a rejection policy we have chosen on purpose. Boot's defaults use an unbounded queue, so for heavy event traffic I would set spring.task.execution.pool.queue-capacity or define a dedicated executor.

6Conclusion

In this answer, we made a listener asynchronous with @EnableAsync and @Async, so publishEvent() returns immediately and the listener runs on an executor thread. We then saw what that changes: exceptions go to the AsyncUncaughtExceptionHandler instead of the publisher, the listener is outside the publisher's transaction and may even run before its commit, ThreadLocal context such as MDC and security is lost unless a TaskDecorator copies it, and return values are not published as events.

What to say in the interviewAbout 90 seconds aloud

Open with

I add @Async to the listener method and make sure async support is enabled with @EnableAsync on a configuration class. Spring then wraps the bean in a proxy, and when the multicaster calls the listener, the proxy submits the call to a task executor and returns at once, so publishEvent returns immediately.

Then, if they want more

In Spring Boot, that is the auto-configured thread pool unless I name a specific executor. For the publisher, a few things change. Exceptions no longer reach it; for a void listener they go to the AsyncUncaughtExceptionHandler, which only logs by default, so I would configure one that records a metric.

The listener no longer takes part in the publisher's transaction, so it cannot see uncommitted data and cannot roll the publisher back, and if the publisher is still in its transaction, the listener might even run before the commit. Combining @Async with @TransactionalEventListener avoids that.

ThreadLocal context is not copied either: MDC values like the request id and the security context are empty on the executor thread unless I add a TaskDecorator that copies them. Finally, the return value of an async listener is not published as an event, so follow-up events must be published explicitly.

And because queued tasks are in memory, I only use this for work that may be lost on a crash.

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 "How do you make an @EventListener run asynchronously, and what changes for the publisher...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/84f95343b7/