Reputation: 11
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?
Upvotes: 0
Views: 329
Reputation: 1206
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).
Upvotes: 1