Module io.github.mmm.event
module io.github.mmm.event
Provides generic, reusable infrastructure to define, send and receive events.
This module provides a minimalistic but powerful API for the event pattern. You may define any kind of custom event like this:
Event
This module provides a minimalistic but powerful API for the event pattern. You may define any kind of custom event like this:
// you may also use a record instead of a class...
public class MyEvent {
}
To get started, here is a simple example how you can define your custom event handler interface:
@FunctionalInterface
public interface MyEventListener extends EventListener<MyEvent> {
}
Your component sending events can be defined like this:
public interface MyComponent extends EventSource<MyEvent, MyEventListener> {
void doSomething();
}
The implementation simply extends from AbstractEventSource inheriting the generic event
infrastructure (in case you already have to extend another class, use
EventSourceAdapter):
public class MyComponentImpl extends AbstractEventSource<MyEvent, MyEventListener> implements MyComponent {
public void doSomething() {
fireEvent(new MyEvent("Hello World!");
}
}
Now you already have everything you need for eventing:
MyComponent component = new MyComponentImpl();
MyEventListener listener = (e) -> System.out.println("Received event: " + e);
component.addListener(listener);
component.doSomething(); // this will echo "Received event: MyEvent@..."
// when you are done, you can unsubscribe the listener
component.removeListener(listener);
-
Packages
ExportsPackageDescriptionProvides the API and base implementation for generic eventing. -
Services
ProvidesUses