Search code examples
osgiclassloaderhazelcast

Hazelcast classloader configuration in OSGI environment?


I am currently using this configuration to set the classloader in my hazelcast instance.

 Config config = new XmlConfigBuilder(HAZELCAST_CONFIG).build();
 config.setInstanceName(HAZELCAST_INSTANCE_NAME);
 config.setClassLoader(MyClassA.class.getClassLoader());

This works for a map that has MyClassA elements.

Is it possible, having only one hazelcast instance, to configure the classloader in order to include all the classes of my project?

I am in an OSGI environment with multiple bundles.


Solution

  • The issue with having visibility to classes from multiple bundles is that different bundles can contain classes with matching names but different implementations. For example, bundle A could have class org.example.Foo version 1.0 but bundle B could have class org.example.Foo version 2.0.

    I recommend creating a bundle that defines exactly which types should be visible to Hazelcast. It does this simply by importing packages with controlled version ranges. You can now use the ClassLoader of this bundle with Hazelcast.

    Unfortunately there is not a standard way to directly access the ClassLoader of a bundle in OSGi. However you can in just a few lines of code write a ClassLoader that delegates to the Bundle.loadClass method from its loadClass method.