Package com.framed.core.remote
Class SocketEventBus
java.lang.Object
com.framed.core.remote.SocketEventBus
- All Implemented Interfaces:
EventBus
A distributed
EventBus implementation that uses a Transport for remote communication.
This class allows local and remote event dispatching across multiple peers connected via TCP or UDP. It supports point-to-point and publish-subscribe semantics, combining local handler execution with remote message forwarding.
Features:
- Integrates with any
Transportimplementation (e.g., TCP or UDP). - Maintains a dynamic set of remote peers for message propagation.
- Supports local handler registration and synchronous or asynchronous local dispatch.
- Graceful shutdown via
shutdown().
Usage Example:
Transport tcpTransport = new TCPTransport(8080);
SocketEventBus eventBus = new SocketEventBus(tcpTransport, DispatchMode.PER_HANDLER);
eventBus.register("sensor.data", msg -> System.out.println("Local handler received: " + msg));
eventBus.addPeer(new Peer("remote-host", 8081));
eventBus.publish("sensor.data", "Temperature: 22°C");
// Later:
eventBus.shutdown();
Threading Model:
DispatchMode.SEQUENTIAL– All 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 for ordered execution.
Transport implementation.
Note: Ensure shutdown() is called to release resources and stop the transport.-
Constructor Summary
ConstructorsConstructorDescriptionSocketEventBus(Transport transport, DispatchMode dispatchMode) Creates a newSocketEventBususing the specified transport and dispatch mode. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a remote peer to the event bus.voidPublishes a message to all registered peers and dispatches locally.voidRegisters a local handler for the specified address.voidremovePeer(Peer peer) Removes a remote peer from the event bus.voidSends a point-to-point message to all registered peers and dispatches locally.voidshutdown()Shuts down the event bus and releases resources.
-
Constructor Details
-
SocketEventBus
Creates a newSocketEventBususing the specified transport and dispatch mode.- Parameters:
transport- the transport implementation (e.g., TCPTransport or UDPTransport)dispatchMode- determines how local handlers are executed:DispatchMode.SEQUENTIAL– handlers run sequentially on the caller threadDispatchMode.PARALLEL– handlers run concurrently using a shared thread poolDispatchMode.PER_HANDLER– each handler has its own single-thread executor for ordered execution
-
-
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.The handler will also be registered with the underlying transport for remote messages.
-
send
Sends a point-to-point message to all registered peers and dispatches locally.Uses
sendsemantics: only the first handler on the remote side will process the message. -
publish
Publishes a message to all registered peers and dispatches locally.Uses
publishsemantics: all handlers on the remote side will process the message. -
shutdown
public void shutdown()Shuts down the event bus and releases resources.Stops the underlying transport, alle executors, and clears local handlers and peer list.
-