Skip to content

io microsphere event GenericEventListener

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

GenericEventListener

Type: Class | Module: microsphere-java-core | Package: io.microsphere.event | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/event/GenericEventListener.java

Overview

A generic implementation of the EventListener interface that supports multiple event handling methods.

This class provides a flexible way to handle events by allowing subclasses to define additional event handler methods, beyond the mandatory EventListener#onEvent(Event) method, which is declared as final and cannot be overridden. Subclasses can define any number of custom event handler methods, provided they meet certain criteria:

  • The method must not be the same as the #onEvent(Event) method.
  • It must be declared as public.
  • It must return void.
  • It must not declare any exceptions.
  • It must accept exactly one parameter, which must be a subclass of Event or an instance of it.

Example Usage

Here's how you might extend this class to handle specific types of events:

`public class MyGenericEventListener extends GenericEventListener {

    // Custom event handler method
    public void onMyEvent(MyEvent event) {
        System.out.println("Received MyEvent: " + event);
    `

    // Another event handler for a different type of event
    public void onAnotherEvent(AnotherEvent event) {
        System.out.println("Received AnotherEvent: " + event);
    }
}
}

When an event is received via the final #onEvent(Event) method, it will automatically invoke all matching handler methods defined in the subclass. This allows for a dynamic dispatch mechanism based on the runtime type of the event.

For example, if the above listener receives a MyEvent, it will call the onMyEvent() method. Similarly, for an instance of AnotherEvent, it will call the onAnotherEvent() method.

This class also supports priority-based ordering through its implementation of the Prioritized interface, allowing instances to be sorted based on their priority values.

Declaration

public abstract class GenericEventListener implements EventListener<Event>

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

public class MyGenericEventListener extends GenericEventListener {

    // Custom event handler method
    public void onMyEvent(MyEvent event) {
        System.out.println("Received MyEvent: " + event);
    }

    // Another event handler for a different type of event
    public void onAnotherEvent(AnotherEvent event) {
        System.out.println("Received AnotherEvent: " + event);
    }
}

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.event.GenericEventListener;

API Reference

Public Methods

Method Description
onEvent

See Also

  • Event
  • EventListener
  • Prioritized

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally