An Event Bus simple implementation inspired by google and my own design (for blogging purposes)
Java 100.00%
event-bus's Introduction
Event Bus
Why?
I write a blog post from time to time and this repository is POC to the ideas explained in the blog post.
To demonstrate to my students that it's not hard at all to implement such types of ideas.
Running the project
It's a maven based project, hence you can just compile and run using maven itself (Just don't forget to install the dependencies and you'll be good to go).
Make sure you have at least a JDK 1.8 or higher installed in your machine and an IDE would be nice to have.
Take a look at the unit tests to see how to use both implementations if the EventBus. Also play with code a little, you can read the blog post for additional explanations.
Usage example
Using Common EventBus implementation
EventBuseventBus = EventBusFactory.createSingletonSyncEventBus();
Subscribablesubs = newEventSubscribable(); // your own implementation of the Subscribable interfaceeventBus.register(subs);
// later on on the code ... eventBus.dispatch(newEventStub());
Using The idea of Guava's EventBus
EventBuseventBus = newEventBus("myEventBusName")
Handlerhandler = newHandler();
eventBus.register(handler);
classEvent {
// your event's state/behaviours
}
classHandler {
@Subscribe("myEventBusName")
@Subscribe("multiEventSubscribingIsAlsoSupported")
publicvoidhandleEvent(Eventevent) {
// consume the event here
}
}
// later on in the codeeventBus.post(newEvent());
// can also unregister handlers later oneventBus.register(handler);
Contribution
Bug fixes, improvements, and more features are all welcomed ๐ (Just make sure to fully describe what you've done and write some unit tests if necessary)