我应该如何实现服务激活器来读取特定的字节数?
我的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>
我的留言处理程序,
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;
}
}
我想读取前4个字节并确定传入的消息长度(从前4个字节开始),然后读取长度由前4个字节指定的消息字节。
添加反序列化器后,我得到以下异常。虽然我发送长度为161的字节数组。
final byte[] data = new byte[] { 2,....};
异常,
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
客户端是否应该基于spring集成才能向基于Spring集成的服务器发送消息?
答案 0 :(得分:1)
您应该在ByteArrayLengthHeaderSerializer
定义上将deserializer
配置为<int-ip:tcp-connection-factory>
。
有关详细信息,请参阅其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.