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);
}
}
我正在使用它:
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
IMap<String, Set<Tuple<Integer, Double>>> myMap = Hazelcast.getMap("myMap");
然而,每当我试图使用这个地图时(除了实现Serializable,那个场景工作正常)我就会回到这个例外:
com.hazelcast.nio.serialization.HazelcastSerializationException: java.io.NotSerializableException: Tuple
我在这里做错了什么?感谢任何想法,万分感谢!
更新
尝试过这个建议(感谢noctarius)我现在有了这个:
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 () {
}
}
这也是
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 () {
}
}
使用这样的配置(尝试评论各种变体和所有)
<hazelcast>
<serialization>
<serializers>
<serializer type-class="Tuple">TupleStreamSerializer</serializer>
<serializer type-class="Set<Tuple>">TupleSetStreamSerializer</serializer>
</serializers>
</serialization>
</hazelcast>
我似乎仍然缺少某些东西?非常感谢!
答案 0 :(得分:0)
您必须为Set接口编写特殊的StreamSerializer。每当Hazelcast开始序列化Serializable标记类时,它就会失去序列化控制。有一些解决方法,但所有这些都不是很好。最好的方法是使用Serializable拦截Set的序列化并提供自定义序列化器:http://docs.hazelcast.org/docs/3.5/manual/html-single/index.html#streamserializer
您将在Hazelcast核心代码中找到LinkedList类型的示例:https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/internal/serialization/impl/LinkedListStreamSerializer.java