TCP Adapters
提供了使用连接工厂 mentioned earlier的 TCP 入站和出站通道适配器。这些适配器有两个相关的属性:connection-factory`和 `channel
。`connection-factory`属性指示哪个连接工厂用于管理该适配器的连接。`channel`属性指定出站适配器接收消息的通道以及入站适配器放置消息的通道。虽然入站和出站适配器都可以共享连接工厂,但服务器连接工厂始终由入站适配器 “owned”。客户端连接工厂始终由出站适配器 “owned”。每种类型的适配器只能获得一个对连接工厂的引用。以下示例显示了如何定义客户端和服务器 TCP 连接工厂:
TCP inbound and outbound channel adapters that use connection factories mentioned earlier are provided.
These adapters have two relevant attributes: connection-factory
and channel
.
The connection-factory
attribute indicates which connection factory is to be used to manage connections for the adapter.
The channel
attribute specifies the channel on which messages arrive at an outbound adapter and on which messages are placed by an inbound adapter.
While both inbound and outbound adapters can share a connection factory, server connection factories are always “owned” by an inbound adapter.
Client connection factories are always “owned” by an outbound adapter.
Only one adapter of each type may get a reference to a connection factory.
The following example shows how to define client and server TCP connection factories:
<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 序列化在线路上使用。
In the preceding configuration, messages arriving in the input
channel are serialized over connections created by client
connection factory, received at the server, and placed on the loop
channel.
Since loop
is the input channel for outboundServer
, the message is looped back over the same connection, received by inboundClient
, and deposited in the replies
channel.
Java serialization is used on the wire.
通常,入站适配器使用 type="server"
连接工厂,该工厂会侦听传入的连接请求。在某些情况下,你可能希望以相反的方式建立连接,以便入站适配器连接到外部服务器,然后等待该连接上的入站消息。
Normally, inbound adapters use a type="server"
connection factory, which listens for incoming connection requests.
In some cases, you may want to establish the connection in reverse, such that the inbound adapter connects to an external server and then waits for inbound messages on that connection.
通过在入站适配器上设置 client-mode="true"
可以支持此拓扑。在这种情况下,连接工厂必须为 client
类型,并且必须将 single-use
设置为 false
。
This topology is supported by setting client-mode="true"
on the inbound adapter.
In this case, the connection factory must be of type client
and must have single-use
set to false
.
还有两个其他属性支持此机制。retry-interval
指定(以毫秒为单位)在连接故障后框架尝试重新连接的频率。scheduler
为 TaskScheduler
提供支持,以便计划连接尝试并测试连接是否仍然处于活动状态。
Two additional attributes support this mechanism.
The retry-interval
specifies (in milliseconds) how often the framework attempts to reconnect after a connection failure.
The scheduler
supplies a TaskScheduler
to schedule the connection attempts and to test that the connection is still active.
如果您未提供调度程序,则使用框架的默认 taskScheduler bean。
If you don’t provide a scheduler, the framework’s default taskScheduler bean is used.
对于出站适配器,连接通常在发送第一条消息时建立。出站适配器上的 client-mode="true"
会导致在启动适配器时建立连接。默认情况下,适配器会自动启动。同样,连接工厂必须为 client
类型,并且必须将 single-use="false"
。retry-interval
和 scheduler
也受支持。如果连接失败,它将由调度程序或在发送下一条消息时重新建立连接。
For an outbound adapter, the connection is normally established when the first message is sent.
The client-mode="true"
on an outbound adapter causes the connection to be established when the adapter is started.
By default, adapters are automatically started.
Again, the connection factory must be of type client
and have single-use="false"
.
A retry-interval
and scheduler
are also supported.
If a connection fails, it is re-established either by the scheduler or when the next message is sent.
对于入站和出站连接,如果启动了适配器,则可以通过发送 <control-bus />
命令来强制适配器建立连接:@adapter_id.retryConnection()
。然后,你可以检查 @adapter_id.isClientModeConnected()
的当前状态。
For both inbound and outbound, if the adapter is started, you can force the adapter to establish a connection by sending a <control-bus />
command: @adapter_id.retryConnection()
.
Then you can examine the current state with @adapter_id.isClientModeConnected()
.