The EventBus module was originally developed by Justin DeWind on Atomicobject blog, under the terms of the MIT License.
This variation allows configuring more than one EventBus for each Injector; let's define first an event:
public final class ApplicationEvent { }
And an event handler
public interface ApplicationEventHandler { void when( ApplicationEvent applicationEvent ); }
with related implementation
public final class DefaultApplicationEventHandler implements ApplicationEventHandler { @javax.inject.Inject private Dependency myDependency; // setters omitted for brevity @Override @com.google.common.eventbus.Subscribe public void when( ApplicationEvent applicationEvent ) { // do something you need } }
then you can create the Injector
public final class MySystem { @javax.inject.Inject @javax.inject.Named( "eventbus.test" ) private EventBus eventBus; // setters omitted for brevity public void start() { com.google.inject.Guice.createInjector( new org.apache.onami.guava.eventbus.EventBusModule() { @Override protected void configure() { bind( ApplicationEventHandler.class ).to( DefaultApplicationEventHandler.class ); bindBus( "eventbus.test" ).toAnyBoundClass(); } } ).injectMembers( this ); } public void notify() { eventBus.post( new ApplicationEvent() ); } }
That's all, have fun!