我使用spring集成来连接到tcp / ip套接字服务器,我基于telnet-mock https://github.com/maltempi/telnet-mock创建了模拟服务器。并且可以发送和接收消息,但是当我关闭模拟服务器时,在主应用程序中会发生循环错误,并占用所有CPU时间:
ERROR 13942 --- [pool-4-thread-1] o.s.i.ip.tcp.TcpOutboundGateway : Cannot correlate response - no pending reply for Cached:localhost:3002:46550:f6234e17-c486-4506-82c8-a757a08ba73d.
如何解决此问题?我的配置类:
@EnableIntegration
@RequiredArgsConstructor
@Configuration
public class StpClientConfiguration {
private static final String REQUEST_CHANNEL = "toStp";
private static final String OUTPUT_CHANNEL = "resultToMap";
private static final String CRLF = "\\0";
private final ApplicationProperties applicationProperties;
private final ApplicationContext context;
private static String readUntil(InputStream inputStream, String stopWord) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream));
int r;
while ((r = buffer.read()) != -1) {
char c = (char) r;
sb.append(c);
if (sb.toString().endsWith(stopWord)) {
break;
}
}
return sb.toString();
}
@Bean
public CachingClientConnectionFactory connectionFactory() {
TcpNetClientConnectionFactory factory = new TcpNetClientConnectionFactory(
applicationProperties.getHost(), applicationProperties.getPort());
factory.setApplicationEventPublisher(this.context);
factory.setTcpSocketSupport(new DefaultTcpSocketSupport());
factory.setDeserializer((InputStream inputStream) -> readUntil(inputStream, CRLF));
return new CachingClientConnectionFactory(factory, applicationProperties.getPoolSize());
}
/**
* Creates the tcp gateway for service activation.
*
* @return the message handler
*/
@Bean
@ServiceActivator(inputChannel = REQUEST_CHANNEL)
public MessageHandler outboundGateway() {
TcpOutboundGateway gateway = new TcpOutboundGateway();
gateway.setConnectionFactory(connectionFactory());
gateway.setOutputChannelName(OUTPUT_CHANNEL);
return gateway;
}
@MessagingGateway(defaultRequestChannel = REQUEST_CHANNEL)
public interface RequestGateway {
Map<String, String> send(String message);
}
@Bean
@Transformer(inputChannel = OUTPUT_CHANNEL)
public ObjectToMapTransformer objectToMapTransformer() {
return new ObjectToMapTransformer();
}
}
答案 0 :(得分:0)
您的解串器看起来可疑; telnet消息以\r\n
而不是\\0
终止。
对telnet使用默认的反序列化器(默认为ByteArrayCrLfSerializer
)。
当反序列化程序在消息之间检测到流的正常结尾(-1
)时,它必须抛出SoftEndOfStreamException
来告知框架套接字已关闭。您的代码不断返回长度为零的字符串,
/**
* Used to communicate that a stream has closed, but between logical
* messages.
*/
public class SoftEndOfStreamException extends IOException {
或者,正如我所说的,使用默认的反序列化器。