A blazing-fast event bus for Java.
Java 17 or higher
In your build.gradle:
plugins {
// Required if you want to use Listener object.
id 'io.github.stanikoc.eventsystem.injector' version '1.0.0'
}
dependencies {
implementation 'io.github.stanikoc:eventsystem-impl:1.0.0'
}Getting started takes only a few lines of code.
package com.example;
// It can be any object, but records are preferred due to a better lookup time.
public record ResultEvent(long result) {} //Create a class that extends SubscriberImpl. That class holds all the listeners.
package com.example;
import io.github.stanikoc.eventsystem.SubscriberImpl;
import io.github.stanikoc.eventsystem.Listener;
public class ResultService extends SubscriberImpl { //
public ResultService() {
// The generic type <ResultEvent> is automatically resolved at compile time
listen(new Listener<ResultEvent>() {
@Override
public void onEvent(ResultEvent event) {
System.out.println("The result is: " + event.result());
}
});
// Alternatively, one may make a normal class, not necessarily an anonymous one.
listen(new ResultListener());
// Or use a lambda method
listen(ResultEvent.class, e -> {});
}
private static final class ResultListener extends Listener<ResultEvent> {
@Override
public void onEvent(ResultEvent event) {
// Your logic here
}
}
}Instantiate your EventBus, register your subscriber, and start dispatching events.
package com.example;
import io.github.stanikoc.eventsystem.EventBus;
import io.github.stanikoc.eventsystem.EventBusImpl;
public class Main {
private static final EventBus eventBus = new EventBusImpl(); //
public static void main(String[] args) {
// Register the subscriber
eventBus.subscribe(new ResultService());
// Post an event to all active listeners!
eventBus.post(new ResultEvent(42));
}
}