草庐IT

spring 集成 tcp 套接字正在立即关闭

coder 2023-09-18 原文

我是 Spring 集成和 tcp ip 模块的新手,我需要一些帮助。

我正在构建一个简单的项目,我应该从一个端口读取数据,一个外部源(嵌入式系统)将一些原始数据推送到指定的端口地址(在这个例子中端口是 4321)

数据样本是这样的: $1,101,16,10,14,01,32,05,343N,0987E,000.0,301,0,A#$1,101,16,10,14,01,32,05,343N,0987E,000.0,301,0, A#

设备有数据就推送,一次发送甚至超过600个字符,

我想访问 portService 类和测试方法中的数据,我有一个分隔符 #,因为每条消息都以 # 结尾,我无法控制客户端数据(我无法更改数据格式,或者我无法将 \r\n 附加到它)

tcp 客户端服务器配置:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">

<bean id="customSerializer" class="com.gerrydevstory.service.CustomSerializer" />

<int-ip:tcp-connection-factory id="serverConnectionFactory" 
    type="server" 
    host="localhost" 
    port="4321" 
    single-use="false"
    so-timeout="100000"
    using-nio="true"
    serializer="customSerializer"
    deserializer="customSerializer"/>

<int-ip:tcp-inbound-gateway id="gatewayCrLf"
    connection-factory="serverConnectionFactory" request-channel="loop" />

<int:channel id="loop" />

<int:service-activator input-channel="loop"
    ref="portService" method="test">
</int:service-activator>

自定义序列化器:

public class CustomSerializer extends ByteArraySingleTerminatorSerializer {

    public CustomSerializer() {
        super((byte) 0x03);
        System.out.println("In Custom Serializer...");
    }
}

端口服务:

@Component
public class PortService {

    public String test(final String input) {
        System.out.println("PortService :" + input);
        if ("FAIL".equals(input)) {
            throw new RuntimeException("Failure Demonstration");
        }
        return input + ":echo";
    }
}

我启用了日志记录跟踪:

控件将转到 CustomSerializer 而不是 PortService,我们将不胜感激任何帮助。

TRACE: org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'appServlet-servlet': TcpConnectionOpenEvent [source=org.springframework.integration.ip.tcp.connection.TcpNioConnection@13c2d58], [factory=serverConnectionFactory, connectionId=xxx.xxxx.xx.xx:9226:4321:e63ea33e-a9e7-416a-bfd6-dc53d5de00c6] OPENED

TRACE: org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'appServlet-servlet': TcpConnectionCloseEvent [source=org.springframework.integration.ip.tcp.connection.TcpNioConnection@13c2d58], [factory=serverConnectionFactory, connectionId=xxx.xxxx.xx.xx:9226:4321:e63ea33e-a9e7-416a-bfd6-dc53d5de00c6] CLOSED

TRACE: org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'appServlet-servlet': TcpDeserializationExceptionEvent [source=com.gerrydevstory.service.CustomSerializer@4833ff0b, cause=java.io.IOException: Socket closed during message assembly]

当我尝试使用以下代码片段测试相同的东西时,它起作用了:

server = new ServerSocket(port);
int index = 0;
while (true) {
    System.out.println("Waiting for client request");
    Socket socket = server.accept();
    InputStream ois = socket.getInputStream();
    byte[] buf = new byte[1024];
    ois.read(buf);
    System.out.write(buf);

    System.out.println("Message Received: " + buf);
    socket.close();
    if (index++ > 5) {
        break;
    }
}
server.close();

最佳答案

由于数据是从嵌入式系统推送的,流是原始格式(字节数组)

ByteArrayRawSerializer 可以处理这种用例。

我添加了以下代码片段配置文件:

<bean id="byteArrayRawSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer" />

<int-ip:tcp-connection-factory id="serverConnectionFactory" 
    type="server" 
    host="localhost" 
    port="4321" 
    single-use="false"
    using-nio="false"
    so-timeout="300000"
    serializer="byteArrayRawSerializer"
    deserializer="byteArrayRawSerializer"
    />

解决了问题。

谢谢。

关于spring 集成 tcp 套接字正在立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26399587/

