我可以将不同类型的消息写入编年史队列吗?
Can I write different types of messages to a chronicle-queue?
我想将不同类型的消息写入编年史队列,并根据消费者的类型处理消息。
我该怎么做?
Chronicle-Queue 提供可用于编写任何类型消息的低级构建块,因此您可以选择正确的数据结构。
例如,您可以在写入编年史的数据前加上一个小 header 和一些 meta-data,然后将其用作数据处理的鉴别器。
您还可以 write/read 通用对象。这会比使用你自己的方案稍微慢一些,但它是一种始终读取你编写的类型的简单方法吗?
为此,我使用 Wire
try (DocumentContext dc = appender.writingDocument())
{
final Wire wire = dc.wire();
final ValueOut v = wire.getValueOut();
valueOut.typePrefix(m.getClass());
valueOut.marshallable(m);
}
回读时我:
try (DocumentContext dc = tailer.readingDocument())
{
final Wire wire = dc.wire();
final ValueIn valueIn = wire.getValueIn();
final Class clazz = valueIn.typePrefix();
// msgPool is a prealloacted hashmap containing the messages I read
final ReadMarshallable readObject = msgPool.get(clazz);
valueIn.readMarshallable(readObject)
// readObject can now be used
}
我想将不同类型的消息写入编年史队列,并根据消费者的类型处理消息。
我该怎么做?
Chronicle-Queue 提供可用于编写任何类型消息的低级构建块,因此您可以选择正确的数据结构。
例如,您可以在写入编年史的数据前加上一个小 header 和一些 meta-data,然后将其用作数据处理的鉴别器。
您还可以 write/read 通用对象。这会比使用你自己的方案稍微慢一些,但它是一种始终读取你编写的类型的简单方法吗?
为此,我使用 Wire
try (DocumentContext dc = appender.writingDocument())
{
final Wire wire = dc.wire();
final ValueOut v = wire.getValueOut();
valueOut.typePrefix(m.getClass());
valueOut.marshallable(m);
}
回读时我:
try (DocumentContext dc = tailer.readingDocument())
{
final Wire wire = dc.wire();
final ValueIn valueIn = wire.getValueIn();
final Class clazz = valueIn.typePrefix();
// msgPool is a prealloacted hashmap containing the messages I read
final ReadMarshallable readObject = msgPool.get(clazz);
valueIn.readMarshallable(readObject)
// readObject can now be used
}