Skip to content

coditory/quark-eventbus

Repository files navigation

Quark EventBus

Build Coverage Maven Central

Super simple, lightweight, single purpose, in-memory EventBus java library. Similar to eventBuses provided by Spring Framework or Guava 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
  • integrates with event bus

This EventBus is threadsafe and synchronous. It deliberately provides no asynchronous api to keep it super simple.

Installation

Add to your build.gradle:

dependencies {
    implementation "com.coditory.quark:quark-eventbus:0.0.6"
}

Usage

EventHandler subscription

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

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

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

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

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •