0

I have several TCP/IP Outbound Adapters to send Messages to connecting clients. The TCP/IP protocol of communication requires the socket flags to be set: TCP_NODELAY, SO_REUSEADDR, and SO_KEEPALIVE.

    @Override
    public void start(String context, int port) {
        TcpServerConnectionFactorySpec server = Tcp.netServer(port)
                .id(context)
                .soTcpNoDelay(true)
                .soKeepAlive(true)
                .serializer(new ByteArrayLengthHeaderSerializer())
                ;
        IntegrationFlow flow = f -> f.handle(Tcp.outboundAdapter(server));
        this.registrations.put(context, flowContext.registration(flow).register());
    }

How do I set the socket's SO_REUSEADDR flag to TRUE?

JBStonehenge
  • 192
  • 3
  • 15

1 Answers1

0

Implement a custom TcpSocketSupport...

public class MySocketSupport extends DefaultTcpSocketSupport {

    @Override
    public void postProcessServerSocket(ServerSocket serverSocket) {
        ...
    }

}

then...

.tcpSocketSupport(new MySocketSupport())
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • It works! Thank you! But ... I get a TCP_NODELAY not supported exception. What is it that does not support the NODELAY? It is the TCP Outbound Adapter? Or deeper in my JVM? – JBStonehenge May 12 '22 at 15:46
  • I don't think that property is supported on server sockets (you don't actually send data there); it's probably only available on the socket created when a new connection is accepted - you can set properties on those sockets in `postProcessSocket()`. – Gary Russell May 12 '22 at 16:01
  • Thanks Gary! I found out the exception came when trying to set the TCP_NODELAY option within postProcessServerSocket(). When I set the option with `.soTcpNoDelay(true)` everything runs fine. I suppose it is exactly as you explained, the TCP_NODELAY then is applied to the created socket of the accepted client connection. – JBStonehenge May 13 '22 at 00:31
  • Yes; I forgot we expose that one as a first class option. – Gary Russell May 13 '22 at 14:38