Skip to content

Instantly share code, notes, and snippets.

@jdewind
Created March 19, 2012 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdewind/2119886 to your computer and use it in GitHub Desktop.
Save jdewind/2119886 to your computer and use it in GitHub Desktop.
Session Scoped EventBus
// Module
- (void)configure {
bind(EventBus.class).toProvider(new Provider<EventBus>() {
AtomicInteger counter = new AtomicInteger();
public EventBus get() {
return new EventBus("Event Bus Session #" + counter.incrementAndGet());
}
}).in(SessionScoped.class);
requestInjection(InjectorHolder.class);
}
public class InjectorHolder {
@Inject private static Injector injector;
public static Injector getInjector() {
return injector;
}
}
public class EventBusTypeListener implements TypeListener {
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
encounter.register(new InjectionListener<I>() {
public void afterInjection(I injectee) {
InjectorHolder.getInjector().getInstance(EventBus.class).register(injectee);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment