Mediator Pattern Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Mediator Pattern runs in O(n) time and O(n) space, in Java as in every other language.
- ✓The JDK has no general event bus; the two dominant options are Guava EventBus, where subscribers are annotated methods discovered by reflection, and Spring ApplicationEventPublisher with EventListener annotations.
- ✓Prefer Spring events inside a Spring application and Guava EventBus only where there is no container, since a second event mechanism in the same process confuses ordering and transaction semantics.
- ✓The Java trap: Two silent behaviours catch people. Guava EventBus catches every exception a subscriber throws and routes it to a SubscriberExceptionHandler whose default merely logs, so a broken subscriber never fails the publisher and a bug can run unnoticed for months.
- ✓Guava EventBus dispatches through reflection over annotated methods, with a cached lookup per event type, so it is considerably slower than a direct call but fine at request rates; the per-event allocation of the subscriber iterator is usually more significant than the reflection.
What Mediator Pattern actually is
Mediator Pattern in Java: what you actually reach for
Java implementation
import com.google.common.eventbus.*;
import org.springframework.context.event.EventListener;
import org.springframework.transaction.event.TransactionalEventListener;
record UserSaved(String id) {}
class Subscribers {
@Subscribe
void onSaved(UserSaved e) { throw new IllegalStateException("boom"); }
}
class Wiring {
static EventBus bus() {
// WITHOUT this handler, a throwing subscriber is silently logged and ignored.
return new EventBus((ex, ctx) -> {
throw new RuntimeException("subscriber failed: " + ctx.getSubscriberMethod(), ex);
});
}
}
class SpringListeners {
// SYNCHRONOUS and inside the publisher's transaction: a throw ROLLS IT BACK.
@EventListener
void onSaved(UserSaved e) { }
// Runs only after the transaction commits, usually what people actually meant.
@TransactionalEventListener
void afterCommit(UserSaved e) { }
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Mediator Pattern?
The JDK has no general event bus; the two dominant options are Guava EventBus, where subscribers are annotated methods discovered by reflection, and Spring ApplicationEventPublisher with EventListener annotations. Within the JDK, java.beans.PropertyChangeSupport is a small mediator for property notifications, and Flow, the Java 9 reactive-streams interface, covers the back-pressure case that a naive bus does not.
What is the most common Java mistake when implementing Mediator Pattern?
Two silent behaviours catch people. Guava EventBus catches every exception a subscriber throws and routes it to a SubscriberExceptionHandler whose default merely logs, so a broken subscriber never fails the publisher and a bug can run unnoticed for months. And a Spring EventListener is SYNCHRONOUS by default: it executes on the publishing thread inside the publisher transaction, so a slow listener slows the request and a listener that throws rolls the transaction back, which is the opposite of the fire-and-forget behaviour most people assume. Adding the Async annotation changes it, and then the listener no longer shares the transaction.
How does Mediator Pattern actually perform in Java?
Guava EventBus dispatches through reflection over annotated methods, with a cached lookup per event type, so it is considerably slower than a direct call but fine at request rates; the per-event allocation of the subscriber iterator is usually more significant than the reflection. Spring events dispatch through a direct call and are cheap. Any synchronous bus makes total latency the sum of every listener, which is the argument for moving expensive work behind an async executor with its own bounded queue. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Devin, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.