0

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.

Ruslan Skaldin
  • 981
  • 16
  • 29
  • Any chances to have that simple Spring Boot project from you to play with on our side and reproduce? – Artem Bilan Jun 04 '21 at 15:47
  • It is simple tcp server, only 2 files and spring-integration-ip in maven dependencies list. That’s all. – Ruslan Skaldin Jun 04 '21 at 17:12
  • Well, but still what you show must work as out-of-the-box. If it doesn't, then there is something fishy. So, would be great if we investigate an issue on the same environment. For example: how you test this solution would be really crucial. Thanks for understanding. – Artem Bilan Jun 04 '21 at 17:32
  • @ArtemBilan the project is simple, OS is MacOS, put it on GitHub https://github.com/RussellSk/spring-tcp-server – Ruslan Skaldin Jun 05 '21 at 08:22
  • I try to move my Netty project to spring-integration-ip. Thank you for helping, I understand that problem might be in serializer and deserializer, but don't get what should I do. I use this command in Netty project and try to achieve it working with spring-integration-ip, command example: echo -ne "\x01\x02" | nc localhost 8080 | hexdump -v -e "%02X" – Ruslan Skaldin Jun 05 '21 at 14:06

1 Answers1

0

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.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • The provided solution gives the same result with error. I suppose there should be outboundChannel as well as inboundChannel. – Ruslan Skaldin Jun 06 '21 at 08:48
  • 1
    Well, you indeed need to be sure that your client sends the delimiter exactly what server expects. The telnet util sends a `\n\r` which is a default for those connection factories in Spring Integration. See docs about serializers: https://docs.spring.io/spring-integration/docs/current/reference/html/ip.html#tcp-codecs – Artem Bilan Jun 06 '21 at 10:37
  • Please write your answer (not in comment) so I can mark it as accepted. Solve my problem, thank you. – Ruslan Skaldin Jun 06 '21 at 10:55
  • 1
    Well, we are in the answer with you right now: the comments to the answer add to its matter. Your question was about Spring Integration not working - my original answer proves opposite. The incompatibility with the client is a different story you didn’t mention in the beginning . But yeah, enough of that. Glad you fixed your problem! – Artem Bilan Jun 06 '21 at 11:00