-
Notifications
You must be signed in to change notification settings - Fork 6
eventbus
James Moger edited this page Nov 20, 2015
·
1 revision
Fathom-Eventbus provides an injectable Guava Eventbus singleton instance for decoupled event passing.
Add the Fathom-Eventbus artifact.
<dependency>
<groupId>com.gitblit.fathom</groupId>
<artifactId>fathom-eventbus</artifactId>
<version>${fathom.version}</version>
</dependency>
None.
Bind your publisher & subscriber objects.
public class Components extends ComponentsModule {
@Override
protected void init() {
bind(MyDao.class);
bind(MyEventsSubscriber.class);
}
}
In this example we inject the Eventbus into our subscriber object so that it can register itself.
@Singleton
public class MyEventsSubscriber {
private final static Logger log = LoggerFactory.getLogger(MyEventsSubscriber.class);
@Inject
public MyEventsSubscriber(EventBus eventBus) {
eventBus.register(this);
}
// the @Subscribe annotation directs the eventbus
// to register this instance as a listener of ChangeEvent
@Subscribe
public void handleCustomerChange(ChangeEvent changeEvent) {
log.info("A customer was changed");
}
}
Your publisher object requires the Eventbus instance so that it can publish messages.
@Singleton
public class MyDao {
EventBus eventBus;
@Inject
public MyDao(EventBus eventBus) {
this.eventBus = eventBus;
}
public void changeCustomer() {
// post a message of type ChangeEvent
ChangeEvent event = getChangeEvent();
eventBus.post(event);
}
}