Class SocketEventBus

java.lang.Object
com.framed.core.remote.SocketEventBus
All Implemented Interfaces:
EventBus

public class SocketEventBus extends Object implements EventBus
A distributed EventBus implementation backed by a Transport for remote communication.

This event bus supports local dispatch and remote forwarding (send/publish) across a dynamic set of peers. Local listeners can be executed using a default DispatchMode configured at bus construction time.

Supported Local Dispatch Modes

Per-Handler Dispatch Override

In addition to the bus-wide default dispatch mode, this implementation allows specifying an optional per-handler dispatch mode at registration time:

  • If a per-handler mode is provided (non-null), it is used for local dispatch of that handler.
  • If it is null, the bus-wide default dispatchMode is used.

Important: The per-handler dispatch override affects only local dispatch performed by dispatchLocally(String, Object). Remote dispatch is delegated to the underlying Transport, which may invoke handlers on its own threads.

Lifecycle

Call shutdown() to stop the transport and terminate all executors created/used by this bus.

  • Constructor Summary

    Constructors
    Constructor
    Description
    SocketEventBus(Transport transport, DispatchMode dispatchMode)
    Creates a new SocketEventBus using the specified transport and bus-wide default dispatch mode.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPeer(Peer peer)
    Adds a remote peer to the event bus.
    void
    publish(String address, Object message)
    Publishes a message to all registered peers and dispatches it locally.
    void
    register(String address, Consumer<Object> handler)
    Registers a local handler for the specified address using the bus-wide default dispatch mode.
    void
    register(String address, Consumer<Object> handler, DispatchMode perHandlerMode)
    Registers a local handler for the specified address with an optional per-handler dispatch mode override.
    void
    Removes a remote peer from the event bus.
    void
    send(String address, Object message)
    Sends a point-to-point message to all registered peers and dispatches it locally.
    void
    Shuts down the event bus and releases resources.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SocketEventBus

      public SocketEventBus(Transport transport, DispatchMode dispatchMode)
      Creates a new SocketEventBus using the specified transport and bus-wide default dispatch mode.
      Parameters:
      transport - the transport implementation (e.g., TCPTransport or UDPTransport)
      dispatchMode - default local dispatch mode used when a handler does not specify an override
  • Method Details

    • addPeer

      public void addPeer(Peer peer)
      Adds a remote peer to the event bus.

      Messages sent or published will also be forwarded to this peer.

      Parameters:
      peer - the remote peer to add
    • removePeer

      public void removePeer(Peer peer)
      Removes a remote peer from the event bus.
      Parameters:
      peer - the remote peer to remove
    • register

      public void register(String address, Consumer<Object> handler)
      Registers a local handler for the specified address using the bus-wide default dispatch mode.

      This method is backwards compatible and behaves exactly like the original implementation. The handler will also be registered with the underlying Transport for remote messages.

      Specified by:
      register in interface EventBus
      Parameters:
      address - the logical address/topic to listen on
      handler - the handler to process incoming payloads
    • register

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

      If perHandlerMode is non-null, it will be used to dispatch this handler locally. If perHandlerMode is null, the bus-wide default dispatchMode will be used.

      Scope: The override applies only to local dispatch performed by this bus. Remote callback threading is determined by the underlying Transport implementation.

      Specified by:
      register in interface EventBus
      Parameters:
      address - the logical address/topic to listen on
      handler - the handler to process incoming payloads
      perHandlerMode - optional per-handler local dispatch mode; null means "use bus default"
    • send

      public void send(String address, Object message)
      Sends a point-to-point message to all registered peers and dispatches it locally.

      Uses send semantics: only the first handler on the remote side will process the message, depending on transport semantics.

      Specified by:
      send in interface EventBus
      Parameters:
      address - the logical address/topic
      message - the payload to send
    • publish

      public void publish(String address, Object message)
      Publishes a message to all registered peers and dispatches it locally.

      Uses publish semantics: all handlers on the remote side will process the message, depending on transport semantics.

      Specified by:
      publish in interface EventBus
      Parameters:
      address - the logical address/topic
      message - the payload to publish
    • shutdown

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

      This method:

      • Stops the underlying Transport
      • Clears local handler and peer state
      • Terminates the shared thread pool
      • Terminates all per-handler executors

      Note: This forcefully interrupts running tasks via shutdownNow().

      Specified by:
      shutdown in interface EventBus