Search code examples
springtcpspring-integrationspring-integration-ip

How to set SO_REUSEADDR in a TCP Outbound Adapter?


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?


Solution

  • Implement a custom TcpSocketSupport...

    public class MySocketSupport extends DefaultTcpSocketSupport {
    
        @Override
        public void postProcessServerSocket(ServerSocket serverSocket) {
            ...
        }
    
    }
    

    then...

    .tcpSocketSupport(new MySocketSupport())