edit

Guava EventBus integration

Example of guicey-eventbus extension usage.

Example source code

Eventbus extension used for:

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

Configuration

Additional dependency required:

compile 'ru.vyarus.guicey:guicey-eventbus:0.2.1'

Note

guicey-eventbus version could be managed with BOM

Register eventbus bundle:

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

Event

Events are simple POJO. 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 using events without properties.

Publication

Inject eventbus instance for publication:

@Inject EventBus eventbus;

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

Listening

Listener method must be annotated with @Subscribe and contain only one parameter (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 method will be registered only 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 complete example