gpt4 book ai didi

java - Spring 集成 : TcpInboundGateway read stream closed before @MessageEndpoint writes to out stream

转载 作者:行者123 更新时间:2023-11-30 08:56:21 25 4
gpt4 key购买 nike

我按照 TcpInboundGateway 的示例使用 spring boot,但我发现行为与我正在替换的手工编码的一段 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;
}

关于java - Spring 集成 : TcpInboundGateway read stream closed before @MessageEndpoint writes to out stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com