Class NioTcpTransport
- All Implemented Interfaces:
Transport
Transport implementation using Java NIO for TCP-based communication.
This class provides non-blocking I/O for message exchange between components,
services or devices using a simple JSON-based protocol.
Messages are framed by newline characters and contain
fields for address, payload, and type.
Features:
- Non-blocking server using
SelectorandServerSocketChannel. - Handles multiple clients concurrently.
- Dispatches messages to registered handlers asynchronously via a worker pool.
- Supports point-to-point (
send(java.lang.String, int, java.lang.String, java.lang.Object)) and broadcast (publish(java.lang.String, int, java.lang.String, java.lang.Object)) messaging.
Message Format:
{
"address": "topic.name",
"payload": "data",
"type": "send" | "publish"
}
Example usage:
NioTcpTransport transport = new NioTcpTransport(8080);
transport.register("sensor.data", msg -> System.out.println("Received: " + msg));
transport.start();
transport.send("localhost", 8080, "sensor.data", "Hello");
Connection model
One TCP connection is kept open per target host:port and reused for every message sent
to that peer. Messages are newline-delimited on that stream, so a single connection carries an
unbounded sequence of them.
This matters for throughput. An earlier implementation opened a fresh SocketChannel
per message — connect, write, close — making one datapoint one TCP connection; the receiving
instance accepts on a single selector thread and could not drain the listen backlog, so above
roughly 7,400 messages/s the kernel dropped SYNs and 10–30 % of messages were lost while
write still returned successfully and no failure was reported. Reuse removes the
per-message handshake entirely and turns silent loss into backpressure: writes are blocking, so a
sender outrunning its peer is throttled by the socket rather than having data discarded.
Threading
Writes to one peer are serialized on that peer's connection — concurrent senders would
otherwise interleave partial JSON on the stream and corrupt the newline framing. Writes to
different peers proceed in parallel. A write blocks its calling thread while the peer's
receive window is full, which is the intended backpressure; callers must therefore not write from
an event-bus handler thread (SocketEventBus already publishes to peers on its own pool).
Reading happens on the single selector thread; handler invocation is handed to the worker pool.
Note: Always call shutdown() to release resources when done.
-
Constructor Summary
ConstructorsConstructorDescriptionNioTcpTransport(int port) Creates a new NIO TCP transport bound to the specified port. -
Method Summary
Modifier and TypeMethodDescriptionvoidPublishes a message to all subscribers of the specified address.voidRegisters a handler for messages received on the specified address.voidSends a message to the first subscriber of the specified address.voidshutdown()Shuts down the transport, closing the selector, server channel, and worker pool.voidstart()Starts the transport event loop in a background thread.
-
Constructor Details
-
NioTcpTransport
Creates a new NIO TCP transport bound to the specified port.- Parameters:
port- the TCP port to listen on- Throws:
IOException- if the server socket or selector cannot be initialized
-
-
Method Details
-
start
public void start()Starts the transport event loop in a background thread.Accepts new connections and reads incoming messages asynchronously.
-
send
Sends a message to the first subscriber of the specified address. -
publish
Publishes a message to all subscribers of the specified address. -
shutdown
public void shutdown()Shuts down the transport, closing the selector, server channel, and worker pool. -
register
Registers a handler for messages received on the specified address.
-
