6

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?

user2165089
  • 61
  • 1
  • 2
  • Did you share that class between all members in your cluster and if you use a client; between client and server? Because I'm quite sure that the class just isn't found. – pveentjer Mar 20 '14 at 12:49
  • Thanks for your reply! I ran my Hazelcast Nodes locally in on my machine using the run client windows batch file and instantiated the Hazelcast Instance in my Web Application hosted by Tomcat Server. Do I have to programatically code the Hazelcast nodes to start up through my web app as well? Sorry I could not seem to find any conceptual knowledge on Hazelcast implementation. – user2165089 Mar 21 '14 at 05:31
  • Don't use the batch files. They are just for playing around. I normally embed the hazelcast nodes within my application, e.g. HazelcastInstance hz = Hazelcast.newHazelcastInstance() But the cause of your problem is that the class file is not available on all member in your cluster. – pveentjer Mar 21 '14 at 06:41
  • How do I propagate the class file across my Hazelcast nodes? I am running the nodes locally so is there a setting I am missing to help Hazelcast point to that specific class? – user2165089 Mar 21 '14 at 12:11
  • We don't provide a distributed classloader. So you need to include the jars of your application (so the one containing aa.Bid) to all the JVM's running Hazelcast. – pveentjer Mar 21 '14 at 12:58
  • where could be the problem if classes **should** be available on all nodes? – nefo_x Nov 19 '14 at 18:36

3 Answers3

5

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!

John W
  • 101
  • 1
  • 8
3

If you are running the code locally the issue is that there are Hazelcast nodes that does not have the same class. Check and confirm in the console for the following:

Members [1] {
  Member [10.17.54.16]:5701 this
}

If there are more hazelcast instances are running shutdown or close them and then run the application.

Shenal
  • 202
  • 5
  • 21
2

public class Bid should be defined in server side also along with client side