ach*_*yya 2 sockets integration spring tcp
我是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>
Run Code Online (Sandbox Code Playgroud)
CustomSerializer:
public class CustomSerializer extends ByteArraySingleTerminatorSerializer {
public CustomSerializer() {
super((byte) 0x03);
System.out.println("In Custom Serializer...");
}
}
Run Code Online (Sandbox Code Playgroud)
PortService:
@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";
}
}
Run Code Online (Sandbox Code Playgroud)
我启用了日志跟踪:
该控件将转到CustomSerializer,而不是PortService,将非常感谢您的帮助。
跟踪:org.springframework.web.context.support.XmlWebApplicationContext-在WebApplicationContext中为名称空间“ 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
跟踪:org.springframework.web.context.support.XmlWebApplicationContext-在WebApplicationContext中为名称空间“ 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]已关闭
跟踪:org.springframework.web.context.support.XmlWebApplicationContext-在WebApplicationContext中为名称空间“ appServlet-servlet”发布事件:TcpDeserializationExceptionEvent [source = com.gerrydevstory.service.CustomSerializer @ 4833ff0b,cause = java.io.IOException:套接字已关闭在消息组装过程中]
当我尝试使用以下代码片段测试相同内容时,它起作用了:
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();
Run Code Online (Sandbox Code Playgroud)
由于数据是从嵌入式系统中推送的,因此流为原始格式(字节数组)
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"
/>
Run Code Online (Sandbox Code Playgroud)
解决了问题。
谢谢。
| 归档时间: |
|
| 查看次数: |
2231 次 |
| 最近记录: |