Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Two @EventListener methods handle the same OrderPlaced event. How do you control which runs first, and what ordering do you get without any configuration?

We control the order with @Order on the listener methods, or by implementing Ordered on an ApplicationListener; lower values run first. Without any ordering, the sequence is not guaranteed, so code must not rely on it even if it looks stable. Ordering only means something for synchronous listeners on the same multicaster, since asynchronous ones can finish in any order, and if one listener needs another's result, a single orchestrating listener or a chained event is clearer than relying on order.

Why this question is asked

When two listeners react to the same OrderPlaced event, the question of which runs first usually comes up only after a bug: a confirmation email mentions a loyalty balance that has not been updated yet, or a cache is refreshed before the data it caches. Interviewers ask about ordering to see whether we know the mechanism and, more importantly, when to stop relying on it. In this answer, we will see what order we get by default, how @Order works on listener methods, where ordering stops applying, and what to do when listeners really depend on each other.

1What Order We Get Without Configuration

When an event is published, the multicaster collects the matching listeners into a list and sorts it with Spring's order comparator. Listeners without @Order or Ordered all get the lowest precedence, so they tie, and among tied listeners the order is whatever the registration order happened to be.

Registration order depends on the order in which Spring processes bean definitions, which depends on component scanning, class names, configuration imports and the Spring version. It often looks stable on one machine and then changes after an unrelated refactoring or upgrade. So the honest answer is that without configuration, the order is unspecified, and our code must work in any order.

2Controlling the Order With @Order

To fix an order, we put @Order on each listener method. Lower values have higher precedence and run first.

Java
@Component
class LoyaltyListener {
@EventListener
@Order(1)
void addPoints(OrderPlaced e) { log.info("points added for {}", e.id()); }
}
@Component
class EmailListener {
@EventListener
@Order(2)
void sendConfirmation(OrderPlaced e) { log.info("email sent for {}", e.id()); }
}
// publisher.publishEvent(new OrderPlaced(42)):
// points added for 42
// email sent for 42
Effect of @Order on OrderPlaced listeners
Listener@OrderRuns
LoyaltyListener.addPoints1first
EmailListener.sendConfirmation2second
AnalyticsListener.recordnone (lowest precedence)after the ordered ones
AuditListener.writenone (lowest precedence)after the ordered ones, order among them unspecified

For a class that implements ApplicationListener<OrderPlaced>, we implement Ordered, or annotate the class with @Order, and the same rule applies. Ordered listeners run before unordered ones, because unordered ones count as the lowest precedence. Notice that the two listeners are in different classes; @Order on a method is read by the method's adapter, so each listener method can have its own position.

3Where Ordering Stops Applying

@Order controls the sequence in which the multicaster starts listeners. For synchronous listeners on the same multicaster, starting first means finishing first, because each runs to completion on the publisher's thread before the next starts. That is the only case in which ordering gives a real guarantee.

With @Async, the multicaster still calls the listeners in order, but each call only submits a task to an executor and returns. The two tasks can run on two threads at the same time, and the email task can finish before the loyalty task. Transactional listeners have their own sequence, since they run in a later phase of the transaction, and events delivered to other contexts or services have no shared order at all.

Java
@Async @EventListener @Order(1) void addPoints(OrderPlaced e) { ... }
@Async @EventListener @Order(2) void sendConfirmation(OrderPlaced e) { ... }
// possible output:
// email sent for 42 (task-2)
// points added for 42 (task-1)

4When One Listener Depends on Another

If the email must mention the new loyalty balance, the two listeners are not independent reactions; they are two steps of one process. Encoding that dependency in @Order values is fragile: someone adds @Async to one of them, or a new listener takes order 1, and the process breaks with no error.

There are two clearer designs. A single orchestrating listener does both steps in order in one method, so the dependency is visible in the code. Or the first step publishes a new event when it is done, such as LoyaltyPointsAdded, and the email listens to that, so the email cannot run before the points exist, whatever the threading.

Java
@EventListener
LoyaltyPointsAdded addPoints(OrderPlaced e) {
int balance = loyalty.add(e.customerId(), e.points());
return new LoyaltyPointsAdded(e.id(), balance); // published as a new event
}
@EventListener
void sendConfirmation(LoyaltyPointsAdded e) {
log.info("email for {} with balance {}", e.orderId(), e.balance()); // always after the points
}

Notice that the chained version keeps working if either listener becomes asynchronous, because the dependency is carried by the data, not by the timing. I would use @Order only for genuinely independent listeners where a preferred order is nice to have, such as running a cheap validation before an expensive one.

5Follow-up questions

Does @Order on the bean class affect @EventListener methods?

No. The adapter for a listener method reads @Order from the method only, and uses the lowest precedence when the method has none. An @Order on the class does not order its @EventListener methods, so put the annotation on each method you want to order.

Can an exception in the first listener stop the second?

Yes, for synchronous listeners. The exception propagates out of the multicaster, so the remaining listeners in the list are not called. That is another hidden dependency of order: moving a failing listener earlier changes which other listeners run.

Is the order the same for @TransactionalEventListener?

Transactional listeners are also sorted by @Order, but they all run in their transaction phase, after the synchronous listeners that ran during publishEvent(). So the overall sequence mixes publication time and commit time, which is another reason not to build logic on it.

6Conclusion

In this answer, we saw that without configuration the order of listeners for the same event is unspecified and must not be relied on. @Order on listener methods, or Ordered on ApplicationListener classes, sets the order, lower values first, and ordered listeners run before unordered ones. The guarantee holds only for synchronous listeners on the same multicaster, and when one listener depends on another, a single orchestrating listener or a chained event expresses the dependency far more safely than ordering.

What to say in the interviewAbout 80 seconds aloud

Open with

By default the order is not guaranteed. The multicaster sorts matching listeners with Spring's order comparator, and listeners without an order all have the lowest precedence, so among them the sequence is just registration order, which depends on scanning and can change after a refactoring or an upgrade.

Then, if they want more

To control it, I put @Order on each listener method, or implement Ordered for an ApplicationListener class. Lower values run first, and ordered listeners run before unordered ones. But that guarantee only really holds for synchronous listeners on the same multicaster, because each one runs to completion on the publishing thread before the next starts.

If a listener is @Async, the multicaster still submits them in order, but they run on different threads and can finish in any order. Transactional listeners run later, in their transaction phase. So if one listener actually depends on another's result, for example the email needs the new loyalty balance, I would not encode that in @Order values.

I would either have one orchestrating listener that does both steps, or have the first listener publish a follow-up event like LoyaltyPointsAdded that the email listens to. That way the dependency is carried by the data and keeps working even if someone makes a listener asynchronous.

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 "Two @EventListener methods handle the same OrderPlaced event. How do you control which...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/e340690a23/