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 implementation supports configurable local dispatch semantics via DispatchMode and allows an optional per-handler override of the dispatch mode at registration time.

Dispatch modes

Per-handler override

Handlers can optionally specify their own mode using register(String, Consumer, DispatchMode). If the mode is null, the bus-wide default mode is used.

Note: This differs from the original "one executor per address" model. Ordering is now defined by the chosen dispatch mode (e.g., PER_HANDLER orders per handler, not per address).

  • Constructor Details

    • LocalEventBus

      public LocalEventBus(DispatchMode defaultDispatchMode)
      Creates a LocalEventBus using the given default dispatch mode.
      Parameters:
      defaultDispatchMode - bus-wide default mode used if a handler does not specify an override
    • LocalEventBus

      public LocalEventBus()
      Creates a LocalEventBus with DispatchMode.PER_HANDLER as default.
  • Method Details

    • register

      public void register(String address, Consumer<Object> handler)
      Registers a handler for the specified address using the bus default dispatch mode.
      Specified by:
      register in interface EventBus
      Parameters:
      address - the address/topic to listen on
      handler - the handler processing messages for this address
    • register

      public void register(String address, Consumer<Object> handler, DispatchMode perHandlerMode)
      Registers a handler for the specified address with an optional per-handler dispatch mode override.

      If perHandlerMode is non-null, it will be used for local dispatch of this handler.

      If perHandlerMode is null, the bus-wide default mode is used.

      Specified by:
      register in interface EventBus
      Parameters:
      address - the address/topic to listen on
      handler - the handler processing messages for this address
      perHandlerMode - optional override; null means "use bus default"
    • 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 (point-to-point semantics).
      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 (broadcast semantics).
      Specified by:
      publish in interface EventBus
      Parameters:
      address - the target address
      message - the message to broadcast
    • shutdown

      public void shutdown()
      Shuts down the event bus and releases resources.

      Stops the shared parallel pool and all per-handler executors.

      Specified by:
      shutdown in interface EventBus