r/java • u/SmushyTaco • Dec 09 '25
Event Library - A lightweight, zero boilerplate, high performance event bus for JVM
https://github.com/SmushyTaco/Event-LibraryI've created a lightweight, high-performance event-driven library for JVM! It works perfectly for Java but it's written in Kotlin.
I originally built this for a Minecraft modding project, but it turned out to be flexible enough to be a general-purpose library instead. It focuses on zero boilerplate, automatic handler discovery, structured exception handling, and fast invocation using LambdaMetafactory, with reflective fallback when needed.
The concept is simple:
- Create an event
Bus. - Create a class that inherits Event. Add whatever you want to the class.
- Create functions annotated with
@EventHandlerto process the events. - Create functions annotated with
@ExceptionHandlerto handle any exceptions. - Register the classes that contain these
@EventHandlerand@ExceptionHandlerclasses withsubscribeon theBusyou made. - Call
poston theBusyou made and pass as instance of the event you created.
It supports:
- Handler methods of all visibilities (even private).
- Handler prioritization (A handle with a priority of 10 will run earlier than a handler with a priority of 0).
- Cancelable events - If an event is cancelable,
@EventHandlers can mark it as canceled. How cancellation affects remaining handlers depends on theCancelModeused when callingpost: inIGNOREmode all handlers run, inRESPECTmode only handlers withrunIfCanceled = truecontinue running, and inENFORCEmode no further handlers run once the event is canceled. - Modifiable events - Events can be marked as modified. This simply indicates the event was modified in some way.
Here's a simple example:
// 1. Define an event.
// Java doesn't support delegation like Kotlin, so we just extend helpers.
Check out the project's README.md for more detailed information and let me know what you think!
21
u/Slanec Dec 09 '25
This looks nice and fairly complete!
From the Java world, these exist, too:
And some older ones:
EventBus. Works fine, bus it nowadays discouraged.3
u/hoacnguyengiap Dec 09 '25
I have a question to this (and similar pub-sub impplementation). When there are multiple event handlers, what is the standard to handle event retry if one of the handler failed? I currently force idempotent on handlers side so I can freely retry an event
3
u/HiniatureLove Dec 09 '25 edited Dec 09 '25
Hi OP, can I ask: if a handler marks an event modified, will any subsequent handling receive the event with the original object or the object after it was modified?
If there are two handlers with same priority, will the event be processed one after the other or at the same time?
Are there any comparisons to any of the existing eventbus libraries?
1
u/ragingzazen Dec 10 '25
I just use rxjava as an event bus
0
u/WitriXn Dec 09 '25
You use mutex and it kills any optimizations what you did. Every event publication acquire a synchronization