邮件的顺序是否发送到" clientOutChannel"保证在" serverInChannel"中收到时保持

时间:2017-10-01 18:35:37

标签: spring-integration

我使用Spring Integration使用类似的

在TCP客户端 - 服务器套接字对之间发送消息

使用

发送客户端消息
<int-ip:tcp-outbound-channel-adapter id="tcpOutboundAdapter"
                                     channel="clientOutChannel"
                                     connection-factory="factoryClient"
                                     client-mode="true"/>

<int-ip:tcp-connection-factory id="factoryClient"
                               type="client"
                               host="127.0.0.1"
                               port="60000"
                               single-use="false"
                               serializer="mySerDe"
                               deserializer="mySerDe"/>

使用

接收服务器端
<int-ip:tcp-inbound-channel-adapter id="tcpInboundAdapter"
                                    channel="serverInChannel"
                                    connection-factory="factoryServer"
                                    client-mode="false/>

<int-ip:tcp-connection-factory id="factoryServer"
                           type="server"
                           host="127.0.0.1"
                           port="60000"
                           single-use="false"
                           serializer="mySerDe"
                           deserializer="mySerDe"/>

我的问题是发送到&#34; clientOutChannel&#34;保证在&#34; serverInChannel&#34;中收到时保持如果没有,那么实现这一目标最好的是什么?

我正在使用服务激活器接收消息

<int:service-activator id="serviceActivator"
                           ref="messageHandler"
                           method="handleMessage"
                           input-channel="serverInChannel"
                           >
    </int:service-activator>

<bean id="messageHandler" class="MessageHandler"/>

谢谢

1 个答案:

答案 0 :(得分:1)

在提出这样的问题时,您必须提供更多的上下文和配置 - 例如您的连接工厂属性。否则,我们必须做太多猜测。

那说;在客户端,如果消息是在同一个线程和同一个套接字(single-use=false - 默认值)上发送的,则消息将按照发送的顺序在线路上发送。

如果它们是在不同的线程或套接字上发送的,则存在竞争条件,并且无法保证订单。

在服务器端,如果客户端在同一个套接字上按顺序发送消息,只要服务器端using-niofalse,它们就会按顺序发出。当服务器端using-nio=true时,无法保证订单,因为消息可能会在不同的线程上调度。

总结一下 - 只要您不使用NIO,就可以使用单个连接,并且您在同一个线程上发送消息,它们将按照发送的顺序接收。