Class LocalEventBus

java.lang.Object
com.framed.core.local.LocalEventBus
All Implemented Interfaces:
EventBus

public class LocalEventBus extends Object implements EventBus
A local implementation of the EventBus interface for message-based communication between components within the same JVM.

This class provides asynchronous message delivery using a dedicated ExecutorService per address to ensure ordered message handling. It supports both point-to-point messaging via send(String, Object) and broadcasting via publish(String, Object).

Features:

  • Thread-safe handler registration and message dispatching.
  • Single-threaded executors per address for sequential message processing.
  • Automatic cleanup of executors when all handlers for an address are removed.
  • Constructor Details

    • LocalEventBus

      public LocalEventBus()
  • Method Details

    • register

      public void register(String address, Consumer<Object> handler)
      Registers a handler for the specified address. Creates a new single-threaded executor for the address if it does not exist.
      Specified by:
      register in interface EventBus
      Parameters:
      address - the address to listen on
      handler - the handler that processes messages for this address
    • send

      public void send(String address, Object message)
      Sends a message to a single handler registered for the given address. The first handler in the list is selected for delivery.
      Specified by:
      send in interface EventBus
      Parameters:
      address - the target address
      message - the message to send
    • publish

      public void publish(String address, Object message)
      Publishes a message to all handlers registered for the given address.
      Specified by:
      publish in interface EventBus
      Parameters:
      address - the target address
      message - the message to broadcast
    • shutdown

      public void shutdown()
      Stops all executors that were added to the executors map.
      Specified by:
      shutdown in interface EventBus