micartey / jation

Advanced high performance Java event system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jation


Introduction | Maven/Gradle | Getting started

πŸ“š Introduction

jation is a java reflection based event manager which uses the build in java reflection api to automatically invoke methods with parameters and eases the work of a developer by reducing the amount of possible mistakes or missing method invocations.

An event manager is a must-have in big or event based applications. This project aims to optimize the event manager I previously build and which was used to power my anticheat.

🎈 Getting Started

Create a new Observer

JationObserver observer = new JationObserver(); // Alternatively you can pass a custom executor to the constructor

Subscribe classes

To subscribe classes you need to call the subscribe method and pass the object instances to the varargs parameter.

observer.subscribe(
   new TestClass(),
   new OtherTestClass()
);

Reflection pattern

To observe methods you need to annotate them with @Observe.
The @Async annotation is optional and can invoke the method in the ForkJoinPool asynchronously.

@Async
@Observe
public void onEvent(MyTestEvent event, @Null String additive) {

}

Events can be published with additional parameters. In case your method uses them and there is a possibility, that the parameter is not always defined, you need to annotate the parameter with @Null.

Consumer Pattern

Reflection is slow. Some say it is more than 100% slower than normal method invocations. This is the result of the runtime being unable to perform any optimizations.

This will be fixed in later versions by utilizing ASM to generate invocation code at runtime to allow the JVM to optimize the calls

Therefore, it is recommended to use the consumer pattern for performance critical projects.

observer.on(TestEvent.class, (event) -> {
    // Access the event
});

The consumer pattern has a major disadvantage apart from scalability: Additional parameters can't be passed on.

Create Events

To create an event you need to implement the JationEvent interface. The interface has a generic type parameter which is the event itself.

public class TestEvent implements JationEvent<TestEvent> {

}

Publish Events

To publish an event you need to call the publish method and pass the event instance to the first parameter and the additional parameters to the varargs parameter.

// Publish the event to all subscribed classes
new TestEvent().publish(observer, "additional information", 5);


// Publish the event to all subscribed classes asynchronously
new TestEvent().publishAsync(observer);

About

Advanced high performance Java event system

License:MIT License


Languages

Language:Java 100.0%