java - TcpInboundGateway - 读取特定数量的字节

标签 java spring tcp spring-integration

我应该如何实现服务激活器来读取特定数量的字节?

我的 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 集成的服务器发送消息?

最佳答案

你应该配置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.

关于java - TcpInboundGateway - 读取特定数量的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829836/

相关文章:

设置路径后仍无法识别java

java - GlassFish 4 无法部署

java - 使用java发送邮件时出现异常

sockets - 检测何时TCP连接无法打开或终止

c - 保留的 TCP/IP 端口

Java - super 调用问题

java - jclouds - 对 openstack-swift 使用身份验证 v1?

c++ - Boost asio async_read_until 然后是 async_read

java - 以最简单的方式使用 OneToMany 的 LazyInitializationException

java - Spring MVC Rest 客户端获取 HttpClientErrorException : 404 null