对于最新稳定版本,请使用 Spring Integration 7.0.0spring-doc.cadn.net.cn

TCP适配器

提供了使用前述连接工厂的TCP入站和出站通道适配器。 这些适配器具有两个相关属性:连接工厂渠道. 这连接工厂属性表示将使用哪个连接工厂来管理适配器的连接。 这渠道属性指定消息到达出站适配器的通道,以及消息被入站适配器放置的通道。 虽然入站和出站适配器可以共享一个连接工厂,但服务器连接工厂始终由入站适配器“拥有”。 客户端连接工厂总是由出站适配器“拥有”。 每种类型只有一个适配器可以获得连接工厂的参考。 以下示例展示了如何定义客户端和服务器TCP连接工厂:spring-doc.cadn.net.cn

<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"/>

在前述配置中,抵达输入通道通过以下连接串行化客户端连接工厂,在服务器接收,并放置在渠道。 因为是 的输入信道outboundServer消息通过同一连接循环回来,接收者为inboundClient,并存入于答复渠道。 线路上使用了 Java 序列化。spring-doc.cadn.net.cn

通常,入站适配器使用一个type=“服务器”连接工厂,监听来的连接请求。 在某些情况下,你可能想反向建立连接,比如入站适配器连接到外部服务器,然后等待该连接上的入站消息。spring-doc.cadn.net.cn

该拓扑由以下设置支持client-mode=“true”在入站适配器上。 在这种情况下,连接工厂必须是客户端并且必须一次性用途设置为false.spring-doc.cadn.net.cn

还有两个额外属性支持这一机制。 这重试区间指定(以毫秒计)框架在连接失败后尝试重新连接的频率。 这调度供应任务调度器用于安排连接尝试并测试连接是否仍然有效。spring-doc.cadn.net.cn

如果你不提供调度器,则使用框架默认的 taskScheduler 豆。spring-doc.cadn.net.cn

对于出站适配器,连接通常在发送第一条消息时建立。 这client-mode=“true”在外接适配器上,连接在适配器启动时才会建立。 默认情况下,适配器会自动启动。 同样,连接工厂必须是类型客户端并且有一次性使用=“false”. 一个重试区间调度也支持。 如果连接失败,调度器或下一条消息发送时会重新建立连接。spring-doc.cadn.net.cn

无论是进站还是出站,如果适配器已启动,你可以通过发送<控制总线 />命令:@adapter_id.retryConnection(). 然后你可以用@adapter_id.isClientModeConnected().spring-doc.cadn.net.cn