我是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>
CustomSerializer:
public class CustomSerializer extends ByteArraySingleTerminatorSerializer {
public CustomSerializer() {
super((byte) 0x03);
System.out.println("In Custom Serializer...");
}
}
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";
}
}
我启用了日志记录跟踪:
控件将转到CustomSerializer而不是PortService,任何帮助都将受到高度赞赏。
TRACE: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] 已打开
TRACE: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] 已关闭
TRACE: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();
答案 0 :(得分:5)
由于数据是从嵌入式系统推送的,因此流采用原始格式(字节数组)
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"
/>
解决了这个问题。
感谢。