Did this only for experimental reasons. Contributions are welcome if you want to add or optimise something.
Step 1:
Create new event bus instance, so you can actually use all of its capabilities.
Example of how to create the instance:
import net.redinginer.eventbus.EventBus;
EventBus eventBus = EventBus.create();
// subscribe, publish or unsubscribe eventsStep 2:
Create your first event. Events must inherit the AbstractEvent class, as the bus highly depends on it.
Currently, the abstraction provides only one property - cancelled. But it should be enough for most apps.
Example on how to create the event:
import net.redinginer.eventbus.AbstractEvent;
public final class SimpleEvent extends AbstractEvent {
// here, you define the properties of the event,
// for example:
private final String string;
public SimpleEvent(String string) {
this.string = string;
}
public String getString() {
return this.string;
}
}Step 3 (and last):
Create your first listener, that can listen to events. Listeners are defined by @Subscribe annotation, and any method w/o it is ignored.
Methods also have strict checks, for example, they can't return anything, cannot have more than 1 parameter, and the parameter type must be assignable from AbstractEvent.
Example on how to listen for the events, publish them, and unsubscribe after:
// here goes your listener class
import net.redinginer.eventbus.EventPriority;
import net.redinginer.eventbus.Subscribe;
public class SimpleListener {
@Subscribe(priority = EventPriority.HIGHEST)
public void handleFirst(SimpleEvent event) {
System.out.println("This event will be called, as nothing else cancels it.");
System.out.println("Here goes the string:");
System.out.println(event.getString());
System.out.println("And now, we are cancelling the event.");
event.setCancelled(true);
}
@Subscribe(priority = EventPriority.HIGH, ignoreCancelled = true)
public void handleSecond(SimpleEvent event) {
System.out.println("This method won't be called when we cancelled the event.");
System.out.println("But here goes the string:");
System.out.println(event.getString());
}
}
// here goes your main class
import net.redinginer.eventbus.EventBus;
EventBus eventBus = ... // the event bus instance you acquired
SimpleListener listener = new SimpleListener();
eventBus.subscribe(listener); // this subscribes the listener
eventBus.publish(new SimpleEvent("Test!")); // this publishes the event
eventBus.unsubscribe(listener); // this unsubscribes the listener (must be the same object ref!)| Benchmark | Mode | Cnt | Score | Error | Units |
|---|---|---|---|---|---|
| PostBenchmark.benchmarkPublish (listenerCount = 10) | avgt | 20 | 176.818 | ± 1.201 | ns/op |
| PostBenchmark.benchmarkPublish (listenerCount = 100) | avgt | 20 | 4525.797 | ± 305.273 | ns/op |
| PostBenchmark.benchmarkPublish (listenerCount = 1000) | avgt | 20 | 598266.246 | ± 14102.518 | ns/op |
| SubscribeBenchmark | avgt | 20 | 112324.134 | ± 8671.304 | ns/op |
| SubscribeUnsubscribeBenchmark | avgt | 20 | 115887.395 | ± 9880.149 | ns/op |
Project was built using Java 21 (specifically Adoptium OpenJDK 21), and Gradle 9.0.0, with the help of JMH 1.3.7.
For any documentation, just look into the code, and you should figure it out pretty easily.
Also, I know that there is a room of improvement (for example, in publishing, because we need to traverse the linked list now),
But I wanted to test the idea of using linked-lists, as an experiment on how fast we could go.