Skip to content

Instantly share code, notes, and snippets.

@jdewind
Created January 13, 2012 14:59
Show Gist options
  • Save jdewind/1606799 to your computer and use it in GitHub Desktop.
Save jdewind/1606799 to your computer and use it in GitHub Desktop.
EventBus on GuiceRoids
public class ApplicationModule extends AbstractModule {
private final EventBus eventBus = new EventBus("Default EventBus");
@Override
protected void configure() {
bind(EventBus.class).toInstance(eventBus);
bindListener(Matchers.any(), new TypeListener() {
public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
typeEncounter.register(new InjectionListener<I>() {
public void afterInjection(I i) {
eventBus.register(i);
}
});
}
});
}
}
public class ApplicationEventListener {
private Application application;
@Subscribe
public void applicationEvent(ApplicationEvent event) {
// handle event
}
}
public class Application {
private EventBus eventBus;
@Inject
public Application(EventBus eventBus) {
this.eventBus = eventBus;
}
public void postEvent() {
eventBus.post(new ApplicationEvent(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment