0

Sorry for the newbie question in advance. I have a REST endpoint that returns the required data in scrollable manner by chunk per every call I made in a loop. And I want to put each of these chunks into the Chronicle queue as a separate document/message.

This chunk response returned by the endpoint call can be described as follows:

public class ItemCollection {
    private Set<Item> items;
}

where

public class Item {
    private String id;
    private int version;
}

As far as I understood from the topic Chronicle Queue: Usage with less or no lambdas, I can't use lambdas with my ItemCollection to write a document/message to the Chronicle queue.

So, should I use the approach mentioned there with tailer.readingDocument().wire().read("itemCollection").marshallable(itemCollection) and make both my Item and ItemCollection classes as extends AbstractMarshallable?

Thank you in advance!

CC: @peter-lawrey

I tried to find similar topics here for the Chronicle queue but no luck unfortunately.

  • Just before I answer, please allow me to confirm my understanding of your question. If I'm not mistaken, you have a Set of Items and your intention is to write each item separately to a Chronicle Queue as individual documents. Am I correct in interpreting your requirement accurately? – Rob Austin May 19 '23 at 10:04
  • @rob-austin, thank you! No, I want to write each returned `ItemCollection` (containing set of `Item`s) as individual document, i.e. one document in the queue per one `ItemCollection` returned per one endpoint call. – Pavlo Denysenko May 19 '23 at 10:14

1 Answers1

0

if so like this:

package X;

import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueue;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder;
import net.openhft.chronicle.wire.DocumentContext;
import net.openhft.chronicle.wire.SelfDescribingMarshallable;

import java.util.HashSet;
import java.util.Set;

public class X {

    public static class Item extends SelfDescribingMarshallable {
        private String id;
        private int version;
    }

    public static void main(String[] args) {

        Set<Item> items = new HashSet<>();

        // todo.. set the items list

        try (SingleChronicleQueue q = SingleChronicleQueueBuilder.single("my-dir").build()) {

            // to write it
            ExcerptAppender appender = q.acquireAppender();

            for (Item item : items) {
                try (DocumentContext dc = appender.writingDocument()) {
                    dc.wire().write("item").object(item);
                }
            }

            // to read it
            ExcerptTailer tailer = q.createTailer();
            for (; ; ) {
                try (DocumentContext dc = tailer.readingDocument()) {
                    if (!dc.isPresent())
                        break;
                    System.out.println(dc.wire().read("item").object());
                }
            }
        }
    }
}
Rob Austin
  • 620
  • 3
  • 5
  • Thank you, but as I wrote previously I want to put into the queue all `ItemCollection` (containing set of `Item`s) as a single document. – Pavlo Denysenko May 19 '23 at 10:20