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

时间:2017-06-02 11:54:48

标签: java spring spring-integration

我正在使用ByteArrayRawSerializer作为套接字消息反序列化器。 消息结束始终由关闭套接字的服务器指示。

由于消息可能很大,我想无限制地定义序列化程序的消息大小。但是如何?

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

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

1 个答案:

答案 0 :(得分:1)

使用如此巨大的缓冲区大小是完全不切实际的,每个新请求都会尝试分配> 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();
    }

}