https://github.com/philippheuer/events4j
Java Event Dispatcher / Consumer
https://github.com/philippheuer/events4j
events hacktoberfest java reactor
Last synced: 12 months ago
JSON representation
Java Event Dispatcher / Consumer
- Host: GitHub
- URL: https://github.com/philippheuer/events4j
- Owner: PhilippHeuer
- License: mit
- Created: 2018-05-12T13:56:51.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2025-07-05T13:46:51.000Z (12 months ago)
- Last Synced: 2025-07-09T12:06:54.640Z (12 months ago)
- Topics: events, hacktoberfest, java, reactor
- Language: Java
- Homepage:
- Size: 732 KB
- Stars: 26
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# *Events4J*
[](https://search.maven.org/search?q=com.github.philippheuer.events4j)
[](https://securityscorecards.dev/viewer/?uri=github.com/philippheuer/events4j)
[](https://sonarcloud.io/summary/overall?id=github-com-133151090)
[](https://sonarcloud.io/summary/overall?id=github-com-133151090)
[](https://sonarcloud.io/summary/overall?id=github-com-133151090)
[](https://sonarcloud.io/component_measures?metric=Coverage&view=list&id=github-com-133151090)
Click the module name in the table below for specific import instructions. (gradle, maven, ...)
| Module | Javadoc | Description |
|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------|
| [](https://search.maven.org/artifact/com.github.philippheuer.events4j/events4j-api) | [](https://javadoc.io/doc/com.github.philippheuer.events4j/events4j-api) | contains all interfaces |
| [](https://search.maven.org/artifact/com.github.philippheuer.events4j/events4j-core) | [](https://javadoc.io/doc/com.github.philippheuer.events4j/events4j-core) | contains the EventManager implementation |
| [](https://search.maven.org/artifact/com.github.philippheuer.events4j/events4j-kotlin) | | improvements for usage in kotlin projects |
| [](https://search.maven.org/artifact/com.github.philippheuer.events4j/events4j-handler-simple) | [](https://javadoc.io/doc/com.github.philippheuer.events4j/events4j-handler-simple) | a simple synchronous event handler |
| [](https://search.maven.org/artifact/com.github.philippheuer.events4j/events4j-handler-reactor) | [](https://javadoc.io/doc/com.github.philippheuer.events4j/events4j-handler-reactor) | a event dispatcher using project reactor |
| [](https://search.maven.org/artifact/com.github.philippheuer.events4j/events4j-handler-spring) | [](https://javadoc.io/doc/com.github.philippheuer.events4j/events4j-handler-spring) | forward events to spring ApplicationEventPublisher |
# Description
**Events4J** provides the following features:
- publish events
- register consumers / listeners for events
- comes with a few prebuilt handlers that you can use out of the box
- provide your custom implementation to process events however you want
# Usage
## Initialization
```java
EventManager eventManager = new EventManager(); // new instance
eventManager.autoDiscovery(); // register modules automatically
```
## Event Producer
```java
TestEvent testEvent = new TestEvent();
eventManager.publish(testEvent);
```
## Event Consumer
#### Subscriber-based
```java
IEventManager eventManager = new EventManager();
ReactorEventHandler reactorEventHandler = new ReactorEventHandler(eventManager);
eventManager.registerEventHandler(reactorEventHandler);
```
The Consumer
```java
reactorEventHandler.onEvent(TestEvent.class).subscribe(event -> {
log.info("TestEvent received");
});
```
#### Simple
If you want to use annotation-based events, you need to enable this feature. Annotation-based event consumers are disabled by default.
```java
IEventManager eventManager = new EventManager();
SimpleEventHandler simpleEventHandler = new SimpleEventHandler();
eventManager.registerEventHandler(simpleEventHandler);
```
*The Consumer*
```java
public class TestEventListener {
@EventSubscriber
public void onTestEvent(TestEvent testEvent) {
System.out.println("TestEvent received");
}
}
```
*Register the Consumer*
```java
eventManager.registerListener(new TestEventListener());
```
#### Spring Events
Configure the following in your `application.properties` to enable spring application events:
```yaml
events4j.handler.spring.enabled: true
```
*The Consumer*
```java
@EventListener
public void handleContextStart(TestEvent testEvent) {
System.out.println("TestEvent received");
}
```
## Kotlin
The kotlin module allows the usage of [flows](https://kotlinlang.org/docs/flow.html) to consume events.
```kotlin
eventManager.flowOn()
.collect { testEvent ->
println("TestEvent received")
}
```
## License
Released under the [MIT License](./LICENSE).