Rishi Rishi - 1 year ago 74
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.


Answer Source

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 {
  public void handleEvent(String eventId) {
    // do something

Foo foo = ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download