0

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. 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?

  • No idea, but I'd probably wouldn't bother saving a single thread, unless you work in a constrained environment (note that e.g., jetty by default starts some 200 threads). – maaartinus Feb 04 '20 at 10:44

1 Answers1

1

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, e.g.:

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:

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).

Dmitry Pisklov
  • 1,196
  • 1
  • 6
  • 16