Search code examples
javaclassloaderignitethin-client

Is there a way to set an Ignite thin Client ClassLoader?


The Ignite thick client provides a method to set the class loader. I have successfully used this to avoid class not found exception when removing values from Ignite Caches. Otherwise I get conflicts with the class loader from my tomcat application. See example below:

IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setClassLoader(MyClass.class.getClassLoader());
Ignite ig = Ignition.start(cfg);
IgniteCache<Integer,MyClass> myCache = ig.getOrCreateCache("MyClass");
MyClass mc = myCache.get( 0 ); //throws java.lang.ClassNotFoundException without line 2

I am now trying to use a thin client which does not provide this method. Is there away way to configure the class loader for the thin client? Or is there some other way to avoid class not found exceptions when attempting to deserialize objects when removing them from the Ignite ClientCache?

ClientConfiguration cfg = new ClientConfiguration();
ClientIgnite ig = Ignition.startClient(cfg);
ClientCache<Integer,MyClass> myCache = ig.getOrCreateCache("MyClass");
MyClass mc = myCache.get( 0 ); //throws java.lang.ClassNotFoundException


Solution

  • After mulling this over I decided to try and solve this from the tomcat side rather than ignite. Below are the links helped me better understand this problem.

    In the end the issue was that the ignite classes were loaded by a class loader that was a parent to my web applications class loader.

    • On startup my webapp's class loader would check its parent's loader for my my key value pairs, not find them and then load them itself.
    • Next when I used the Ignite classes, the webapp's class would look in its parent class loader ( the common class loader), find that the
      parent had already loaded the classes, and use that class loader for the Ignite classes.
    • When the Ignite classes tried to convert my classes into binaries, it would look for the classes in its parent class loaders, not find them and load them itself.
    • When I tried to get values out of the cache I would then get a conflict because Ignite and Tomcat had each loaded the classes on their own.

    In the end I just added the ignite jars to WEB-INF/lib and then the ignite classes would be loaded by the webapp class loader, solving the whole problem.

    https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html https://www.mulesoft.com/tcat/tomcat-classpath