Click the module name in the table below for specific import instructions. (gradle, maven, ...)
Events4J provides the following features:
- publish events
- register consumers / listeners for events
- comes with a few prebuilt handlers that you can use out of the box
- provide your custom implementation to process events however you want
EventManager eventManager = new EventManager(); // new instance
eventManager.autoDiscovery(); // register modules automatically
TestEvent testEvent = new TestEvent();
eventManager.publish(testEvent);
IEventManager eventManager = new EventManager();
ReactorEventHandler reactorEventHandler = new ReactorEventHandler(eventManager);
eventManager.registerEventHandler(reactorEventHandler);
The Consumer
reactorEventHandler.onEvent(TestEvent.class).subscribe(event -> {
log.info("TestEvent received");
});
If you want to use annotation-based events, you need to enable this feature. Annotation-based event consumers are disabled by default.
IEventManager eventManager = new EventManager();
SimpleEventHandler simpleEventHandler = new SimpleEventHandler();
eventManager.registerEventHandler(simpleEventHandler);
The Consumer
public class TestEventListener {
@EventSubscriber
public void onTestEvent(TestEvent testEvent) {
System.out.println("TestEvent received");
}
}
Register the Consumer
eventManager.registerListener(new TestEventListener());
Configure the following in your application.properties
to enable spring application events:
events4j.handler.spring.enabled: true
The Consumer
@EventListener
public void handleContextStart(TestEvent testEvent) {
System.out.println("TestEvent received");
}
The kotlin module allows the usage of flows to consume events.
eventManager.flowOn<TestEvent>()
.collect { testEvent ->
println("TestEvent received")
}
Released under the MIT License.