我正在使用Kryo Serialization for Hazelcast(3.5.4)。现在我有以下对象。 公共类XData扩展HashMap {...} 公共课X. {..} 现在,XData将包含键为Integer,值为X对象。我试图为XData设置KryoSerializer:
public class XDataKryoSerializer<K, V> implements StreamSerializer<XData<K, V>>
{
private static final ThreadLocal<Kryo> kryoThreadLocal = new ThreadLocal<Kryo>()
{
@Override
protected Kryo initialValue()
{
Kryo kryo = new Kryo();
kryo.register(XData.class);
kryo.register(X.class);
return kryo;
}
};
@Override
public int getTypeId()
{
return KryoSerializationConstant.XDATA_TYPE;
}
@Override
public XData<K, V> read(ObjectDataInput inp) throws IOException
{
InputStream in = (InputStream) inp;
Input input = new Input(in);
Kryo kryo = kryoThreadLocal.get();
return kryo.readObject(input, XData.class);
}
@Override
public void write(ObjectDataOutput out, XData<K, V> xData) throws IOException
{
Kryo kryo = kryoThreadLocal.get();
Output output = new Output((OutputStream) out);
kryo.writeObject(output, xData);
output.flush();
}
@Override
public void destroy()
{
// TODO Auto-generated method stub
}
}
这是hazelcast.xml文件:
<serialization>
<portable-version>0</portable-version>
<serializers>
<serializer type-class="com.abc.hazelcast.benchmarking.data.javaSer.X"
class-name="com.abc.hazelcast.benchmarking.data.kryoSer.XKryoSerializer" />
<serializer type-class="com.abc.hazelcast.benchmarking.data.javaSer.XData"
class-name="com.abc.hazelcast.benchmarking.data.kryoSer.XDataKryoSerializer" />
</serializers>
</serialization>
<services enable-defaults="true" />
我也有类似的XKryoSerializable类。使用Hazelcast可以很好地处理事情,但是当xdata对象被放入hazelcast时,XKryoSerializer不会被调用。这是预期的行为还是我错过了什么? 是否有一种方法可以在调用xdata对象的读/写方法时调用内容对象的Kryo序列化器,X?