java - 如何将ByteArrayRawSerializer的messageSize设置为无限制?

标签 java spring spring-integration

我使用ByteArrayRawSerializer作为套接字消息反序列化器。 消息结束总是由服务器关闭套接字来指示。

由于消息可能很大,我想将序列化器的消息大小定义为无限。但如何呢?

以下情况会导致缓冲区溢出错误:

ByteArrayRawSerializer s = new ByteArrayRawSerializer();
s.setMaxMessageSize(Integer.MAX_VALUE);

最佳答案

使用如此巨大的缓冲区大小是完全不切实际的,每个新请求都会尝试分配 > 2Gb 的内存。

您需要使用更合理的大小,足以处理您预期的消息大小。

或者,创建一个自定义反序列化器,根据需要分配更多缓冲区。

编辑

这是一个弹性原始反序列化器...

/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.integration.ip.tcp.serializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.core.serializer.Deserializer;
import org.springframework.util.StreamUtils;

/**
 * A deserializer that uses an elastic {@link ByteArrayOutputStream}
 * instead of a fixed buffer. Completion is indicated by the sender
 * closing the socket.
 *
 * @author Gary Russell
 * @since 5.0
 *
 */
public class ByteArrayElasticRawDeserializer implements Deserializer<byte[]> {

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamUtils.copy(inputStream, out);
        out.close();
        return out.toByteArray();
    }

}

关于java - 如何将ByteArrayRawSerializer的messageSize设置为无限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44328321/

相关文章:

java - 使用 Spring Web 服务客户端解密消息

java - 如何读取每行都有一个数字的文件java

tcp - Spring Integration tcp客户端多连接

java - 如何消除 Windows 7 中丑陋的 SWT 按钮边缘?

java - 类型PreparedStatement中的错误: The method setString(int, String)不适用于参数(int,Object)

.net - Oracle.DataAccess.Client 错误

SpringIntegration : Service-Activator not invoking method

log4j - Spring集成中限制jsch的输出

java - 从多个客户端更新服务器上对象的模式/最佳实践

java - 词类的在线(最好)查找 API