Search code examples
javamessagingchronicle

java chronicle messages are not removed after reading


I am trying to use Java Chronicle 1.9.2 to write/read messages. I know newer versions are available but have few questions before i invest more time in it.

I need to have the message removed once i read the Excerpt from the Chronicle. So if my reader starts again it doesn't go back to the beginning. Also message is of no use to me once i read it so want to remove it.

Is there an option to do it? i am trying following code and every time i start reader, i get all the messages again. Also Is there an option to put a Time to Live on message so it is automatically removed after certain time period.

WRITER -

        String tempPath = System.getProperty("java.io.tmpdir");
        String basePrefix = tempPath  + "chronicle";
        System.out.println("base prefix: " + basePrefix);
        Chronicle chr = new IndexedChronicle(basePrefix);
        final Excerpt excerpt = chr.createExcerpt();
        excerpt.startExcerpt(this.getmsgtext().length() + 4);
        excerpt.writeBytes(this.getmsgtext());
        excerpt.finish();
        chr.close();

READER -

        String tempPath = System.getProperty("java.io.tmpdir");
        String basePrefix = tempPath +  "chronicle";
        System.out.println("base prefix: " + basePrefix);
        Chronicle chr = new IndexedChronicle(basePrefix);
        final Excerpt excerpt = chr.createExcerpt();

        while (excerpt.nextIndex()) {
                System.out.println("Read string from chronicle: " + excerpt.readByteString());
        }
        chr.close();

Solution

  • I need to have the message removed once i read the Excerpt from the Chronicle.

    The only way to remove them is with file rolling. You can do this yourself with IndexedChronicle or using VanillaChronicle or the current SingleChronicleQueue

    So if my reader starts again it doesn't go back to the beginning. Also message is of no use to me once i read it so want to remove it.

    The standard practice is to write your results, including the source id, to a Chronicle. This way you always know which messages have been processed successfully. e.g. you could have message read but not processed if a crash occurs.

    i am trying following code and every time i start reader, i get all the messages again.

    That is what it is designed to do by default. How you keep track of which messages were processed correctly is up to you. (Though we recommend recording this in another queue and reading the last message to find that index)

    Is there an option to put a Time to Live on message so it is automatically removed after certain time period.

    You can add a timestamp to each message and ignore old messages.

    Chronicle Queue v4.1.0 is the latest release.