0

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

RichardFeynman
  • 478
  • 1
  • 6
  • 16
  • We'll need to see a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – M-Chen-3 Feb 26 '21 at 22:06
  • 1
    @M-Chen-3 added examples for both setups. –  RichardFeynman Feb 27 '21 at 00:40
  • Try setting current thread class loader before starting client: https://stackoverflow.com/a/4096399/36498 – alamar Feb 27 '21 at 10:24
  • I tried setting the class loader in the thread either before creating the IgniteClinet and or before attempting to retive my object but consitently get the same error. java.lang.ClassCastException: class com.myCompany.MyClass cannot be cast to class com.myCompnay.MyClass (com.myCompany.MyClass is in unnamed module of loader java.net.URLClassLoader @67784306; com.myCompany.MyClass is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @475835b1) –  RichardFeynman Mar 01 '21 at 15:54

1 Answers1

1

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

RichardFeynman
  • 478
  • 1
  • 6
  • 16