我使用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/