fastbus is a blazing fast java event bus for projects that need simple typed events without a framework.
it targets java 1.8 for compatibility.
<repositories>
<repository>
<id>tsukure-releases</id>
<url>https://maven.tsuku.re/releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>re.tsuku</groupId>
<artifactId>fastbus</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>for snapshots, use:
<repository>
<id>tsukure-snapshots</id>
<url>https://maven.tsuku.re/snapshots</url>
</repository>create an event:
import re.tsuku.fastbus.Event;
public final class LoginEvent implements Event {
private final String username;
public LoginEvent(String username) {
this.username = username;
}
public String username() {
return username;
}
}subscribe with annotated methods:
import re.tsuku.fastbus.FastBus;
import re.tsuku.fastbus.Subscribe;
public final class LoginLogger {
@Subscribe
public void onLogin(LoginEvent event) {
System.out.println(event.username() + " logged in");
}
}
FastBus bus = new FastBus();
LoginLogger logger = new LoginLogger();
bus.subscribe(logger);
bus.post(new LoginEvent("rin"));
bus.unsubscribe(logger);subscribe with listener functions:
import re.tsuku.fastbus.EventPriority;
import re.tsuku.fastbus.FastBus;
import re.tsuku.fastbus.Subscription;
FastBus bus = new FastBus();
Subscription subscription = bus.subscribe(
LoginEvent.class,
event -> System.out.println(event.username()),
EventPriority.HIGH
);
bus.post(new LoginEvent("rin"));
subscription.unsubscribe();cancelable events can extend CancellableEvent:
import re.tsuku.fastbus.CancellableEvent;
public final class MessageEvent extends CancellableEvent {
private final String message;
public MessageEvent(String message) {
this.message = message;
}
public String message() {
return message;
}
}./mvnw clean testformat sources:
./mvnw formatter:formatcheck formatting:
./mvnw formatter:validatebuild the jmh benchmark jar:
./mvnw -Pbenchmarks clean package -DskipTestsrun the benchmark suite:
java -jar target/benchmarks.jarfor a shorter local run:
java -jar target/benchmarks.jar 'post_.*|subscribe_.*' -wi 2 -i 3 -f 1rough local jmh numbers, averaged from short runs on jdk 21:
| benchmark | throughput |
|---|---|
| fastbus direct post | ~270m ops/s |
| fastbus annotated post | ~208m ops/s |
| greenrobot eventbus post | ~16m ops/s |
| guava eventbus post | ~10m ops/s |
| fastbus direct subscribe/unsubscribe | ~8.7m ops/s |
| guava eventbus subscribe/unsubscribe | ~5.5m ops/s |
| fastbus annotated subscribe/unsubscribe | ~5.3m ops/s |
| greenrobot eventbus subscribe/unsubscribe | ~200k ops/s |
your mileage may vary a bit depending on jvm, hardware, and listener shape.
mit