Chronicle Queue - readDocument().isPresent() 在存在值时返回 false

Chronicle Queue - readDocument().isPresent() returning false when values are present

这里修改了基本代码[github]https://github.com/OpenHFT/Chronicle-Queue/blob/master/docs/How_it_works.adoc

基本上我正在拍摄编年史队列中一些相同的可编组对象。我包含了一些打印语句来显示正在发生的事情。


import java.io.IOException;
import java.nio.file.Files;

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.Marshallable;


public class App {

    static class MyObject implements Marshallable {
        String name;
        int age;

        @Override
        public String toString() {
            //return "";
            return Marshallable.$toString(this);
        }
    }

    public static void main(String[] args) throws IOException {

        // will write the .cq4 file to working directory
        SingleChronicleQueue queue = SingleChronicleQueueBuilder.builder().path(Files
                .createTempDirectory("queue").toFile()).build();
        ExcerptAppender appender = queue.acquireAppender();
        ExcerptTailer tailer = queue.createTailer();

        MyObject me = new MyObject();
        me.name = "rob";
        me.age = 40;

        System.out.println("1. Tailer is Present =  " + tailer.readingDocument().isPresent() + ", Tailer Current Index = " + tailer.index() + ", End = " + queue.createTailer().toEnd().index());
        // write 'MyObject' to the queue
        appender.writeDocument(me);
        appender.writeDocument(me);
        appender.writeDocument(me);
        appender.writeDocument(me);
        System.out.println("2. Tailer is Present =  " + tailer.readingDocument().isPresent() + ", Tailer Current Index = " + tailer.index() + ", End = " + queue.createTailer().toEnd().index());
        // read 'MyObject' from the queue
        MyObject result = new MyObject();
        tailer.readDocument(result);


        System.out.println("3. Tailer is Present =  " + tailer.readingDocument().isPresent() + ", Tailer Current Index = " + tailer.index() + ", End = " + queue.createTailer().toEnd().index());
        System.out.println(result);



        try {
            Thread.sleep(500);
        } catch (Exception e){
            System.out.println(e);
        }
        System.out.println("Tailer is Present =  " + tailer.readingDocument().isPresent() + ", Tailer Current Index = " + tailer.index() + ", End = " + queue.createTailer().toEnd().index());

    }
}

产出

1. Tailer is Present =  false, Tailer Current Index = 0, End = 0
2. Tailer is Present =  true, Tailer Current Index = 78129750081536, End = 78129750081540
3. Tailer is Present =  false, Tailer Current Index = 78129750081536, End = 78129750081540
!chron.App$MyObject {
  name: !!null "",
  age: 0
}

Tailer is Present =  false, Tailer Current Index = 78129750081536, End = 78129750081540

所以根据我的理解...我创建了一个静态对象。我总共将它喂给编年史队列四次,都是同一个对象。可以看到chronicle queue里面一共有四个对象。结束索引 - 起始索引。

trailer.readingDocument().isPresent()只能看到创建的第一个对象。之后它 returns 错误...

如果队列中有对象,为什么 readingDocument().isPresent() 返回 false?还有为什么对象返回 null?

编辑 - 版本实现 'net.openhft:chronicle-queue:5.17.25'

尝试将它与 try-with-resources 块一起使用,如下所示:

try (DocumentContext context = tailer.readingDocument()) {
 if (context.isPresent()) {
  // do something
 }
 int myIndex = context.index
}

例如

try (final DocumentContext dc = appender.writingDocument()) {
    dc.wire().write().text(“your text data“);
    System.out.println("your data was store to index="+ dc.index());
}

try (DocumentContext context = tailer.readingDocument()) {
   String youText = context.wire().read().text();
}

我用 tryWithResources 重写了你的例子并且它有效。 您的代码中的问题是每次调用 tailer.readingDocument().isPresent() 实际上是将指针移动到最后读取的条目。

static class MyObject extends SelfDescribingMarshallable {
    String name;
    int age;
}

public static void main(String[] args) throws IOException {

    // will write the .cq4 file to working directory
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.builder().path(Files.createTempDirectory("queue").toFile()).build();
    ExcerptAppender appender = queue.acquireAppender();
    ExcerptTailer tailer = queue.createTailer();

    MyObject me = new MyObject();
    me.name = "rob";
    me.age = 40;

    System.out.println("1. Tailer is Present =  " + tailer.readingDocument().isPresent() + ", Tailer Current Index = " + tailer.index() + ", End = " + queue.createTailer().toEnd().index());
    // write 'MyObject' to the queue
    try (final DocumentContext dc = appender.writingDocument()) {
        me.writeMarshallable(dc.wire());
    }
    try (final DocumentContext dc = appender.writingDocument()) {
        me.writeMarshallable(dc.wire());
    }
    try (final DocumentContext dc = appender.writingDocument()) {
        me.writeMarshallable(dc.wire());
    }
    try (final DocumentContext dc = appender.writingDocument()) {
        me.writeMarshallable(dc.wire());
    }
    try (final DocumentContext dc = tailer.readingDocument()) {
        System.out.println("2. Tailer is Present =  " + dc.isPresent() + ", Tailer Current Index = " + dc.index() + ", End = " + queue.createTailer().toEnd().index());
        if (dc.isPresent()) {
            MyObject result = new MyObject();
            result.readMarshallable(dc.wire());
            System.err.println(result);
        }
    }

    try (final DocumentContext dc = tailer.readingDocument()) {
        System.out.println("3. Tailer is Present =  " + dc.isPresent() + ", Tailer Current Index = " + dc.index() + ", End = " + queue.createTailer().toEnd().index());
        if (dc.isPresent()) {
            MyObject result = new MyObject();
            result.readMarshallable(dc.wire());
            System.err.println(result);
        }
    }

    try (final DocumentContext dc = tailer.readingDocument()) {
        System.out.println("4. Tailer is Present =  " + dc.isPresent() + ", Tailer Current Index = " + dc.index() + ", End = " + queue.createTailer().toEnd().index());
    }



    try {
        Thread.sleep(500);
    } catch (Exception e){
        System.out.println(e);
    }
    System.out.println("Tailer is Present =  " + tailer.readingDocument().isPresent() + ", Tailer Current Index = " + tailer.index() + ", End = " + queue.createTailer().toEnd().index());

}