如何在使用writingDocument时从Chronicle队列中读取



这是Chronicle Queue的版本:

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

这就是我使用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();
}

我是这样读的:

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

这就是例外:

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

我的问题是,我如何读取数据,在哪里可以获得这种用法(github或他们的文档?(

尝试以下操作:您可以在中找到文档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);
}
}
}

用户指南如下:https://github.com/OpenHFT/Chronicle-Queue#user-引导

正在写入队列:https://github.com/OpenHFT/Chronicle-Queue#writing-对排队-排队-应答器

正在从队列中读取:https://github.com/OpenHFT/Chronicle-Queue#reading-从排队到尾随

最新更新