简体   繁体   English

使用单线程从 2 个编年史队列中读取

[英]Reading from 2 chronicle queue using single thread

I am trying to build a service where I will get a one type of message on one chronicle queue and another type of message on another chronicle queue within the same process.我正在尝试构建一个服务,我将在同一进程中的一个编年史队列上获得一种类型的消息,并在另一个编年史队列上获得另一种类型的消息。 I don't want to give 2 dedicated threads to each of these queues as frequency of these messages will not be very high even though they need to be responded with minimum possible latency.我不想为每个队列提供 2 个专用线程,因为这些消息的频率不会很高,即使它们需要以尽可能小的延迟进行响应。 I can still give one thread for both the queues.我仍然可以为两个队列提供一个线程。 What would be your suggestion around designing this so that I can read 2 or multiple queues using a single thread?关于设计这个以便我可以使用单个线程读取 2 个或多个队列,您有什么建议?

First of all, I'd suggest if possible you use the same queue for both types of messages - this will be the fastest option for a number of reasons, main one will be memory locality.首先,如果可能的话,我建议您对两种类型的消息使用相同的队列 - 由于多种原因,这将是最快的选择,主要是内存局部性。

However you can as well do round-robin style reading across more than one queue, eg:但是,您也可以跨多个队列进行循环式阅读,例如:

ChronicleQueue q1 = ...;
ChronicleQueue q2 = ...;
ExcerptTailer tailer1 = q1.createTailer();
ExcerptTailer tailer2 = q2.createTailer();
while (true) {
    try (DocumentContext dc = tailer1.readingDocument()) {
        if (dc.isPresent()) {
            // do something with message type 1
        }
    }
    try (DocumentContext dc = tailer2.readingDocument()) {
        if (dc.isPresent()) {
            // do something with message type 2
        }
    }
}

Or in methodReader style:或者在 methodReader 样式中:

ChronicleQueue q1 = ...;
ChronicleQueue q2 = ...;
ExcerptTailer tailer1 = q1.createTailer();
ExcerptTailer tailer2 = q2.createTailer();
MethodReader reader1 = tailer1.methodReader(MessageTypeOneInterface.class);
MethodReader reader2 = tailer2.methodReader(MessageTypeTwoInterface.class);
while (true) {
    reader1.readOne();
    reader2.readOne();
}

If there's no new message in the queue, calling tailer.readingDocument() (or, equivalent, reader.readOne() ) is very cheap (in fact it's just a couple of volatile reads).如果队列中没有新消息,调用tailer.readingDocument() (或等效的reader.readOne() )非常便宜(实际上它只是一些易失性读取)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM