Asuka_Truth
Asuka_Truth

Reputation: 15

How to read from Chronicle Queue while using writingDocument?

this is the version of Chronicle Queue:

<groupId>net.openhft</groupId>
<artifactId>chronicle-queue</artifactId>
<version>5.21.93</version>

this is how I write using appender:

        String basePath = OS.userDir() + "\\start";
        ChronicleQueue queue = SingleChronicleQueueBuilder.single(basePath).build();

        ExcerptAppender appender = queue.acquireAppender();
        DocumentContext documentContext = null;
        int i = 0;
        try {
            documentContext = appender.writingDocument();
            for (int j=0;j<1000;j++) {
                     documentContext.wire().write().text("text :: "+i++);
            }
        } finally {
            documentContext.close();
        }

this is how I read:

        ExcerptTailer b = queue.createTailer("b");

        String out = "";
        while ((out=b.readText())!=null){
            System.out.println(out);
        }

and this is the Exception:

Exception in thread "main" net.openhft.chronicle.bytes.UTFDataFormatRuntimeException: 1010d
00010080                                         00 00 00               ···
........
00010100 00 00 00 00 00 00 00 00  5a 32 00 00 c0 e9 74 65 ········ Z2····te
00010110 78 74 20 3a 3a 20 30 c0  e9 74 65 78 74 20 3a 3a xt :: 0· ·text ::
00010120 20 31 c0 e9 74 65 78 74  20 3a 3a 20 32 c0 e9 74  1··text  :: 2··t
00010130 65 78 74 20 3a 3a 20 33  c0 e9 74 65 78 74 20 3a ext :: 3 ··text :
00010140 3a 20 34 c0 e9 74 65 78  74 20 3a 3a 20 35 c0 e9 : 4··tex t :: 5··
00010150 74 65 78 74 20                                   text             
... truncated
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf8_SB1(BytesInternal.java:503)
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf8(BytesInternal.java:221)
    at net.openhft.chronicle.bytes.StreamingDataInput.parseUtf8(StreamingDataInput.java:553)
    at net.openhft.chronicle.wire.BinaryWire.getStringBuilder(BinaryWire.java:775)
    at net.openhft.chronicle.wire.BinaryWire.readText(BinaryWire.java:1264)
    at net.openhft.chronicle.wire.BinaryWire.readText(BinaryWire.java:1270)
    at net.openhft.chronicle.wire.BinaryWire$BinaryValueIn.textTo(BinaryWire.java:2326)
    at net.openhft.chronicle.wire.ValueIn.text(ValueIn.java:61)
    at net.openhft.chronicle.wire.MarshallableIn.readText(MarshallableIn.java:103)
    at com.cq.test.Test.main(Test.java:48)
Caused by: net.openhft.chronicle.bytes.UTFDataFormatRuntimeException: malformed input around byte 2 was 116 101
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf82(BytesInternal.java:630)
    at net.openhft.chronicle.bytes.internal.BytesInternal.parseUtf8_SB1(BytesInternal.java:500)
    ... 9 more

Process finished with exit code 1

My question is that how can I read data and where can I get this usage(github or their docs somewhere?)

Upvotes: 0

Views: 1462

Answers (2)

Forough
Forough

Reputation: 36

try the following: you can find the documentation in https://github.com/OpenHFT/Chronicle-Queue

 public static void main(String[] args) {
    String basePath = OS.userDir() + "/start";
    ChronicleQueue queue = SingleChronicleQueueBuilder.single(basePath).build();

    ExcerptAppender appender = queue.acquireAppender();
    DocumentContext documentContext = null;
    int i = 0;
    try {
        documentContext = appender.writingDocument();
        for (int j=0;j<10;j++) {
            documentContext.wire().write("No "+i).text(" text :: "+i++);

        }
    }
    finally {
        documentContext.close();
    }

    ExcerptTailer b = queue.createTailer("b");

        int k=0;
        try (final DocumentContext dc = b.readingDocument()) {
            if (!dc.isPresent())
                return;
            for (i=0;i<10 ;i++ ) {
            final String output = dc.wire().read("No "+k++).text();

                System.out.println(output);

        }
    }

}

Upvotes: 0

Related Questions