[英]TcpInboundGateway - Read specific number of bytes
How should i implement service activator to read specific number of bytes ?我应该如何实现服务激活器来读取特定数量的字节?
My context.xml,我的context.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="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">
<int:annotation-config />
<context:component-scan base-package="com.spring.integration.tcp" />
<bean id="tcpDeserializer"
class="org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer">
<property name="maxMessageSize" value="300" />
</bean>
<int-ip:tcp-connection-factory id="tcpConnectionFactory" type="server" port="9070" using-nio="true" deserializer="tcpDeserializer" />
<int:channel id="tcpRequestChannel" />
<int-ip:tcp-inbound-gateway id="tcpInboundGateway" connection-factory="tcpConnectionFactory" request-channel="tcpRequestChannel" />
<bean id="messageHandler" class="com.spring.integration.tcp.MessageHandler" />
<int:service-activator id="tcpServiceActivator" input-channel="tcpRequestChannel" ref="messageHandler" method="receiveAndSend" />
</beans>
My message handler,我的消息处理程序,
package com.spring.integration.tcp;
import java.io.InputStream;
import java.util.Arrays;
public class MessageHandler
{
public byte[] receiveAndSend(final InputStream inputStream) throws Exception
{
int bytesToRead = 50;
final byte[] requestMessageBytes = new byte[bytesToRead];
int read = 0;
while (bytesToRead > read)
{
final int c = inputStream.read(requestMessageBytes, read, bytesToRead - read);
if (c == -1)
{
throw new Exception("EOF");
}
read += c;
}
System.out.println("server received - length [" + requestMessageBytes.length + "] bytes " + Arrays.toString(requestMessageBytes));
final byte[] responseMessageBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
System.out.println("server sending - " + Arrays.toString(responseMessageBytes));
return responseMessageBytes;
}
}
I would like to read first 4 bytes and determine the incoming message length(from first 4 bytes) and then read message bytes whose length is specified by first 4 bytes.我想读取前 4 个字节并确定传入消息长度(从前 4 个字节开始),然后读取长度由前 4 个字节指定的消息字节。
After adding deserializer I get the following exception.添加解串器后,我得到以下异常。 Though i send a byte array of length 161.
虽然我发送了一个长度为 161 的字节数组。
final byte[] data = new byte[] { 2,....};
Exception,例外,
Sep 28, 2015 7:03:28 PM org.springframework.integration.ip.tcp.connection.TcpNetConnection handleReadException
SEVERE: Read exception localhost:54756:9070:a580527b-95bd-42c7-b1cc-e0726b433199 IOException:Message length 38159362 exceeds max message length: 300
Should the client be based on spring integration to be able to send a message to spring integration based server ?客户端是否应该基于 spring 集成才能向基于 spring 集成的服务器发送消息?
You should configure ByteArrayLengthHeaderSerializer
as a deserializer
on the <int-ip:tcp-connection-factory>
definition.您应该在
<int-ip:tcp-connection-factory>
定义上将ByteArrayLengthHeaderSerializer
配置为deserializer
。
See its JavaDocs for more info:有关更多信息,请参阅其 JavaDocs:
* The default length field is a 4 byte signed integer. During deserialization,
* negative values will be rejected.
* Other options are an unsigned byte, and unsigned short.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.