Reputation: 9251
I need to implement a pub/sub system within a domain model. I was thinking about using Guava's EventBus, but I'd like to use interfaces and adapters to keep my domain model ignorant of such an implementation detail. Unfortunately, EventBus's use of annotations for subscription throw a monkey wrench at this idea.
Is there any way to subscribe a handler without using the @Subscribe annotation? Looking at the docs, there doesn't seem to be but perhaps there's something I'm not seeing.
Thanks!
Upvotes: 4
Views: 1483
Reputation: 351
A workaround is to adapt the Handler. Something like:
class GuavaHandler<T extends Message> implements Handler<T> {
private Handler<T> handler;
public GuavaHandler(Handler<T> handler) {
this.handler = handler;
}
@Override
@Subscribe
public void handle(T message) {
try {
handler.getClass().getMethod("handle", message.getClass());
handler.handle(message);
} catch (NoSuchMethodException ignored) {
// workaround
}
}
}
You only define the attribute in this specific implementation.
Upvotes: 1
Reputation: 198591
Guava team member here.
It's quite deliberate that you can only subscribe a handler with the @Subscribe
annotation -- EventBus
is intended to replace interfaces, adapters, etc., not to supplement them -- but why do you say that exposes more implementation details? In our experience, it usually exposes fewer details.
Upvotes: 7