2017-06-02 23 views
0

我使用ByteArrayRawSerializer作爲套接字消息解串器。 消息結束始終由關閉套接字的服務器指示。如何將ByteArrayRawSerializer的messageSize設置爲無限制?

由於消息可能很大,我想定義無限制的序列化器的消息大小。但是如何?

下導致緩衝區溢出錯誤:

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

我不是天才,但是這是我的幫助.. https://stackoverflow.com/q/479701/5204909 –

回答

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(); 
    } 

} 
+0

所以是不是可以初始化具有無限消息大小的'ByteArrayRawSerializer'?其實我以前不知道郵件大小。 – membersound

+0

Java語言只會允許一個'Integer.MAX_VALUE'的緩衝區 - 即使只有當你有足夠的內存時。 JVM進一步限制了「線程中的異常」主「java.lang.OutOfMemoryError:請求的數組大小超過VM限制」 –

+0

原始解串器很簡單;你需要一個更復雜的分配內存(隨着更多的字節到達)。 –

相關問題