Today, I’ve released my another tiny project. It’s a very simple implementation of Event Bus with RxJava 2 under the hood. This library is compatible with Java 1.7 or higher. I didn’t use Java 1.8 or 1.9 because I wanted to make it compatible with Android apps.

You can use it as follows:

1
2
3
4
5
6
7
Bus bus = ReactiveBus.create();

Disposable observer = bus.receive().subscribe(new Consumer<Event>() {
@Override public void accept(Event event) {
// handle event here
}
});

Once, we created Event Bus object and our observer (or more precisely: disposable subscriber), we can start sending events:

1
bus.send(Event.name("my event").build());

We can also send some serializable data:

1
bus.send(Event.name("my another event with data").data(serializableObject).build());

All events, will be received by the consumer in subscribe(...) method.

If we want to stop propagation of the events, we can just dispose observer like any RxJava subscription:

1
observer.dispose();

Actually, most of the work is done by RxJava itself. In this project, I just put a few pieces together and played around with them in unit tests.

With such kind of Event Bus implementation, we can create fluent, functional, reactive piplines of data flow in our apps.

You can include this tiny library in your project via Maven:

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.github.pwittchen</groupId>
<artifactId>reactivebus</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.1.10</version>
</dependency>

or via Gradle:

1
2
implementation 'com.github.pwittchen:reactivebus:0.0.5'
implementation 'io.reactivex.rxjava2:rxjava:2.1.10'

For more details, visit project repository on GitHub: https://github.com/pwittchen/ReactiveBus

References