Search code examples
javaspring-bootspring-integrationspring-integration-ip

Connection not found when processing reply GenericMessage


Try to set up Spring Boot TCP Server with spring-integration-ip, but when send message to the server always get the error message:

2021-06-03 23:58:36.916 ERROR 35752 --- [pool-1-thread-4] o.s.i.ip.tcp.TcpInboundGateway           : Connection not found when processing reply GenericMessage [payload=byte[5], headers={ip_tcp_remotePort=51436, ip_connectionId=localhost:51436:10001:dbc9ca03-a509-4a66-9bbd-020478794bbd, ip_localInetAddress=/0:0:0:0:0:0:0:1, ip_address=0:0:0:0:0:0:0:1, id=3c446dc8-f417-e387-a4e4-42c023da0d4f, ip_hostname=localhost, timestamp=1622746716916}] for GenericMessage [payload=byte[3], headers={ip_tcp_remotePort=51436, ip_connectionId=localhost:51436:10001:dbc9ca03-a509-4a66-9bbd-020478794bbd, ip_localInetAddress=/0:0:0:0:0:0:0:1, ip_address=0:0:0:0:0:0:0:1, id=59caa1c3-20df-57c0-d1dc-81c2b0adfe0d, ip_hostname=localhost, timestamp=1622746716916}]

Here is my TcpServerConfig file:

@Configuration
@EnableIntegration
public class TcpServerConfig {

    @Value("${tcp.server.port}")
    private int port;

    @Bean
    public AbstractServerConnectionFactory serverConnectionFactory() {
        TcpNioServerConnectionFactory serverConnectionFactory = new TcpNioServerConnectionFactory(port);
        serverConnectionFactory.setUsingDirectBuffers(true);
        return serverConnectionFactory;
    }

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

    @Bean
    public TcpInboundGateway inboundGateway(AbstractServerConnectionFactory serverConnectionFactory,
                                            MessageChannel inboundChannel) {
        TcpInboundGateway tcpInboundGateway = new TcpInboundGateway();
        tcpInboundGateway.setConnectionFactory(serverConnectionFactory);
        tcpInboundGateway.setRequestChannel(inboundChannel);
        return tcpInboundGateway;
    }
}

And TcpServerEnpoint file:

@Component
@MessageEndpoint
public class TcpServerEndpoint {

    @ServiceActivator(inputChannel = "inboundChannel")
    public byte[] process(byte[] message) {
        System.out.println(Arrays.toString(message));
        return "Hello".getBytes();
    }
}

I'm trying to make simple request -> response TCP server. When message has been sent it print out in debug console, but also throws the error.


Solution

  • Here is what I have for you:

    @SpringBootApplication
    public class So67827589Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So67827589Application.class, args);
        }
    
        @Bean
        public AbstractServerConnectionFactory serverConnectionFactory() {
            TcpNioServerConnectionFactory serverConnectionFactory = new TcpNioServerConnectionFactory(7777);
            serverConnectionFactory.setUsingDirectBuffers(true);
            return serverConnectionFactory;
        }
    
        @Bean
        public MessageChannel inboundChannel() {
            return new DirectChannel();
        }
    
        @Bean
        public TcpInboundGateway inboundGateway(AbstractServerConnectionFactory serverConnectionFactory,
                MessageChannel inboundChannel) {
    
            TcpInboundGateway tcpInboundGateway = new TcpInboundGateway();
            tcpInboundGateway.setConnectionFactory(serverConnectionFactory);
            tcpInboundGateway.setRequestChannel(inboundChannel);
            return tcpInboundGateway;
        }
    
        @ServiceActivator(inputChannel = "inboundChannel")
        public byte[] process(byte[] message) {
            System.out.println(Arrays.toString(message));
            return "Hello".getBytes();
        }
    
    }
    

    Then I go to command line and do:

    telnet localhost 7777
    

    and type anything and get Hello back. Then I can type more and more - and Hello comes back to me. Always!

    So, everything works as expected. Spring Boot 2.5.0.