当前位置: 首页 > 知识库问答 >
问题:

从Hazelcast反序列化synchronizedSet时的Stackoverflow

谭锐藻
2023-03-14

考虑以下代码片段,以获取/提交collections.synchronizedSet到/从Hazelcast:

IMap imap = getIMapFromHazelcastClient();
Set<UUID> mySet = (Set<UUID>)imap.get("someKey"); //This may trigger a stackOverflow, see below.
if ( mySet == null ){
    mySet = Collections.synchronizedSet(new HashSet<UUID>());
}else{
    mySet = Collections.synchronizedSet(mySet);
}

//Concurrently add/remove elements from mySet

//After all concurrent operations complete, store the updated Set to Hazelcast.
//mySet should contain about 1-1.5K UUIDs at this point, every time.
imap.set("someKey", mySet);

在此代码运行几次之后(不确定有多少次,也不确定为什么),在试图从Hazelcast(上面标记的)获取集合时,会触发以下操作:

java.lang.StackOverflowError
at java.base/java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2914)
at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2930)
at java.base/java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(ObjectInputStream.java:3439)
at java.base/java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:3497)
at java.base/java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1242)
at java.base/java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:800)
at java.base/java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:991)
at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2017)
at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1895)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2202)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:519)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:477)
at java.base/java.util.HashSet.readObject(HashSet.java:344)
at java.base/jdk.internal.reflect.GeneratedMethodAccessor564.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1226)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2401)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2235)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712)

...About 900 lines of these
at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2540)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2434)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2235)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712)

at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2540)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2434)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2235)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712)
at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2540)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2434)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2235)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:519)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:477)
at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:84)
at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:77)
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:187)
at com.hazelcast.client.spi.ClientProxy.toObject(ClientProxy.java:102)
at com.hazelcast.client.proxy.ClientMapProxy.get(ClientMapProxy.java:318)
...
imap.set("someKey", new HashSet(mySet));

共有1个答案

秦钟展
2023-03-14

在使用Hazelcast 4.2-beta-1之前,这不会为我重新创建完整的代码。这与您的设置有何不同?

    public static void main(String[] args) throws Exception {
        String mapName = "so66579138";
        String key = "k";

        Config config = new Config();
        config.getNetworkConfig().getJoin().getAutoDetectionConfig().setEnabled(false);
        config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);

        HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config);
        
        IMap<String, Set<UUID>> imap = hazelcast.getMap(mapName);
        
        Set<UUID> mySet = Collections.synchronizedSet(new HashSet<UUID>());
        for (int i = 0 ; i < 1500 ; i++) {
            mySet.add(UUID.randomUUID());
        }

        // PUT
        imap.set(key, mySet);
        
        // GET
        Set<UUID> mySet2 = imap.get(key);
        System.out.println("mySet2.size() == " + mySet2.size());
        System.out.println("mySet2.getClass().getName() == " + mySet2.getClass().getName());
        
        hazelcast.shutdown();
    }

 类似资料:
  • 错误: java.lang.ClassNotFoundException:testprocedure.tp$3在java.net.URLClassLoader$1上运行(未知源)在java.net.URLClassLoader上运行(未知源)在java.security.accessController.doprivileged(本机方法)在java.net.URLClassLoader.find

  • 我正在尝试使用kryo序列化和反序列化到二进制。我想我已经完成了序列化,但似乎无法反序列化。下面是我正在处理的代码,但最终我想存储一个字节[],然后再次读取它。文档只显示了如何使用文件。

  • 我想序列化一个Book对象: 问题是我不知道如何实例化ObjectDataOutput/ObjectDataInPut类型的对象来序列化/反序列化Book对象。

  • 您好,我猜客户端和成员配置有问题,我应该使用哪个配置,正如您所看到的,当我调用get_data时,它返回时没有问题,但当我尝试使用谓词sql时,它会给我错误消息“hazelcast.errors.HazelcastSerializationError:服务器异常:com.hazelcast.nio.serialization.HazelcastSerializationException:没有适合

  • 上下文:我们使用Activiti作为流程引擎,使用Activiti-Rest作为应用程序的接口。由于这个问题与返回由Java序列化的对象的REST服务有关,所以我没有将其添加到标题中。 场景:我们有一个JBoss Wildfly实例,它包含一个EAR和一个包含类“ProcessContext”的模块(为了参考,我们将其称为X)。Activiti在这个EAR中运行,ServiceTasks(从进程中

  • I'va是一个OID接口,可以由许多具体类型实现: 现在我有一个具有两个字段的对象,一个使用抽象接口类型(OID)定义,另一个使用具体类型(MyOID)定义 我想使用jackson以不同的方式序列化/反序列化字段,无论它们是使用抽象接口类型还是具体类型定义的: 注意,被序列化,包括类型信息(多态序列化),而被序列化为文本 为此,我将OID接口注释为: 并为每个具体类型分配了类型id: 最后,对容器