【发布时间】:2022-06-27 15:18:54
【问题描述】:
假设一个编年史队列和一个将两种类型的消息写入队列的生产者。 每种类型的消息都使用不同的“WireKey”编写。
// Writes: {key1: TestMessage}
appender.writeDocument(w -> w.write("key1").text("TestMessage"));
// Writes: {key2: AnotherTextMessage}
appender.writeDocument(w -> w.write("key2").text("AnotherTextMessage"));
问题:
如何编写一个单线程的消费者来读取这两种类型的消息并以不同的方式处理它们?
我的尝试:
// This can read both types of messages, but cannot
// tell which type a message belongs to.
tailer.readDocument(wire -> {
wire.read().text();
});
// This only reads type "key1" messages, skips all "key2" messages.
tailer.readDocument(wire -> {
wire.read("key1").text();
});
// This crashes. (because it advances the read position illegally?)
tailer.readDocument(wire -> {
wire.read("key1").text();
wire.read("key2").text();
});
我希望我可以做类似wire.readKey() 的操作并获取文档的 WireKey,然后继续读取文档并动态处理它。我该怎么做?
注意:我知道这可以使用 methodReader 和 methodWriter 来完成,并且似乎文档/演示推荐了这种方法(?)但我不想使用那个 API,并明确说明读取和写入消息。我认为必须有一种方法来完成这个用例。
谢谢。
【问题讨论】:
标签: chronicle chronicle-queue chronicle-wire