我有嵌套类,其中包含日期字段以及其他原始字段。我已经实现了Outer和Inner类的DataSerializable接口。
外课
public class Outer implements DataSerializable {
private static final long serialVersionUID = 1L;
private Long number;
private String enumerationType;
private Basic basic;
.
.
.
@Override
public void readData(ObjectDataInput in) throws IOException {
number = in.readLong();
enumerationType = in.readUTF();
basic = new Basic();
basic.readData(in);
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeLong(number);
out.writeUTF(enumerationType);
basic.writeData(out);
}
}
内部课 - 基本
private class Basic implements DataSerializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String a;
private String b;
private String c;
private String d;
private String e;
private String f;
private String g;
private String h;
private String i;
private Date date1;
private Long long1;
private Date date2;
private Long long2;
@Override
public void readData(ObjectDataInput in) throws IOException {
Field[] fields = HazelcastUtil.ReflectUtil.getFields(this.getClass());
for (Field field : fields) {
if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
try {
if (field.getType().isAssignableFrom(String.class)) {
field.set(this, in.readUTF());
} else if (field.getType().isAssignableFrom(Long.class)) {
field.set(this, in.readLong());
} else if (field.getType().isAssignableFrom(Character.class)) {
field.set(this, in.readChar());
} else if (!field.getType().isAssignableFrom(Outer.class)) {
field.set(this, in.readObject(field.getType()));
}
//}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
//out.writeUTF(replacementNpi);
Field[] fields = HazelcastUtil.ReflectUtil.getFields(this.getClass());
for (Field field : fields) {
if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
try {
Object value = field.get(this);
if (value != null) {
//HazelCastLogger.info(value.getClass()+" >> "+value.toString()+" >> "+field.getName() + " >> "+field.getType(), this.getClass());
if (field.getType().isAssignableFrom(String.class)) {
out.writeUTF((String) value);
} else if (field.getType().isAssignableFrom(Long.class)) {
out.writeLong((Long) value);
} else if (field.getType().isAssignableFrom(Character.class)) {
out.writeChar((Character) value);
} else if (!field.getType().isAssignableFrom(Outer.class)) {
out.writeObject(value);
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
加载地图时,我面临异常。
com.hazelcast.nio.serialization.HazelcastSerializationException: StreamSerializerAdapter{serializer=com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$DateSerializer@f8b802f} is not implementing the interface com.hazelcast.nio.serialization.TypedStreamDeserializer interface. Please implement this interface to deserialize for class class java.util.Date
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:55)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.readObject(AbstractSerializationService.java:294)
at com.hazelcast.internal.serialization.impl.ByteArrayObjectDataInput.readObject(ByteArrayObjectDataInput.java:580)
at com.entity.Outer$Basic.readData(Outer.java:626)
at com.entity.Outer.readData(Outer.java:155)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.readInternal(DataSerializableSerializer.java:158)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.read(DataSerializableSerializer.java:105)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.read(DataSerializableSerializer.java:50)
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:191)
at com.hazelcast.map.impl.record.ObjectRecordFactory.newRecord(ObjectRecordFactory.java:37)
at com.hazelcast.map.impl.recordstore.AbstractRecordStore.createRecord(AbstractRecordStore.java:95)
at com.hazelcast.map.impl.recordstore.DefaultRecordStore.createRecord(DefaultRecordStore.java:78)
at com.hazelcast.map.impl.recordstore.DefaultRecordStore.putFromLoadInternal(DefaultRecordStore.java:956)
at com.hazelcast.map.impl.recordstore.DefaultRecordStore.putFromLoad(DefaultRecordStore.java:931)
at com.hazelcast.map.impl.operation.PutFromLoadAllOperation.run(PutFromLoadAllOperation.java:74)
at com.hazelcast.spi.Operation.call(Operation.java:148)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.call(OperationRunnerImpl.java:202)
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:191)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:120)
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.run(OperationThread.java:100)
该异常专门针对java.util.Date对象。
com.hazelcast.nio.serialization.HazelcastSerializationException:StreamSerializerAdapter {serializer = com.hazelcast.internal.serialization.impl.JavaDefaultSerializers$DateSerializer@f8b802f}未实现com.hazelcast.nio.serialization.TypedStreamDeserializer接口接口。请实现此接口以反序列化类java.util.Date
你能帮我解决一下这个问题吗?我正在使用hazelcast IMDG 3.10版以及maven和spring boot。
答案 0 :(得分:0)
应该
if (!field.getType().isAssignableFrom(Outer.class))
是
if (!field.getType().isAssignableFrom(Date.class))
内部类没有外部类作为字段,因此不需要序列化它。但它确实有日期字段,并没有相应的逻辑。
另外,如果你正在使用反射,它会很慢