Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hazelcast Serializable Map ClassNotFound Exception

I am trying to implement Hazelcast in a simple web application.

I am trying to store a custom object into my Hazelcast Map and have implemented Serializable in my Bid Object Class with the necessary imports.

import java.io.Serializable;

Here is a snippet of the class object.

public class Bid implements Serializable{

private String bidId;      
private String stock;
private int price;
private String userId;
private Date date;

Here are the syntax as with the tutorial to store the Bid Object into the Map where newBid is a Bid Object.

Config cfg = new Config();
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);    

Map<String, Bid> mapBids = instance.getMap("bids");
        mapBids.put(newBid.getUserId(), newBid);

My Hazelcast nodes are up and running but when I query the bids map, I get the following error.

com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.Class
NotFoundException: aa.Bid
        at com.hazelcast.nio.serialization.DefaultSerializers$ObjectSerializer.r
ead(DefaultSerializers.java:190)
        at com.hazelcast.nio.serialization.StreamSerializerAdapter.read(StreamSe
rializerAdapter.java:59)
        at com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(Ser
ializationServiceImpl.java:221)
        at com.hazelcast.spi.impl.NodeEngineImpl.toObject(NodeEngineImpl.java:15
6)
        at com.hazelcast.map.MapService.toObject(MapService.java:773)
        at com.hazelcast.map.proxy.MapProxyImpl.entrySet(MapProxyImpl.java:502)
        at com.hazelcast.examples.TestApp.handleMapEntries(TestApp.java:882)
        at com.hazelcast.examples.TestApp.handleCommand(TestApp.java:371)
        at com.hazelcast.examples.TestApp.start(TestApp.java:187)
        at com.hazelcast.examples.TestApp.main(TestApp.java:1641)

Caused by: java.lang.ClassNotFoundException: aa.Bid
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at com.hazelcast.nio.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:109)
        at com.hazelcast.nio.IOUtil$1.resolveClass(IOUtil.java:89)
        at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
        at java.io.ObjectInputStream.readClassDesc(Unknown Source)
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at com.hazelcast.nio.serialization.DefaultSerializers$ObjectSerializer.r
ead(DefaultSerializers.java:185)
        ... 9 more

The object class is located in the same folder as my web app with the necessary import syntax but it is not reading it. I have added the CLASSPATH to my Hazelcast jar file as well.

Are there any variable that I have to make Transient in my Bid class object in order for Serializable to work? Or am I missing something?

like image 393
user2165089 Avatar asked Mar 20 '14 08:03

user2165089


People also ask

What are the different types of serialization available in Hazelcast?

Hazelcast supports various types of Serialization. However, let’s look at some commonly used ones, i.e., Java Serialization and Java Externalizable. First let's look at Java Serialization. Let's say, we define an Employee class with Serializable interface implemented. Let’s now write code to add Employee object to the Hazelcast map.

Are client objects deserialised in cluster side in Hazelcast?

Normally client objects are not deserialised in cluster side in hazelcast. There is a few exceptions to that. One of them happens to be Transaction related API unfortunately. We have plan to change and review these parts but it is not soon.

What is the difference between Hazelcast and HashMap?

Also note that Hazelcast stores serialized data for key and value instead of storing it in-memory like HashMap. So, Hazelcast does the heavy-lifting of Serialization and Deserialization. However, there is a pitfall here.

Why is my Hazelcast object with the same value but different attributes?

It is because Hazelcast does not deserialize the key, i.e., Employee while comparison. It directly compares the bytecode of the serialized key. So, an object with the same value to all the attributes would be treated the same.


1 Answers

I was experiencing this same issue using a remote two-node cluster when trying to put an instance of 'MyClass' into the relevant iMap. I was tearing my hair out thinking that I'd need to distribute the jar containing 'MyClass' but realised that the problem was that the definition within hazelcast.xml file was specifying an in-memory-format of OBJECT...

<in-memory-format>OBJECT</in-memory-format>

I changed this value to BINARY and everything started working.

<in-memory-format>BINARY</in-memory-format>

nb. I now have a number of different maps within the cluster, some of which are defined as OBJECT and some as BINARY.

I hope this helps someone out there!

like image 153
John W Avatar answered Sep 22 '22 12:09

John W