Spring集成:TCP禁用响应读取



我有以下要求

  1. 连接到套接字(TCP)
  2. 发送初始化消息[服务器没有响应]
  3. 检查连接是否处于活动状态,如果是,则发送轮询消息
  4. 如果有一个响应过程

我正在尝试为TCP连接使用Spring集成,但当我尝试执行第二步时,框架会等待响应。有没有办法将其配置为不等待步骤2中的响应?

以下是弹簧配置:

<int:gateway default-request-channel="msOutbound"
    service-interface="com.home.tcp.UserService" id="gw" />
<int-ip:tcp-outbound-gateway id="tcpObGw"
    connection-factory="client" request-channel="msOutbound"
     />
<int-ip:tcp-connection-factory id="client"
    type="client" deserializer="serDeSer" serializer="serDeSer"
    host="remoteServer" port="valid port"  single-use="true" />
<bean id="serDeSer"
    class="org.springframework.integration.ip.tcp.serializer.ByteArrayStxEtxSerializer" />

Java代码:

service.send("init msg"); // The program exits with no response exception.
service.send("polling message"); //Never executed.

网关是一种请求/响应机制(一个请求,一个响应)。您将single-use设置为true,因此即使第一个返回了响应,第二个也会进入另一个套接字。

对于您的场景,您需要将single-use设置为false,并在第一个send周围放置一个try/catch,因为您预计会出现异常。

为了改善定时,您应该将网关上的remoteTimeout设置为一个非常小的值(0),仅用于第一次发送,然后将其增加到合理的值。

最新更新