6
votes

Am running a OSGI plugin project (with N no of bundles), I have integrated hazel cast in it, i. When I try to add the data as map of string and test bean, It is getting added in hazel cast map but ! ii. When I try to get the map, Its showing an error as follows,

com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.test.Data cannot be found by com.eclipsesource.jaxrs.jersey-all_2.10.1.SR1

FULL STACK TRACE

com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.test.Data cannot be found by com.eclipsesource.jaxrs.jersey-all_2.10.1.SR1 at com.hazelcast.nio.serialization.DefaultSerializers$ObjectSerializer.read(DefaultSerializers.java:201) at com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSerializerAdapter.java:63) at com.hazelcast.nio.serialization.SerializationServiceImpl.readObject(SerializationServiceImpl.java:285) at com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:262) at com.hazelcast.client.spi.ClientProxy.toObject(ClientProxy.java:156) at com.hazelcast.client.proxy.ClientMapProxy.values(ClientMapProxy.java:629) at com.test.commons.db.hazelcast.HazelCastDao.fetchingMapInstance(HazelCastDao.java:126) at com.test.services.agent.dao.DataDao.getData(DataDao.java:79) at com.test.services.agent.eapi.ExecuteEAPI.executePOSTRequest(ExecuteEAPI.java:105) at com.test.services.ztp.BootStrapLoaderService.getZTPConfigDetails(BootStrapLoaderService.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104) at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103) at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:267) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221) at org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(ServletRegistration.java:61) at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:128) at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:68) at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) at org.eclipse.equinox.http.jetty.internal.HttpServerManager$InternalHttpServiceServlet.service(HttpServerManager.java:386) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:229) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) at org.eclipse.jetty.server.Server.handle(Server.java:370) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494) at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:722) Caused by: java.lang.ClassNotFoundException: com.test.data cannot be found by com.eclipsesource.jaxrs.jersey-all_2.10.1.SR1 at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421) at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412) at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at com.hazelcast.nio.ClassLoaderUtil.tryLoadClass(ClassLoaderUtil.java:124) at com.hazelcast.nio.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:113) at com.hazelcast.nio.IOUtil$1.resolveClass(IOUtil.java:122) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1610) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1515) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1348) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370) at com.hazelcast.nio.serialization.DefaultSerializers$ObjectSerializer.read(DefaultSerializers.java:196) ... 60 more

4
Have you added the bundle ClassLoader to the Hazelcast Config instance before creating HazelcastInstance itself? - noctarius
noctarius, I didnt get you! Please explain me - Harry
It complains about com.test.Data not found, If I were you I would start looking from there like, Is Data class in classpath? - Anuj Patel
When creating a HazelcastInstance using Hazelcast::newHazelcastInstance you can give it a Config instance as a parameter. If you don't do that the Config instance is created from classpath available config files. XmlConfigBuilder can create you a Config instance from those files, too. After you got the Config instance you call Config::setConfigClassLoader and set it to your bundle classloader. - noctarius
Thanks noctarius, It worked... Awesome buddy - Harry

4 Answers

3
votes

When creating a HazelcastInstance using Hazelcast::newHazelcastInstance you can give it a Config instance as parameter. If you don't do that the Config instance is created from classpath-available config files. XmlConfigBuilder can create Config from those files, too.

After you got the Config instance you call Config::setConfigClassLoader and set it to your bundle classloader.

Config config = new XmlConfigBuilder().build();
config.setClassLoader(getBundleClassLoader());
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
0
votes

I had the same issue with <version>3.5.1</version> Upgrading the hazelcast-all version to <version>3.12.1</version> solved my issue.

0
votes

I also got the same exception when i was trying to cast map value to my cacheEntity like given below.

IMap<Object, UserCacheEntity> imap = hzClient.getMap("userMap");
System.out.println(imap.size());

Collection<UserCacheEntity> coll = imap.values();
coll.forEach(entity -> {
    System.out.println(entity);
});

In my case(hazelcast version 3.12.4.), workaround was to keep UserCacheEntity in same package name as it was in cacheServer. i.e, make sure your entity package not changes. It should be same package in client as it was declared in cache server.

-1
votes

The solution in Harry's links are working. I don't think that's the proper way to do that but I can assure that it's working.

I'll summarize the useful part in the link. This happens due to the class loader picked by Hazelcast, so you can fix it by temporally replacing the class loader of Thread.currentThread() with the class loader of your class. Code sample as below.

// Cache the current context class loader
// We can't be sure no-one is using it, so only set it for the hazelcast specific stuff
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
// Get the classloader of a class from inside the bundle
ClassLoader classLoader = this.getClass().getClassLoader();
// Set it as context class loader
Thread.currentThread().setContextClassLoader(classLoader);

// Hazelcast stuff here

// Reset the context class loader to the cached loader
Thread.currentThread().setContextClassLoader(ccl);

Thanks