Can you please let me know what am I doing wrong here? I'm really struggling implementing a more efficient serializer for Hazelcast. I have the following Java class:
public class Tuple<X, Y> implements DataSerializable
{
public X x;
public Y y;
private Tuple()
{
}
public Tuple(X x, Y y)
{
this.x = x;
this.y = y;
}
@Override
public void readData(ObjectDataInput in)
throws IOException {
Instance.log("read data");
this.x = (X)in.readObject();
this.y = (Y)in.readObject();
}
@Override
public void writeData(ObjectDataOutput out)
throws IOException {
Instance.log("write data");
out.writeObject(this.x);
out.writeObject(this.y);
}
}
and I'm using it like so:
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
IMap<String, Set<Tuple<Integer, Double>>> myMap = Hazelcast.getMap("myMap");
however, whenever I'm attempting to use this map (except implementing Serializable, that scenario works fine) I'm getting back this exception:
com.hazelcast.nio.serialization.HazelcastSerializationException: java.io.NotSerializableException: Tuple
What am I doing wrong here? Appreciate any idea, thanks a million!
Update
Having tried the suggestion (thanks noctarius) I now have this:
public class TupleSetStreamSerializer
implements StreamSerializer<Set<Tuple>> {
@Override
public int getTypeId () {
return SerializationIdentifiers.TupleSerializationFactoryId;
}
@Override
public void write(ObjectDataOutput out, final Set<Tuple> set )
throws IOException {
int size = set == null ? 0 : set.size();
Instance.log("Serialize set");
out.writeInt(size);
if (size>0) {
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
out.writeObject(iterator.next());
}
}
}
@Override
public Set<Tuple> read( ObjectDataInput in )
throws IOException {
Instance.log("Deserialize set");
int size = in.readInt();
Set result = null;
if (size > 0) {
result = new HashSet<Tuple>();
for (int i = 0; i < size; i++) {
result.add(in.readObject());
}
}
return result;
}
@Override
public void destroy () {
}
}
and this also
public class TupleStreamSerializer
implements StreamSerializer<Tuple> {
@Override
public int getTypeId () {
return SerializationIdentifiers.TupleSerializationFactoryId;
}
@Override
public void write(ObjectDataOutput out, Tuple tup )
throws IOException {
out.writeObject(tup.x);
out.writeObject(tup.y);
}
@Override
public Tuple read( ObjectDataInput in )
throws IOException {
Object x = in.readObject();
Object y = in.readObject();
return new Tuple(x,y);
}
@Override
public void destroy () {
}
}
with a configuration like this (attempted commenting various variants and all)
<hazelcast>
<serialization>
<serializers>
<serializer type-class="Tuple">TupleStreamSerializer</serializer>
<serializer type-class="Set<Tuple>">TupleSetStreamSerializer</serializer>
</serializers>
</serialization>
</hazelcast>
I still seem to be missing something? Thanks a lot!
You have to write a special StreamSerializer for the Set interface. Whenever Hazelcast starts to serialize a Serializable marked class it looses serialization control. There are a few workarounds but all of them are not really nice. The best way is to intercept serialization of Set using Serializable and offer a custom serializer: http://docs.hazelcast.org/docs/3.5/manual/html-single/index.html#streamserializer
You'll find an example for the LinkedList type inside the Hazelcast core code: https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/internal/serialization/impl/LinkedListStreamSerializer.java
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.