【发布时间】:2016-10-17 09:52:33
【问题描述】:
我正在尝试序列化两个自定义类的哈希图和集合(包含更多哈希图和集合)。
Class1:NodeStorage.java
@NotNull
private final String id;
@Nullable
private String type;
@Nullable
private HashMap<String, String> properties;
Class2:RelationshipStorage.java
@NotNull
private final String id;
@Nullable
private String type;
@Nullable
private HashMap<String, String> properties;
@NotNull
private final NodeStorage startNode;
@NotNull
private final NodeStorage endNode;
要序列化的集合:
private HashMap<NodeStorage, NodeStorage> readsSetNode;
private HashMap<NodeStorage, NodeStorage> updateSetNode;
private ArrayList<NodeStorage> deleteSetNode;
private ArrayList<NodeStorage> createSetNode;
private HashMap<RelationshipStorage, RelationshipStorage> readsSetRelationship;
private HashMap<RelationshipStorage, RelationshipStorage> updateSetRelationship;
private ArrayList<RelationshipStorage> deleteSetRelationship;
private ArrayList<RelationshipStorage> createSetRelationship;
我到现在为止的尝试:
kryo.register(NodeStorage.class, 1);
kryo.register(RelationshipStorage.class, 2);
kryo.register(HashMap.class, mapSerializer);
mapSerializer.setKeyClass(NodeStorage.class, kryo.getSerializer(NodeStorage.class));
mapSerializer.setKeyClass(RelationshipStorage.class, kryo.getSerializer(RelationshipStorage.class));
mapSerializer.setValuesCanBeNull(false);
mapSerializer.setKeysCanBeNull(false);
listSerializer.setElementClass(NodeStorage.class, kryo.getSerializer(NodeStorage.class));
listSerializer.setElementClass(RelationshipStorage.class, kryo.getSerializer(RelationshipStorage.class));
listSerializer.setElementsCanBeNull(false);
public byte[] serialize()
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Output output = new Output(stream);
mapSerializer.write(kryo, output, readsSetNode);
byte[] bytes = output.toBytes();
output.close();
return bytes;
}
我用 kryo.writeclassandobject 尝试过,但效果不佳。我明白了:
> > #
> # A fatal error has been detected by the Java Runtime Environment:
> #
> # SIGSEGV (0xb) at pc=0x00007f92f7f6efe0, pid=4637, tid=0x00007f92f94fd700
> #
> # JRE version: OpenJDK Runtime Environment (8.0_102-b14) (build 1.8.0_102-b14)
> # Java VM: OpenJDK 64-Bit Server VM (25.102-b14 mixed mode linux-amd64 compressed oops)
> # Problematic frame:
> # V [libjvm.so+0x787fe0]
> #
> # Core dump written
完整代码在:https://github.com/Raycoms/thesis
声明:
private Kryo kryo = new Kryo();
MapSerializer mapSerializer = new MapSerializer();
CollectionSerializer listSerializer = new CollectionSerializer();
【问题讨论】:
-
您可以尝试从 100 而不是 1 作为第一个 id 开始吗?见github.com/EsotericSoftware/kryo/issues/430
-
将其设置为 100 和 200,根本没有改变任何东西。相同的崩溃消息。
-
可能与线程相关 - 请参阅this GitHub issue,这似乎相似。那里链接的thread-pooling readme 至少帮助了其他一名编码人员。 (通过检查your log 中的堆栈跟踪发现这一点。)
-
目前我只写一个线程,在发送给其他人阅读之前抛出异常。
-
添加了声明。
标签: java linux serialization fatal-error kryo