我正在使用 spring-integration
将数据发送到服务器套接字,并从中读取数据。
问题:从接收到的套接字数据流中读取大约需要1000ms! 我正在针对即时响应的本地套接字服务器进行测试。
根本原因一定是在 spring 框架中的某个地方,因为我将 spring-integration 部分更改为原生套接字实现,并且这个立即可以工作。
@Bean
@Primary
public AbstractClientConnectionFactory clientFactory() throws Exception {
TcpConnectionFactoryFactoryBean fact = new TcpConnectionFactoryFactoryBean();
fact.setType("client");
fact.setHost("127.0.0.1");
fact.setPort("9876");
fact.setUsingNio(true); //delay is gone if I change this to false
fact.setSingleUse(true);
fact.setSoTimeout(timeout);
fact.setDeserializer(new MyDeserializer());
fact.afterPropertiesSet();
return (AbstractClientConnectionFactory) fact.getObject();
}
/**
* The same routine applied on a native java socket works instantly!
* But the time measured if used in spring-integration is always at least 1000ms!
*/
static class MyDeserializer implements Deserializer<String> {
@Override
public String deserialize(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
StopWatch w = new StopWatch();
w.start();
String str;
while ((str = br.readLine()) != null) {
sb.append(str).append("\n");
}
w.stop();
System.out.println("time taken: " + w.getTotalTimeMillis());
return sb.toString();
}
}
}
解串器时间大多约为 1005-1010ms。在我的 native 套接字上,相同的例程是 5-10 毫秒。那么 spring 的 TcpNioConnection.ChannelInputStream
上的某个地方一定是导致第二次延迟的原因?
旁注:我刚刚发现,如果我更改 fact.setUsingNio(false)
,延迟就会消失。使用 nio 会对此有何影响?
最佳答案
感谢您的报告 - 这是一个错误 - 我打开了 INT-4465 .
关于java - spring集成中的套接字消息读取延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50251193/