我正在使用spring-integration
将数据发送到服务器套接字,并从中读取。
问题:收到的套接字数据流中的读取大多需要1000毫秒! 我正在测试即时响应的本地套接字服务器。
根本原因必须在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。在我的原生套接字上,相同的例程是5-10ms。那么春天的TcpNioConnection.ChannelInputStream
上的某个地方必然会引起第二次延迟吗?
旁注:我刚刚发现,如果我改变fact.setUsingNio(false)
,延迟就消失了。如何使用nio影响这个?