【问题标题】:How to read stream (messages) from TCP in Spring Inegration如何在 Spring Integration 中从 TCP 读取 Steam(消息)
【发布时间】:2017-05-08 04:58:24
【问题描述】:

我无法清楚地理解如何在 Spring Integration 中读取 TCP 流。我有一个外部服务,它向连接到它的客户端(TCP、Java 套接字)广播一些数据。我已经在 Spring Integration 中尝试了许多配置。我的应用程序成功连接到我的外部服务(我可以从服务的日志中确认),但我仍然无法捕获流。但是,当我手动将一些消息发送到服务(通过 MessageChannel)时,我的 bean 工作正常。这确实意味着我的配置配置为捕获写入事件。我找不到明确的指南或手册来配置它以接收 TCP 流。我的项目中有这个 xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
       xmlns:int="http://www.springframework.org/schema/integration"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="accumulator"
          class="ru.utilex.trueguard.Accumulator" />


    <int-ip:tcp-connection-factory id="client"
                                   type="client"
                                   host="192.168.2.28"
                                   port="8383"
                                   so-timeout="10000"/>

    <int:channel id="input" />
    <int:channel id="toSA" />

    <int-ip:tcp-outbound-channel-adapter id="inboundClient"
                                        client-mode="true"
                                        channel="input"
                                        auto-startup="true"
                                        connection-factory="client"/>


    <int:object-to-string-transformer id="serverBytes2String"
                                      input-channel="input"
                                      output-channel="toSA"/>


    <int:service-activator
            input-channel="toSA"
            ref="accumulator"
            method="read"/>
</beans>

我的累加器类应该是我的接收通道的端点:

public class Accumulator {

    public void read(String buffer) {
        System.out.println("We have caught this message: " + buffer);
    }

}

【问题讨论】:

    标签: java spring sockets tcp spring-integration


    【解决方案1】:

    您需要一个入站通道适配器。

    请注意,在您的连接工厂配置中,流上的消息需要用 CRLF (0x0d0a) 分隔。

    如果您的服务使用其他机制来划分消息,您将需要不同的反序列化器。

    入站适配器发出的消息将具有byte[] 有效负载;如果你需要 String,你应该在适配器和你的服务之间插入一个 object-to-string-transformer。

    【讨论】:

    • 感谢您的回复,加里。我都将配置更改为入站通道适配器和服务器端以推送数据,用 crlf 分隔,但我的累加器中仍然没有任何内容。消息只是不会调用
    • 现在它正在工作:)。我对我的配置进行了几处更改,以使用我自己的数据转换器(从字节制作字符串)并将结果吐出到累加器,然后我的应用程序开始工作。我想,我的大错误是没有在消息之间添加分隔符,所以 spring 适配器根本无法读取消息。对于那些正在寻找配置的人来说,这里是 pastebin。配置(xml)pastebin.com/Dqdkn5kS,java类:pastebin.com/PJ8gYLfa
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2015-07-02
    • 2019-01-26
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多