有关spring 集成 tcp 套接字正在立即关闭的更多相关文章

  1. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  2. ruby-on-rails - 如何使辅助方法在 Rails 集成测试中可用? - 2

    我在app/helpers/sessions_helper.rb中有一个帮助程序文件,其中包含一个方法my_preference,它返回当前登录用户的首选项。我想在集成测试中访问该方法。例如,这样我就可以在测试中使用getuser_path(my_preference)。在其他帖子中,我读到这可以通过在测试文件中包含requiresessions_helper来实现,但我仍然收到错误NameError:undefinedlocalvariableormethod'my_preference'.我做错了什么?require'test_helper'require'sessions_hel

  3. ruby-on-rails - 我如何将 Hoptoad 与 DelayedJob 和 DaemonSpawn 集成? - 2

    我一直很高兴地使用DelayedJob习惯用法:foo.send_later(:bar)这会调用DelayedJob进程中对象foo的方法bar。我一直在使用DaemonSpawn在我的服务器上启动DelayedJob进程。但是...如果foo抛出异常,Hoptoad不会捕获它。这是任何这些包中的错误...还是我需要更改某些配置...或者我是否需要在DS或DJ中插入一些异常处理来调用Hoptoad通知程序?回应下面的第一条评论。classDelayedJobWorker 最佳答案 尝试monkeypatchingDelayed::W

  4. ruby - 如何关闭 ruby​​ gem "Spreadsheet?"中的文件 - 2

    下面的代码在我第一次运行它时就可以正常工作:require'rubygems'require'spreadsheet'book=Spreadsheet.open'/Users/me/myruby/Mywks.xls'sheet=book.worksheet0row=sheet.row(1)putsrow[1]book.write'/Users/me/myruby/Mywks.xls'当我再次运行它时,我会收到更多消息,例如:/Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:11

  5. 网络编程套接字 - 2

    网络编程套接字网络编程基础知识理解源`IP`地址和目的`IP`地址理解源MAC地址和目的MAC地址认识端口号理解端口号和进程ID理解源端口号和目的端口号认识`TCP`协议认识`UDP`协议网络字节序socket编程接口`sockaddr``UDP`网络程序服务器端代码逻辑:需要用到的接口服务器端代码`udp`客户端代码逻辑`udp`客户端代码`TCP`网络程序服务器代码逻辑多个版本服务器单进程版本多进程版本多线程版本线程池版本服务器端代码客户端代码逻辑客户端代码TCP协议通讯流程TCP协议的客户端/服务器程序流程三次握手(建立连接)数据传输四次挥手(断开连接)TCP和UDP对比网络编程基础知识

  6. jenkins部署1--jenkins+gitee持续集成 - 2

    前置步骤我们都操作完了,这篇开始介绍jenkins的集成。话不多说,看操作1、登录进入jenkins后会让你选择安装插件,选择第一个默认的就行。安装完成后设置账号密码,重新登录。2、配置JDK和Git都需要执行路径,所以需要先把执行路径找到,先进入服务器的docker容器,2.1JDK的路径root@69eef9ee86cf:/usr/bin#echo$JAVA_HOME/usr/local/openjdk-82.2Git的路径root@69eef9ee86cf:/#whichgit/usr/bin/git3、先配置JDK和Git。点击:ManageJenkins>>GlobalToolCon

  7. spring.profiles.active和spring.profiles.include的使用及区别说明 - 2

    转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev

  8. ruby - 是否可以在不实际发送或读取数据的情况下查明 ruby​​ 套接字是否处于 ESTABLISHED 或 CLOSE_WAIT 状态? - 2

    s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成

  9. ruby - 我正在学习编程并选择了 Ruby。我应该升级到 Ruby 1.9 吗? - 2

    我完全不是程序员,正在学习使用Ruby和Rails框架进行编程。我目前正在使用Ruby1.8.7和Rails3.0.3,但我想知道我是否应该升级到Ruby1.9,因为我真的没有任何升级的“遗留”成本。缺点是什么?我是否会遇到与普通gem的兼容性问题,或者甚至其他我不太了解甚至无法预料的问题? 最佳答案 你应该升级。不要坚持从1.8.7开始。如果您发现不支持1.9.2的gem,请避免使用它们(因为它们很可能不被维护)。如果您对gem是否兼容1.9.2有任何疑问,您可以在以下位置查看:http://www.railsplugins.or

  10. ruby-on-rails - Ruby 的 'open_uri' 是否在读取或失败后可靠地关闭套接字? - 2

    一段时间以来,我一直在使用open_uri下拉ftp路径作为数据源,但突然发现我几乎连续不断地收到“530抱歉,允许的最大客户端数(95)已经连接。”我不确定我的代码是否有问题,或者是否是其他人在访问服务器,不幸的是,我无法真正确定谁有问题。本质上,我正在读取FTPURI:defself.read_uri(uri)beginuri=open(uri).readuri=="Error"?nil:urirescueOpenURI::HTTPErrornilendend我猜我需要在这里添加一些额外的错误处理代码...我想确保我采取一切预防措施来关闭所有连接,这样我的连接就不是问题所在,但是我

随机推荐