magicroomy
magicroomy

Reputation: 395

Hazelcast ClassNotFound using Near Cache in Client

I try to use Hazelcast (3.9.2, 3.11 no difference) in the following way: I got Hazelcast servers (members). I run them dedicated, not embedded. I do not want to teach the Hazelcast members the classes I want to store within them. I used the bundled hazelcast.xml file and did the following addon (3.9.2)

<replicatedmap name="default">
    <in-memory-format>BINARY</in-memory-format>
    <statistics-enabled>true</statistics-enabled>
</replicatedmap>

I also activated TCP, not Multicast (true/false) That is all changes I did. I started with one Member listening to 127.0.0.1:5701

Then I try to attach Hazelcast clients to the member for storing and retrieving Maps (Primarily ReplicatedMaps, but Maps also do not work in my scenario)

My Client Code looks like this (Cache is just a Serializable Class with no attributes):

public class Main {
  public static final String HAZELCAST_INSTANCE_NAME = "HAZI";
  public static final String REPLICATEDMAP_NAME = "REP_MAP";
  public static final String MAP_NAME = "NORMAL_MAP";

public static void main(String[] args) {
    init();
    HazelcastInstance instance = HazelcastClient.getHazelcastClientByName(HAZELCAST_INSTANCE_NAME);
    Map<String, Object> repMap = instance.getReplicatedMap(REPLICATEDMAP_NAME);
    repMap.put("MyKey", new Cache());
    System.err.println("Retrieve " + repMap.get("MyKey"));
    Map<String, Object> normalMap = instance.getReplicatedMap(MAP_NAME);
    normalMap.put("MyKey", new Cache());
    System.err.println("Retrieve " + normalMap.get("MyKey"));
    System.exit(1);
}

private static void init() {
    ClientConfig cfg = new ClientConfig();
    cfg.setInstanceName(HAZELCAST_INSTANCE_NAME);
    cfg.addNearCacheConfig(defineNearCache(REPLICATEDMAP_NAME));
    cfg.addNearCacheConfig(defineNearCache(MAP_NAME));
    // for analysis in the hazelcast management console
    cfg.getProperties().put("hazelcast.client.statistics.enabled", "true");
    cfg.getProperties().put("hazelcast.client.statistics.period.seconds", "60");
    cfg.getNetworkConfig().addAddress("127.0.0.1:5701");
    if (HazelcastClient.newHazelcastClient(cfg) == null) {
        System.err.println(" !!! ERROR in Cache Config !!!");
    }
}
private static NearCacheConfig defineNearCache(String mapName) {
    EvictionConfig evictionConfig = new EvictionConfig()
            .setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT)
            .setSize(200);
    return new NearCacheConfig()
            .setName(mapName)
            .setInMemoryFormat(InMemoryFormat.BINARY)
            .setInvalidateOnChange(true)
            .setEvictionConfig(evictionConfig);
}

}

My problem now is: Using this code I get a ClassNotFoundError trying put put things to the replicated map or regular map, but in the dedicated Hazelcast server (member), not on the client side.

    SCHWERWIEGEND: [127.0.0.1]:5701 [dev] [3.9.2] hz._hzInstance_1_dev.event-3 caught an exception while processing task:com.hazelcast.spi.impl.eventservice.impl.LocalEventDispatcher@eeed098
com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: de.empic.hazelwar.model.Cache
        at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:224)
        at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
        at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:185)
        at com.hazelcast.map.impl.DataAwareEntryEvent.getValue(DataAwareEntryEvent.java:90)
        at com.hazelcast.client.impl.protocol.task.replicatedmap.AbstractReplicatedMapAddEntryListenerMessageTask.handleEvent(AbstractReplicatedMapAddEntryListenerMessageTask.java:92)
        at com.hazelcast.client.impl.protocol.task.replicatedmap.AbstractReplicatedMapAddEntryListenerMessageTask.entryAdded(AbstractReplicatedMapAddEntryListenerMessageTask.java:132)
        at com.hazelcast.replicatedmap.impl.ReplicatedMapEventPublishingService.dispatchEvent(ReplicatedMapEventPublishingService.java:82)
        at com.hazelcast.replicatedmap.impl.ReplicatedMapService.dispatchEvent(ReplicatedMapService.java:247)
        at com.hazelcast.spi.impl.eventservice.impl.LocalEventDispatcher.run(LocalEventDispatcher.java:64)
        at com.hazelcast.util.executor.StripedExecutor$Worker.process(StripedExecutor.java:225)
        at com.hazelcast.util.executor.StripedExecutor$Worker.run(StripedExecutor.java:208)
Caused by: java.lang.ClassNotFoundException: de.empic.hazelwar.model.Cache
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.hazelcast.nio.ClassLoaderUtil.tryLoadClass(ClassLoaderUtil.java:173)
    at com.hazelcast.nio.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:147)
    at com.hazelcast.nio.IOUtil$ClassLoaderAwareObjectInputStream.resolveClass(IOUtil.java:591)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1868)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
    at com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$JavaSerializer.read(JavaDefaultSerializers.java:219)
    ... 10 more

Whenever I remove the near cache config from the client config, all works pretty perfect, except I do not have a near cache of course.

What do I miss here ?

Upvotes: 0

Views: 724

Answers (2)

magicroomy
magicroomy

Reputation: 395

My problem is solved using the 3.11.1 version of hazelcast.

Upvotes: 0

Gokhan Oner
Gokhan Oner

Reputation: 3257

@magicroomy, I run the same on both 3.9.2 & 3.11. I can confirm that:

  • If you change Replicated Map to Map, it works with or without Near Cache.
  • When using Replicated Map, if Near Cache defined, the exception thrown on the server side.
  • Without Near Cache, ReplicatedMap also works.

I created a github issue as well: https://github.com/hazelcast/hazelcast/issues/14210

Upvotes: 0

Related Questions