It's a tool for using the Publish-Subscribe pattern.
You can implement as many of your own EventListener types as you need, and your listeners may @Subscribe to as many
events as you need.
When an event is published, methods that subscribe to it, or a super class, will be invoked.
You may supply your own Executor to publish events with
// Create a new EventPublisherEventPublisherpublisher = newEventPublisher();
// Register some listeners that implement EventListenerpublisher.register(newMyListenerForLogging());
publisher.register(newMyListenerThatCounts());
// Publish an event for our listeners to handlepublisher.publish(newMyEvent());classMyListenerForLoggingimplementsEventListener {
@SubscribepublicvoidlistenForMyEvent(MyEventevent) {
System.out.println("This action just happened: " + event.getAction());
}
@SubscribepublicvoiddidActionFail(MyEventevent) {
if (!event.getStatus()) {
System.out.err("Uh oh, failed to " + event.getAction());
}
}
}
classMyListenerThatCountsimplementsEventListener {
privatefinalAtomicIntegercounter = newAtomicInteger(0);
@SubscribepublicvoidlistenForMyEvent(MyEventevent) {
counter.getAndIncrement();
}
publicvoidgetCount() {
returncounter.get();
}
}// Very basic data objectclassMyEvent {
privateStringaction;
privatebooleanstatus;
publicStringgetAction() {
returnaction;
}
publicvoidsetAction(Stringaction) {
this.action = action;
}
publicbooleangetStatus() {
returnstatus;
}
publicvoidsetStatus(booleanstatus) {
this.status = status;
}
}// Create a new EventPublisher that uses a fixed-size thread poolEventPublisherpublisher = newEventPublisher(Executors.newFixedThreadPool(4));repositories {
mavenCentral()
maven {
url = uri('https://maven.pkg.github.com/ajs1998/Event')
credentials {
username = {YOURGITHUBUSERNAME}
// This is a PAT (Personal Access Token) that only has permission to read/download public GitHub Packages.// This is not the actual password for the account.
password = {YOURGITHUBPAT}
}
}
}dependencies {
implementation 'me.alexjs:event:2.0.0'
}You need to create a GitHub Personal Access Token (PAT) to be able to
download GitHub Packages.
The token only needs the read:packages permission to work.
Coming soon
- Dead-simple
EventPublisher - 100% test coverage
- 100% Javadoc coverage