Felix framework

The framework is based on the OSGi pattern and therefore has problems integrating many dependencies, such as the class loading failure encountered in this article:

Problem screenshots

You can see is org. Apache. Hadoop. HDFS. DistributedFileSystem loading failure

The debugger can query this class using the following method:

CACHE_CLASSES.put(classLoader, map);

The loaded class will be put into the corresponding cache object. The information in the cache object is related to the classLoader, so it will fail to get the information when it is loaded later. The loader of the referenced object is loaded with BundleClassLodaerJava5.


why

By querying the data, we learned that each felix framework bundle has its own class loader. The name of this class loader can be obtained by using the code:

getClass().getClassLoader();

You can also see this using debugging tools such as visualVM in the screenshot:


The solution

If you look at the code logic, you can see that the classes looked up in Hadoop are cached in a CACHE_CLASSES object, which is cached depending on the class load, so either change this part of the source code logic, or have a new classloader be changed every time the class is loaded: BundleClassLodaerJava5.

So here we create the Hadoop configuration class by manually changing it to the class loader of the current class, and then we debugger

BundleClassLodaerJava5 has a class cache for BundleClassLodaerJava5:

Conclusion:

Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
Copy the code