Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring Inegration中从TCP读取流(消息)_Java_Spring_Sockets_Tcp_Spring Integration - Fatal编程技术网

Java 如何在Spring Inegration中从TCP读取流(消息)

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

我不清楚如何在Spring集成中读取TCP流。我有一个外部服务,它向连接到它的客户端(TCP、Java套接字)广播一些数据。我已经在Spring集成中尝试了许多配置。我的应用程序成功地连接到我的外部服务(我可以从服务的日志中确认),但我仍然无法捕获流。然而,当我手动向服务发送一些消息(通过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>

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

请注意,对于您的连接工厂配置,它期望流上的消息用CRLF(0x0d0a)分隔

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


入站适配器发出的消息将具有
字节[]
有效载荷;如果您需要字符串,您应该在适配器和服务之间插入一个对象到字符串转换器。

谢谢您的回复,Gary。我都将配置更改为入站通道适配器,并将服务器端更改为推送数据,以crlf分隔,但累加器中仍然没有任何内容。消息只是不调用upNow它正在工作:)。我对我的配置做了一些更改,以使用我自己的数据转换器(从字节生成字符串)并将spit结果保存到累加器,然后我的应用程序开始工作。我想,我最大的错误是没有在消息之间添加分隔符,所以SpringAdapter根本无法读取消息。对于那些正在寻找配置的用户,这里是pastebin。配置(xml),java类:
public class Accumulator {

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

}