https://github.com/coditory/quark-eventbus
https://github.com/coditory/quark-eventbus
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/coditory/quark-eventbus
- Owner: coditory
- License: mit
- Created: 2022-10-31T14:50:05.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-22T09:20:28.000Z (almost 2 years ago)
- Last Synced: 2024-05-01T09:39:48.158Z (almost 2 years ago)
- Language: Java
- Size: 127 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Quark EventBus
[](https://github.com/coditory/quark-eventbus/actions/workflows/build.yml)
[](https://codecov.io/gh/coditory/quark-eventbus)
[](https://mvnrepository.com/artifact/com.coditory.quark/quark-eventbus)
> Super simple, lightweight, single purpose, in-memory EventBus java library. Similar to eventBuses provided by [Spring Framework](https://docs.spring.io/spring-framework/docs/5.3.9/javadoc-api/org/springframework/context/ApplicationEvent.html)
or [Guava](https://github.com/google/guava/wiki/EventBusExplained) but without the unrelated parts.
- lightweight, no dependencies
- single purpose, not part of a framework
- provides both functional and annotation based API
- public API annotated with `@NotNull` and `@Nullable` for better [kotlin integration](https://kotlinlang.org/docs/java-to-kotlin-nullability-guide.html#platform-types)
- integrates with [event bus](https://github.com/coditory/quark-context)
This EventBus is threadsafe and synchronous. It deliberately provides no asynchronous api to keep it super simple.
## Installation
Add to your `build.gradle`:
```gradle
dependencies {
implementation "com.coditory.quark:quark-eventbus:0.0.6"
}
```
## Usage
### EventHandler subscription
```java
public class Application {
public static void main(String[] args) {
EventBus eventBus = EventBus.create();
eventBus.subscribe(new TwoHandlers());
eventBus.emit("hello");
eventBus.emit(42);
}
static class TwoHandlers {
@EventHandler
void handle(String event) {
System.out.println("String event: " + event);
}
@EventHandler
void handle(Integer event) {
System.out.println("Integer event: " + event);
}
}
}
// Output:
// String event: hello
// Integer event: 42
```
### EventListener subscription
```java
public class Application {
public static void main(String[] args) {
EventBus eventBus = EventBus.create();
eventBus.subscribe(String.class, (event) -> System.out.println("String event: " + event));
eventBus.subscribe(Number.class, (event) -> System.out.println("Integer event: " + event));
eventBus.subscribe(Integer.class, (event) -> System.out.println("Integer event: " + event));
eventBus.emit("hello");
eventBus.emit(42);
}
}
// Output:
// String event: hello
// Integer event: 42
// Number event: 42
```
### Exception handling
```java
public class Application {
public static void main(String[] args) {
EventBus eventBus = EventBus.builder()
.subscribe(String.class, (event) -> { throw new RuntimeException("xxx"); })
.subscribe(String.class, (event) -> System.out.println("String event: " + event))
.setExceptionHandler(ctx -> System.out.println("Exception: " + ctx.exception()))
.build();
eventBus.emit("hello");
}
}
// Output:
// Exception: java.lang.RuntimeException: xxx
// String event: hello
```
### Handling unhandled events
```java
public class Application {
public static void main(String[] args) {
EventBus eventBus = EventBus.builder()
.subscribe(String.class, (event) -> System.out.println("String event: " + event))
.subscribe(UnhandledEvent.class, (unhandled) -> System.out.println("Unhandled event: " + unhandled.event()))
.build();
eventBus.emit(42);
}
}
// Output:
// Unhandled event: 42
```