Guava EventBus: subscribe without annotations?

I need to implement a pub / subsystem as part of a domain model. I was thinking about using Guava EventBus, but I would like to use interfaces and adapters so that my domain model is not aware of such implementation details. Unfortunately, using EventBus annotations for subscriptions throws a monkey clue on this idea.

Is there a way to subscribe to a handler without using the @Subscribe annotation? Looking at the docs doesn't seem to be, but maybe there is something that I don’t see.

Thank!

+3
source share
2 answers

Guava team member here.

, @Subscribe - EventBus , .., - , ? , .

+6

. - :

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
        }
    }
}

.

0

All Articles