【发布时间】:2014-12-11 12:51:42
【问题描述】:
我是 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 - 在命名空间“appServlet-servlet”的 WebApplicationContext 中发布事件: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 - 在命名空间“appServlet-servlet”的 WebApplicationContext 中发布事件: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 - 在命名空间“appServlet-servlet”的 WebApplicationContext 中发布事件: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();
【问题讨论】:
标签: spring sockets tcp integration