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!
View all comments
Show parent comment
2
u/SmushyTaco Dec 09 '25 edited Dec 09 '25
So for this event library, if one handler fails, the failure is routed to any matching
@ExceptionHandlermethods, after these exception handlers run, the event bus resumes with processing the other handlers.If any exception handlers throw, the throw isn’t passed to other exception handlers. Instead, it’ll lead to normal Java exception behavior. If there are no exception handlers, the default behavior is to log and swallow Exceptions and to rethrow non exception throwables (like Error).
The library intentionally does not retry events automatically, because retries require idempotence guarantees. If you want retries, you can implement them inside your
@ExceptionHandleror handler logic.If you want the entire event bus to retry all handlers in the case of any throw, you can:
@ExceptionHandlerthat grabs Throwable (no need to grab the event) and simply rethrow it.postcall in a try catch and loop.postcall completes without any throws.