Search code examples
spring-integration

Can i set remoteTimeOut to Infinity on TcpOutBoundgateway


I am using CachingClientConnectionFactory, How can i keep the connections alive, its closing out after default remoteTimeOut elapses, Can I set the remoteTimeOut to LONG.MAX_VALUE?

Thanks

 @Bean
    public AbstractClientConnectionFactory clientConnectionFactory() {  
        TcpNioClientConnectionFactory tcpNioClientConnectionFactory = new TcpNioClientConnectionFactory(host, port);
        tcpNioClientConnectionFactory.setUsingDirectBuffers(true);
        tcpNioClientConnectionFactory.setApplicationEventPublisher(applicationEventPublisher);
        return new CachingClientConnectionFactory(tcpNioClientConnectionFactory, connectionPoolSize);
    }

    @Bean
    public MessageChannel outboundChannel() {
        return new DirectChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "outboundChannel")
    public MessageHandler outboundGateway(AbstractClientConnectionFactory clientConnectionFactory) {
        TcpOutboundGateway tcpOutboundGateway = new TcpOutboundGateway();
        tcpOutboundGateway.setConnectionFactory(clientConnectionFactory);
        tcpOutboundGateway.setRemoteTimeout(Long.MAX_VALUE);
        tcpOutboundGateway.setRequestTimeout(5_000);
        return tcpOutboundGateway;
    }

Solution

  • Yes I want it to single thread, I want concurrent send to be blocked until the socket is available;

    Yes; with that configuration, it will be single-threaded; concurrent requests will wait for up to requestTimeout to get access to the shared socket.

    https://github.com/spring-projects/spring-integration/blob/4484c4da753096094e5b376411b94ac4ba2834c6/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpOutboundGateway.java#L217

    try {
        haveSemaphore = acquireSemaphoreIfNeeded(requestMessage);
        connection = this.connectionFactory.getConnection();
        ...
    

    The request that gets access to the socket then waits for up to replyTimeout for a reply. If it times out, the socket is closed, to avoid the next request getting this request's reply. The next request will get a new socket.

    tcpOutboundGateway.setRemoteTimeout(Long.MAX_VALUE);
    

    That should be reduced to something more reasonable otherwise you could block forever if, for some reason, the server doesn't reply (but keeps the socket open).