我使用Spring Integration处理服务器和客户端之间的自定义TCP协议。这是我的context.xml:

<int-ip:tcp-connection-factory id="serverConnectionFactory"
                               type="server"
                               host=""
                               port="5678"
                               so-linger="5000"
                               so-keep-alive="true"
                               single-use="false"
                               using-nio="true"
                               so-timeout="5000"
                               serializer="customeMessageSerializerDeserializer"
                               deserializer="customeMessageSerializerDeserializer"/>

<int-ip:tcp-inbound-channel-adapter id="inAdapter.server"
                                    channel="toSA"
                                    connection-factory="serverConnectionFactory"/>

<int:channel id="toSA" datatype="com.my.Message"/>

<int:service-activator input-channel="toSA"
                       output-channel="toObAdapter"
                       ref="handleMessage"
                       method="handle"/>

<int:channel id="toObAdapter"/>

<int-ip:tcp-outbound-channel-adapter id="outAdapter.server"
                                     channel="toObAdapter"
                                     connection-factory="serverConnectionFactory"/>

<bean id="handleMessage" class="com.my.MessageHandler"/>
<bean id="customeMessageSerializerDeserializer"
      class="com.my.MessageSerializyerDeserializer"/>


当我通过tomcat部署它时,它的服务时间只有几分钟,并且对客户端没有响应。发现一个错误:

org.springframework.messaging.MessageHandlingException: Unable to find outbound socket
at      org.springframework.integration.ip.tcp.TcpSendingMessageHandler.handleMessageInternal(  TcpSendingMessageHandler.java:122)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(  AbstractMessageProducingHandler.java:231)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(  AbstractMessageProducingHandler.java:154)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(  AbstractMessageProducingHandler.java:102)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(  AbstractReplyProducingMessageHandler.java:105)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage(MessageProducerSupport.java:105)
at org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter.onMessage(TcpReceivingChannelAdapter.java:87)
at org.springframework.integration.ip.tcp.connection.TcpNioConnection.sendToChannel(TcpNioConnection.java:370)
at org.springframework.integration.ip.tcp.connection.TcpNioConnection.run(TcpNioConnection.java:239)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)


我想知道如何修复它以及错误之处。

最佳答案

使用collaborating channel adapters时,必须确保在出站消息中设置ip_connectionId标头,以便适配器知道将其写入哪个套接字。

通常,使用这样的简单流程,框架将负责将标头从入站消息传播到出站消息。

因此,我需要查看该方法以了解您的工作。

您的“ handleMessage”代码是什么样的?

另一种可能性是5秒超时-如果您的服务花费的时间超过了连接将不再存在的时间。

通常,打开DEBUG日志记录应有助于调试此类问题。

07-24 14:52