简体   繁体   中英

Why apache ignite is not deserializing the java.util.Collections$SetFromMap?

Apache ignite is throwing below exception when i am trying to put data into cache,

Caused by: class org.apache.ignite.IgniteCheckedException: Failed to deserialize object with given class loader: [clsLdr=org.apache.ignite.internal.processors.cache.GridCacheDeploymentManager$CacheClassLoader@5d5b5fa7, err=Failed to deserialize object [typeName=java.util.Collections$SetFromMap]]
    Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.apache.ignite.internal.marshaller.optimized.OptimizedObjectInputStream.readSerializable(OptimizedObjectInputStream.java:608)
        ... 43 more
    Caused by: java.lang.ClassNotFoundException: com.example.springapachecache.HelloWorld$1

Program:

 package com.example.springapachecache;

 import org.apache.ignite.Ignite;
 import org.apache.ignite.IgniteCache;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.Ignition;
 import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
 import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;

 import java.util.*;

 public class HelloWorld {
 public static void main(String[] args) throws IgniteException {
    // Preparing IgniteConfiguration using Java APIs
    IgniteConfiguration cfg = new IgniteConfiguration();

    // The node will be started as a client node.
    cfg.setClientMode(true);

    // Classes of custom Java logic will be transferred over the wire from this app.
    cfg.setPeerClassLoadingEnabled(true);

    // Setting up an IP Finder to ensure the client can locate the servers.
    TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
    ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
    cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));

    // Starting the node
    Ignite ignite = Ignition.start(cfg);
    IgniteCache<Integer, Set<String>> setCache = ignite.getOrCreateCache("mySetCache");
    System.out.println("setCache before: "+setCache.get(1));
    Set<String> meetingUuids = Collections.newSetFromMap(new LinkedHashMap<String, Boolean>() {
        // Set backed by LRU map.
        private static final long serialVersionUID = -8251614405342939979L;
        @Override
        protected boolean removeEldestEntry(Map.Entry<String, Boolean> eldest) {
            return size() > 1000;
        }
    });

    meetingUuids.add("meetingId");
   //        Set<String> meetingUuidSet = new HashSet<>(meetingUuids);
   //        setCache.put(3, meetingUuidSet);
    setCache.put(1, meetingUuids);
    System.out.println("setCache after: "+setCache.get(1));

    // Disconnect from the cluster.
    ignite.close();
}

}

Constructing Set using simple linkedHashMap(Set meetingUuids = Collections.newSetFromMap(new LinkedHashMap<String, Boolean>()) works fine. Could you please help?

You hint at the problem in your last paragraph. You can store Sets in Ignite, and you can "peer class load" code over the.network. What you can't do is peer class load code used in your "model," that is, any class used to define your caches. In your sample code, that's the closure in Collections.newSetFromMap .

I think storing your custom Set in Ignite is a bit messy. You can't query collections using SQL, for instance. So the best solution would be to model your data differently.

If you really want to go this route, if you put a copy of your class on the server-side it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM