处理回复 GenericMessage 时找不到连接

Connection not found when processing reply GenericMessage

尝试使用 spring-integration-ip 设置 Spring 启动 TCP 服务器,但是向服务器发送消息时总是收到错误消息:

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}]

这是我的 TcpServerConfig 文件:

@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;
    }
}

和 TcpServerEnpoint 文件:

@Component
@MessageEndpoint
public class TcpServerEndpoint {

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

我正在尝试发出简单的请求 -> 响应 TCP 服务器。发送消息后,它会在调试控制台中打印出来,但也会抛出错误。

这是我为您准备的:

@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();
    }

}

然后我转到命令行并执行:

telnet localhost 7777

并输入任何内容并返回 Hello。然后我可以输入越来越多 - Hello 返回给我。永远!

所以,一切都按预期进行。 Spring 启动 2.5.0.