我按照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;
}
答案 0 :(得分:2)
所以我发现它可能是流终结者角色。做了一些摆弄,发现使用ByteArrayRawSerializer解决了我的问题。
@Bean TcpNetServerConnectionFactory cf () {
TcpNetServerConnectionFactory tcpNetServerConnectionFactory = new TcpNetServerConnectionFactory(9876);
tcpNetServerConnectionFactory.setDeserializer(new ByteArrayRawSerializer());
return tcpNetServerConnectionFactory;
}