对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
TCP 适配器
提供了使用前面提到的连接工厂的 TCP 入站和出站通道适配器。
这些适配器有两个相关属性:connection-factory
和channel
.
这connection-factory
属性指示要使用哪个连接工厂来管理适配器的连接。
这channel
属性指定消息到达出站适配器的通道以及入站适配器将消息放置在的通道上。
虽然入站适配器和出站适配器都可以共享连接工厂,但服务器连接工厂始终由入站适配器“拥有”。
客户端连接工厂始终由出站适配器“拥有”。
每种类型的适配器只能获得对连接工厂的引用。
以下示例显示了如何定义客户端和服务器 TCP 连接工厂:
<bean id="javaSerializer"
class="org.springframework.core.serializer.DefaultSerializer"/>
<bean id="javaDeserializer"
class="org.springframework.core.serializer.DefaultDeserializer"/>
<int-ip:tcp-connection-factory id="server"
type="server"
port="1234"
deserializer="javaDeserializer"
serializer="javaSerializer"
using-nio="true"
single-use="true"/>
<int-ip:tcp-connection-factory id="client"
type="client"
host="localhost"
port="#{server.port}"
single-use="true"
so-timeout="10000"
deserializer="javaDeserializer"
serializer="javaSerializer"/>
<int:channel id="input" />
<int:channel id="replies">
<int:queue/>
</int:channel>
<int-ip:tcp-outbound-channel-adapter id="outboundClient"
channel="input"
connection-factory="client"/>
<int-ip:tcp-inbound-channel-adapter id="inboundClient"
channel="replies"
connection-factory="client"/>
<int-ip:tcp-inbound-channel-adapter id="inboundServer"
channel="loop"
connection-factory="server"/>
<int-ip:tcp-outbound-channel-adapter id="outboundServer"
channel="loop"
connection-factory="server"/>
<int:channel id="loop"/>
在前面的配置中,到达input
通道通过client
连接工厂,在服务器上接收,并放置在loop
渠道。
因为loop
是outboundServer
,消息通过同一连接循环回传,由inboundClient
,并存入replies
渠道。
Java 序列化用于网络。
通常,入站适配器使用type="server"
连接工厂,它监听传入的连接请求。
在某些情况下,您可能希望反向建立连接,以便入站适配器连接到外部服务器,然后等待该连接上的入站消息。
此拓扑由设置client-mode="true"
在入站适配器上。
在这种情况下,连接工厂的类型必须是client
并且必须有single-use
设置为false
.
另外两个属性支持此机制。
这retry-interval
指定(以毫秒为单位)框架在连接失败后尝试重新连接的频率。
这scheduler
供应TaskScheduler
以安排连接尝试并测试连接是否仍然处于活动状态。
如果不提供调度程序,则使用框架的默认 taskScheduler bean。
对于出站适配器,通常在发送第一条消息时建立连接。
这client-mode="true"
在出站适配器上导致在启动适配器时建立连接。
默认情况下,适配器会自动启动。
同样,连接工厂的类型必须是client
并且有single-use="false"
.
一个retry-interval
和scheduler
也受支持。
如果连接失败,则由调度程序或在发送下一条消息时重新建立连接。
对于入站和出站,如果适配器已启动,则可以通过发送<control-bus />
命令:@adapter_id.retryConnection()
.
然后你可以检查当前状态@adapter_id.isClientModeConnected()
.