【问题标题】:spring -integration TCP gateway, connect and receive data from Socketspring - 集成 TCP 网关,连接和接收来自 Socket 的数据
【发布时间】:2017-06-28 04:33:07
【问题描述】:

我正在尝试在 Spring 集成中实现 TCP 客户端。我有一个远程 TCP 服务器,它将数据泵入一个套接字。我的基于 Spring 的 TCP 客户端必须从该套接字接收数据。

作为客户端,我不会从我这边向服务器发送任何数据,只是连接和接收数据。看着这个http://forum.spring.io/forum/spring-projects/integration/94696-want-to-configure-simple-tcp-client-to-receive-data-from-java-based-tcp-server?view=thread,我明白这是不可能的。但是,收到的答案已经很老了,现在有什么可用的配置吗?

如果您还有其他问题,请告诉我。

@更新配置

<bean id="javaSerializer" class="org.springframework.core.serializer.DefaultSerializer" />
<bean id="javaDeserializer" class="org.springframework.core.serializer.DefaultDeserializer" />

<context:property-placeholder />

<!-- Client side -->

<int:gateway id="gw"
    service-interface="com.my.client.SimpleGateway"
    default-request-channel="input" default-reply-channel="replies" />

<int-ip:tcp-connection-factory id="client"
    type="client" host="localhost" port="5678"
    single-use="false" so-timeout="10000" serializer="javaSerializer"
    deserializer="javaDeserializer" so-keep-alive="true"/>

<int:channel id="input" />

<int:channel id="replies">
    <int:queue />
</int:channel>

<!-- <int-ip:tcp-outbound-gateway id="outGateway" request-channel="input" 
    reply-channel="reply" connection-factory="client" request-timeout="10000" 
    reply-timeout="10000" /> -->

<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"
    client-mode="true" retry-interval="10000" auto-startup="true" />

这是我的远程 TCP 客户端:

    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.load("classpath:config.xml");

    context.registerShutdownHook();
    context.refresh();

    final SimpleGateway gateway = context.getBean(SimpleGateway.class);
    int i=0;
    while(i++<10){
    String h = gateway.receive();
    System.out.println(System.currentTimeMillis()+h);

我的 TCP 模拟服务器:

while(true) {
     try {
        System.out.println("Waiting for client on port " +
        serverSocket.getLocalPort() + "...");

        Socket server = serverSocket.accept();
        System.out.println("Just connected to "
              + server.getRemoteSocketAddress());

        DataOutputStream out =
             new DataOutputStream(server.getOutputStream());
        out.write("ACK\r\n".getBytes());

        out.flush();

       //server.close();

     } catch(SocketTimeoutException s) {
        System.out.println("Socket timed out!");
        break;
     } catch(IOException e) {
        e.printStackTrace();
        break;
     } 
  }

我的网关类:

public interface SimpleGateway {    
    public String receive();
}

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    TcpReceivingChannelAdapter (&lt;ip:tcp-inbound-channel-adapter/&gt;) 通常在服务器模式下运行 - 它在套接字上侦听到客户端的传入连接。

    然而,为这个用例添加了一个 clientMode (client-mode) 布尔值。它将连接到服务器并接收来自它的传入数据。如果连接丢失,它将重试连接(根据配置计划)。

    the documentation:

    通常,入站适配器使用 type="server" 连接工厂,它侦听传入的连接请求。在某些情况下,需要反向建立连接,即入站适配器连接到外部服务器,然后等待该连接上的入站消息。

    在入站适配器上使用 client-mode="true" 支持此拓扑。在这种情况下,连接工厂的类型必须为 client,并且必须将 single-use 设置为 false。

    两个附加属性用于支持此机制:retry-interval 指定(以毫秒为单位)框架在连接失败后尝试重新连接的频率。 scheduler 用于提供一个 TaskScheduler,用于安排连接尝试,并测试连接是否仍处于活动状态。

    如果未提供调度程序,则使用默认的taskScheduler bean。

    【讨论】:

    • 感谢您的快速回复。我在 GIT 上遵循你的例子。我会做上述配置,并会返回结果。希望我能得到它! :)
    • 我尝试了解释的配置。但我看不到服务器的响应。我不确定我是否遗漏了任何东西或可能不正确。我有一个接口网关,其方法定义为:public String receive(String text);。它只是从 TCP 服务器接收数据。 xml 配置如上面文档中所述。请在上面找到配置。在我的客户TCPClient 中,我调用receive(),但我没有收到任何数据。
    • 更新上面的配置后,我尝试使用自定义反序列化器,而不是java反序列化器。好消息是,我看到数据进入deserialize() 方法,但没有进入我的TCPClient.receive()。它在运行方法中卡在org.springframework.integration.ip.tcp.connection.TcpNetConnection ,因为 okToRun 是真的。我之所以详细写这篇文章,是因为,我明白了,你是这门课的作者。
    • 我很困惑;您的配置与您的问题不一致。 As a Client I am not sending any data from my side to the server, just connect and receive data.您的客户端确实在发送数据。
    • 之前我只是尝试连接和接收数据,但没有成功。所以我想,让我尝试发送一些数据并接收回复。现在我能够得到响应,我看到数据来自我的deserialize() 方法。这不会传播给它的调用者。这是现在的问题阶段。
    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多