gpt4 book ai didi

java - Hazelcast 可移植序列化 - 处理空属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:53 28 4
gpt4 key购买 nike

我正在使用 Hazelcast 将一些数据存储在 IMap 中,但目前我在存储在缓存中的 POJO 中遇到空属性的“问题”。

假设我有以下 pojo:

public class TestPojo implements Portable {

public static final int CLASS_ID = 1;

private Long id;
private String description;
private Long value;

@Override
public int getFactoryId() {
return 1;
}

@Override
public int getClassId() {
return CLASS_ID;
}

@Override
public void writePortable(PortableWriter writer) throws IOException {
writer.writeLong("id", id);
writer.writeUTF("description", description);
writer.writeLong("value", value);
}

@Override
public void readPortable(PortableReader reader) throws IOException {
id = reader.readLong("id");
description = reader.readUTF("description");
value = reader.readLong("value");
}
}

问题是在序列化包含属性 'value' = null 的此类时,它将抛出 NullPointerException,因为 writeLong 方法使用原始 long。

[3.3-EA2] Exception while load all task:com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.NullPointerException

我的问题是:应该如何正确处理这个空值?

我正在使用 Hazelcast 3.3-EA2。

谢谢。

最佳答案

您为什么要使用可移植设备? (Identified)DataSerializable 不那么痛苦。 Portable 不处理空值,因此这意味着您需要转换为字符串,然后例如为 null 插入“null”或为处理 null 的数字插入“1234”。通过阅读,你读出了字符串,如果它包含 'null' 那么你设置 null 否则你例如做 Long.parseLong。

所以除非你有很好的理由,否则我会远离它。

关于java - Hazelcast 可移植序列化 - 处理空属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24680512/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com