Rishi Rishi - 1 month ago 5
Java Question

subscribing to an event bus

I am trying to use guava for subscribing to EventBus. Trying to look at the site doc but unable to see any example where it shows how to do it.

Anyone tried this before??

private final EventBus eventBus = new EventBus();
eventBus.post(eventId); // where eventId is a string.


This is being in one of the jars. Now I need to subscribe to this eventbus and check if there are any new eventId's posted. How can I do that?

Any help is appreciated.

Thanks!!

Answer

You would need some object with a method annotated @Subscribe that takes a parameter of type String (since you're posting a String as an event to it... note that some more specific event type is probably preferable). You then need to pass that object to the EventBus.register(Object) method. Example:

public class Foo {
  @Subscribe
  public void handleEvent(String eventId) {
    // do something
  }
}

Foo foo = ...
eventBus.register(foo);
eventBus.post(eventId);
Comments