Java 无法从IgniteCach检索对象,引发ClassNotFoundException

Java 无法从IgniteCach检索对象,引发ClassNotFoundException,java,tomcat,ignite,Java,Tomcat,Ignite,我试图将Ignite添加到现有的ApacheTomcat应用程序中,但在检索之前插入Ignite缓存的数据时遇到了问题 下面是一个有问题的代码模型 public static MyClass getInstance(){ Ignite ig = WSUtil.getIgnite(); IgniteCache<Integer,MyClass> myCache = ig.getOrCreateCache("MyClass"); MyClass

我试图将Ignite添加到现有的ApacheTomcat应用程序中,但在检索之前插入Ignite缓存的数据时遇到了问题

下面是一个有问题的代码模型

public static MyClass getInstance(){
    Ignite ig = WSUtil.getIgnite();
    IgniteCache<Integer,MyClass> myCache = ig.getOrCreateCache("MyClass");
    MyClass instance = myCache.get( 0 );
    if(instance == null) {
        try{
            instance =  //IO opperations to create instance 
            myCache.put(0, instance);
        }catch(Exception e) {
            log.error("\tMessage: "+e.getMessage());
            throw e;
        }finally {
            //close IO
        }
        
    }
    return instance; 
}
我已经尝试过在我的工作站上运行Ignite服务器的简单Person对象上实现类似的功能,没有任何问题

public static void main (String[] args) throws IgniteException{
        //preparing igniteconfig
        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setClientMode(true);
        cfg.setPeerClassLoadingEnabled(true);
        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
        ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
        Ignite ignite = Ignition.start(cfg);
        
        IgniteCache <Integer, Person> personCache = ignite.getOrCreateCache("personCache");
        Person p = new Person("Billy","Jean");
        personCache.put(0, p);
        
        Person p2 = personCache.get(0);
        
        System.out.println("p: " + p.toString());
        System.out.println("p2: " + p2.toString());
        
        System.out.println(">> task exectued, check output");
        ignite.close();
}
publicstaticvoidmain(字符串[]args)引发异常{
//准备igniteconfig
IgniteConfiguration cfg=新IgniteConfiguration();
setClientMode(true);
cfg.setPeerClassLoadingEnabled(真);
TcpDiscoveryMulticastIpFinder ipFinder=新的TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList(“127.0.0.1:47500..47509”);
setDiscoverySpi(新的TcpDiscoverySpi().setIpFinder(ipFinder));
点火=点火启动(cfg);
IgniteCache personCache=ignite.getOrCreateCache(“personCache”);
人员p=新人员(“比利”、“吉恩”);
personCache.put(0,p);
Person p2=personCache.get(0);
System.out.println(“p:+p.toString());
System.out.println(“p2:+p2.toString());
System.out.println(“>>已执行任务,检查输出”);
点燃。关闭();
}

很可能Ignite和您的代码是从两个不同的类加载器加载的(正如您所指出的),例如,Apache Ignite可以从公共库目录加载,而用户代码可以从特定的web应用目录加载。在这种情况下,ApacheIgnite可能看不到您的库


在启动Ignite节点之前,您可以尝试将
IgniteConfiguration.classLoader
属性设置为例如
getClass().getClassLoader()

如果在
get(0)
行之前添加
Class.forName(“com.MyClass”)
会产生什么效果?在调试器中,我可以看到它的计算结果是:
Class(com.MyClass)
,在阅读了更多的内容之后,通过将toString评估为
class com.MyClass
,我已经确保在客户端和服务器上都打开了对等类加载(它是)。尽管如此,我还是将包含MyClass的已编译JAR添加到了ii/apache-ignite-2.9.1-bin/libs中,但在尝试从缓存中提取值时仍然会出错。对等类加载不会对等加载键值类。请提供异常的完整堆栈跟踪。@alamar编辑了这篇文章,以包含完整的清理堆栈跟踪。非常感谢您,对于任何偶然发现这一点的人,这正是我添加的语法:cfg.setClassLoader(MyClass.class.getClassLoader());
public static void main (String[] args) throws IgniteException{
        //preparing igniteconfig
        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setClientMode(true);
        cfg.setPeerClassLoadingEnabled(true);
        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
        ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
        Ignite ignite = Ignition.start(cfg);
        
        IgniteCache <Integer, Person> personCache = ignite.getOrCreateCache("personCache");
        Person p = new Person("Billy","Jean");
        personCache.put(0, p);
        
        Person p2 = personCache.get(0);
        
        System.out.println("p: " + p.toString());
        System.out.println("p2: " + p2.toString());
        
        System.out.println(">> task exectued, check output");
        ignite.close();
}