EventBus is an open-source library for Java using the publisher/subscriber pattern for loose coupling. EventBus enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development.
- An
EventBus
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-eventbus</artifactId>
<version>1.5.0</version>
</dependency>{
use(new EventBusby()
.register(new MySubscriber())
);
post("/message", req -> {
EventBus bus = require(EventBus.class);
MessageEvent event = req.body(MessageEvent.class);
bus.post(event);
});
}EventBus module supports Guice subscribers:
{
use(new EventBusby()
.register(MySubscriber.class)
);
}Here the MySubscriber class will be provisioned by Guice.
Jooby uses the default EventBus: EventBus.getDefault(), you can provide a custom EventBus at creation time:
{
use(new EventBusby(() -> {
return EventBus.builder()
.logNoSubscriberMessages(false)
.sendNoSubscriberEvent(false)
.build();
}));
}That's all! Happy coding!!