Guava EventBus integration

Example of guicey-eventbus extension usage.

Example source code

The eventbus extension is used for:

  • automatic listeners registration
  • binding eventbus instance in guice context (for publication)
  • printing available listeners to console

Configuration

An additional dependency is required:

implementation 'ru.vyarus.guicey:guicey-eventbus:5.0.1-1'

Note

guicey-eventbus version could be managed with BOM

Register eventbus bundle:

GuiceBundle.builder()
      .bundles(new EventBusBundle())

Event

Events are simple POJOs. Create event classes with properties you need (or without everything):

public class FooEvent {
    private String something;

    public FooEvent(String something) {
        this.something = something;
    }

    public void getSomething() {
        return something;
    }
}

Use event hierarchies, if appropriate:

public abstract class BaseEvent {}

public class FooEvent extends BaseEvent {}

public class BarEvent extends BaseEvent {}

For simplicity, properties are omitted.

Publication

Inject the eventbus instance to enable publication:

@Inject EventBus eventbus;

public void someAction() {
    ...
    eventbus.post(new FooEvent());
}

Listening

Listener methods must be annotated with @Subscribe and contain only one parameter of the target event type:

@Subscribe
public void onFooEvent(FooEvent event) {}

@Subscribe
// listen for all events of type (FooEvent, BarEvent)
public void onMultipleEvents(BaseEvent event) {}

Attention

Listener methods will only be registered for "known" guice beans. That means any extension or manually declared guice bean (using module) or bean created with guice AOT (because it's declared as dependency for other bean) will be searched for listener methods.

See a complete example