An event handling library for Java utilizing annotations.
Go to file
2020-09-08 08:43:52 +02:00
src Add simple unit test and fix event handler ordering 2020-09-08 08:43:51 +02:00
.classpath Remove resource folders, add JUnit 5 dependency 2020-09-08 08:43:50 +02:00
.gitignore Add Maven / Eclipse project 2020-09-08 08:43:49 +02:00
.project Add Maven / Eclipse project 2020-09-08 08:43:49 +02:00
LICENSE Add Maven / Eclipse project 2020-09-08 08:43:49 +02:00
pom.xml Enable Javadoc and source generation in POM 2020-09-08 08:43:52 +02:00
README.md Add installation instructions 2020-09-08 08:43:52 +02:00

Event Bus

Introduction

This library allows passing events between different objects without them having a direct reference to each other. Any class can be made an event by implementing the IEvent interface.

Using an instance of the EventBus class, an instant of the event class can be dispatched. This means that it will be forwarded to all listeners registered for it at the event bus.

To listen to events, register event handling methods using the Event annotation. For this to work, the method must have a return type of void and declare a single parameter of the desired event type. Additionally, the class containing the method must implement the EventListener interface.

A Simple Example

Lets look at a simple example: we declare the empty class SimpleEvent that implements IEvent and can thus be used as an event.

import dev.kske.eventbus.IEvent;

public class SimpleEvent implements IEvent {}

Next, an event listener for the SimpleEvent is declared:

import dev.kske.eventbus.*;

public class SimpleEventListener implements EventListener {

    public SimpleEventListener() {
        EventBus eventBus = new EventBus();

        // Register this listener at the event bus
        eventBus.register(this);

        // Dispatch a SimpleEvent
        eventBus.dispatch(new SimpleEvent());
    }

    @Event
    private void onSimpleEvent(SimpleEvent event) {
        System.out.println("SimpleEvent received!");
    }
}

In this case, an event bus is created and used locally. In a more sophisticated example the class would acquire an external event bus that is used by multiple classes.

Installation

Event Bus is currently hosted at kske.dev. To include it inside your project, just add the Maven repository and the dependency to your pom.xml:

<repositories>
	<repository>
		<id>kske-repo</id>
		<url>https://kske.dev/maven-repo</url>
	</repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>dev.kske</groupId>
        <artifactId>event-bus</artifactId>
        <version>0.0.1</version>
    </dependency>
</dependencies>