Class SocketEventBus
- All Implemented Interfaces:
EventBus
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
DispatchMode.SEQUENTIAL– Handlers run sequentially on the calling thread.DispatchMode.PARALLEL– Handlers run concurrently using a shared thread pool.DispatchMode.PER_HANDLER– Each handler has its own single-thread executor to preserve per-handler ordering while still allowing concurrency across handlers.
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 defaultdispatchModeis 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
ConstructorsConstructorDescriptionSocketEventBus(Transport transport, DispatchMode dispatchMode) Creates a newSocketEventBususing the specified transport and bus-wide default dispatch mode. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a remote peer to the event bus.voidPublishes a message to all registered peers and dispatches it locally.voidRegisters a local handler for the specified address using the bus-wide default dispatch mode.voidregister(String address, Consumer<Object> handler, DispatchMode perHandlerMode) Registers a local handler for the specified address with an optional per-handler dispatch mode override.voidremovePeer(Peer peer) Removes a remote peer from the event bus.voidSends a point-to-point message to all registered peers and dispatches it locally.voidshutdown()Shuts down the event bus and releases resources.
-
Constructor Details
-
SocketEventBus
Creates a newSocketEventBususing 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
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
Removes a remote peer from the event bus.- Parameters:
peer- the remote peer to remove
-
register
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
Transportfor remote messages. -
register
Registers a local handler for the specified address with an optional per-handler dispatch mode override.If
perHandlerModeis non-null, it will be used to dispatch this handler locally. IfperHandlerModeis null, the bus-wide defaultdispatchModewill be used.Scope: The override applies only to local dispatch performed by this bus. Remote callback threading is determined by the underlying
Transportimplementation. -
send
Sends a point-to-point message to all registered peers and dispatches it locally.Uses
sendsemantics: only the first handler on the remote side will process the message, depending on transport semantics. -
publish
Publishes a message to all registered peers and dispatches it locally.Uses
publishsemantics: all handlers on the remote side will process the message, depending on transport semantics. -
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(). - Stops the underlying
-
