Spring 集成:TcpInboundGateway 读取流在 @MessageEndpoint 写入输出流之前关闭

Spring Integration : TcpInboundGateway read stream closed before @MessageEndpoint writes to out stream

我正在按照 TcpInboundGateway 的示例使用 spring 引导,但发现行为与我正在替换的手工编码的一段 TCP 代码略有不同。

特别是当使用 netcat 时,nc 在收​​到应答之前退出,因为 TcpInboundGateway 读取流在任何写入发生之前关闭,导致 nc 退出。

是否可以延迟到写入完成后关闭读取流?

代码如下:

@Configuration
@EnableIntegration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class);
    }

    @Bean
    TcpNetServerConnectionFactory cf () {
        return new TcpNetServerConnectionFactory(9876);
    }

    @Bean
    TcpInboundGateway tcpGate() {
        TcpInboundGateway gateway = new TcpInboundGateway();
        gateway.setConnectionFactory(cf());
        gateway.setRequestChannel(requestChannel());
        return gateway;
    }

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

    @MessageEndpoint
    public static class Echo {
        @ServiceActivator(inputChannel = "requestChannel")
        public String echo(byte [] in) {
            return "echo: " + new String(in);
        }
    }

    @Autowired
    private Environment env;
}

所以我发现它可能是流终止符。做了一些摆弄,发现使用 ByteArrayRawSerializer 解决了我的问题。

@Bean TcpNetServerConnectionFactory cf () { 
TcpNetServerConnectionFactory tcpNetServerConnectionFactory = new TcpNetServerConnectionFactory(9876); 
tcpNetServerConnectionFactory.setDeserializer(new ByteArrayRawSerializer()); 
return tcpNetServerConnectionFactory; 
}