我正在使用 Chronicle-queue (5.16.13) 将 json 值写入和读取到编年史文件。要编写对象,我在循环中使用以下内容
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().write(() -> "msg").text("Hallo asdf");
System.out.println("your data was store to index="+ dc.index());
return true;
} catch (Exception e) {
logger.warn("Unable to store value to chronicle", e);
return false;
}
并阅读项目我在循环中执行以下调用
DocumentContext documentContext;
do {
documentContext = tailer.readingDocument();
currentOffset = documentContext.index();
System.out.println("Current offset: " + currentOffset);
} while (!documentContext.isData());
我观察到变量currentOffset
没有改变,并且在一段时间后(取决于有效负载大小,似乎)循环变为无限并且当前偏移量具有疯狂的值。第一个循环的输出(缩短)是
Writing 0
your data was store to index=76385993359360
Writing 1
your data was store to index=76385993359361
Writing 2
your data was store to index=76385993359362
Writing 3
your data was store to index=76385993359363
Writing 4
your data was store to index=76385993359364
Writing 5
your data was store to index=76385993359365
Writing 6
your data was store to index=76385993359366
Writing 7
your data was store to index=76385993359367
Writing 8
your data was store to index=76385993359368
Writing 9
your data was store to index=76385993359369
Writing 10
your data was store to index=76385993359370
Writing 11
your data was store to index=76385993359371
Writing 12
your data was store to index=76385993359372
Writing 13
your data was store to index=76385993359373
Writing 14
your data was store to index=76385993359374
Writing 15
your data was store to index=76385993359375
Writing 16
your data was store to index=76385993359376
Writing 17
your data was store to index=76385993359377
Writing 18
your data was store to index=76385993359378
Writing 19
your data was store to index=76385993359379
Writing 20
your data was store to index=76385993359380
Writing 21
your data was store to index=76385993359381
Writing 22
your data was store to index=76385993359382
Writing 23
your data was store to index=76385993359383
Writing 24
your data was store to index=76385993359384
Writing 25
your data was store to index=76385993359385
Writing 26
your data was store to index=76385993359386
对于第二个循环
Reading 0
Current offset: 76385993359360
Reading 1
Current offset: 76385993359360
Reading 2
Current offset: 76385993359360
Reading 3
Current offset: 76385993359360
Reading 4
Current offset: 76385993359360
Reading 5
Current offset: 76385993359360
Reading 6
Current offset: 76385993359360
Reading 7
Current offset: 76385993359360
Reading 8
Current offset: 76385993359360
Reading 9
Current offset: 76385993359360
Reading 10
Current offset: 76385993359360
Reading 11
Current offset: 76385993359360
Reading 12
Current offset: 76385993359360
Reading 13
Current offset: 76385993359360
Reading 14
Current offset: 76385993359360
Reading 15
Current offset: 76385993359360
Reading 16
Current offset: 76385993359360
Reading 17
Current offset: 76385993359360
Reading 18
Current offset: 76385993359360
Reading 19
Current offset: 76385993359360
Reading 20
Current offset: 76385993359360
Reading 21
Current offset: 76385993359360
Reading 22
Current offset: 76385993359360
Reading 23
Current offset: 76385993359360
Reading 24
Current offset: 76385993359360
Reading 25
Current offset: -9223372036854775808
我做错了什么吗?那么有人可以提示我正确的用法吗?
非常感谢!
编辑:添加了最小的工作示例
以下单元测试对我来说失败了。
@Test
public void fails() throws Exception {
String basePath = System.getProperty("java.io.tmpdir");
String path = Files.createTempDirectory(Paths.get(basePath), "chronicle-")
.toAbsolutePath()
.toString();
logger.info("Using temp path '{}'", path);
SingleChronicleQueue chronicleQueue = SingleChronicleQueueBuilder
.single()
.path(path)
.build();
// Create Appender
ExcerptAppender appender = chronicleQueue.acquireAppender();
// Create Tailer
ExcerptTailer tailer = chronicleQueue.createTailer();
tailer.toStart();
int numberOfRecords = 10;
// Write
for (int i = 0; i <= numberOfRecords; i++) {
System.out.println("Writing " + i);
try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().write(() -> "msg").text("Hello World!");
System.out.println("your data was store to index=" + dc.index());
} catch (Exception e) {
logger.warn("Unable to store value to chronicle", e);
}
}
// Read
for (int i = 0; i <= numberOfRecords; i++) {
System.out.println("Reading " + i);
DocumentContext documentContext = tailer.readingDocument();
long currentOffset = documentContext.index();
System.out.println("Current offset: " + currentOffset);
Wire wire = documentContext.wire();
if (wire != null) {
String msg = wire
.read("msg")
.text();
}
}
chronicleQueue.close();
}
输出是
Writing 0
your data was store to index=76385993359360
Writing 1
your data was store to index=76385993359361
Writing 2
your data was store to index=76385993359362
Writing 3
your data was store to index=76385993359363
Writing 4
your data was store to index=76385993359364
Writing 5
your data was store to index=76385993359365
Writing 6
your data was store to index=76385993359366
Writing 7
your data was store to index=76385993359367
Writing 8
your data was store to index=76385993359368
Writing 9
your data was store to index=76385993359369
Writing 10
your data was store to index=76385993359370
Reading 0
Current offset: 76385993359360
Reading 1
Current offset: 76385993359360
Reading 2
Current offset: 76385993359360
Reading 3
Current offset: 76385993359360
Reading 4
Current offset: -9223372036854775808
Reading 5
Current offset: -9223372036854775808
Reading 6
Current offset: -9223372036854775808
Reading 7
Current offset: -9223372036854775808
Reading 8
Current offset: -9223372036854775808
Reading 9
Current offset: -9223372036854775808
Reading 10
Current offset: -9223372036854775808