Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subscribing to an event bus

Tags:

java

guava

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

like image 293
Rishi Avatar asked Jan 18 '23 12:01

Rishi


1 Answers

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);
like image 176
ColinD Avatar answered Jan 21 '23 03:01

ColinD