Skip to content

Repository files navigation

Simple event publishing system for Java

What is it?

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

What can I do with it?

Publish events

// 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());

Create listeners

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();
}
}

Create events

// Very basic data objectclassMyEvent {
privateStringaction;
privatebooleanstatus;
publicStringgetAction() {
returnaction;
}
publicvoidsetAction(Stringaction) {
this.action = action;
}
publicbooleangetStatus() {
returnstatus;
}
publicvoidsetStatus(booleanstatus) {
this.status = status;
}
}

Publish with your own Executor

// Create a new EventPublisher that uses a fixed-size thread poolEventPublisherpublisher = newEventPublisher(Executors.newFixedThreadPool(4));

How do I get it?

Gradle package from GitHub Packages

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.

Maven Central package

Coming soon

Notes

  • Dead-simple EventPublisher
  • 100% test coverage
  • 100% Javadoc coverage

About

Simple event publishing system for Java

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages