通过Aeron Archive的API,可以使用以下代码段扩展现有录制:
archive.tryExtendRecording(extendPosition, streamId, sourceIdentity);
其中,extendPosition是扩展录制的初始位置,streamId是录制的数据流ID,sourceIdentity是录制数据的源。
如果扩展录制成功,将返回扩展录制的新记录ID。如果扩展失败,将返回零。
以下是一个完整的代码示例:
public static void tryExtendRecording(AeronArchive archive, long position, int streamId, String sourceIdentity) {
final Publication publication = archive.addRecordedExclusivePublication(streamId, position);
if (publication == null) {
System.out.println("Unable to extend recording, no image available");
return;
}
final long recordedId = archive.tryExtendRecording(position, streamId, sourceIdentity);
if (recordedId == Aeron.NULL_VALUE) {
System.out.println("Unable to extend recording");
return;
}
try (Subscription subscription = archive.replay(recordedId, 0, AeronArchive.NULL_LENGTH, sourceIdentity)) {
final Image image = subscription.imageAtIndex(0);
final MutableDirectBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(1024));
int messages = 0;
while (image.poll(buffer, 0, buffer.capacity()) > 0) {
messages++;
}
System.out.println("Replayed " + messages + " messages at " + image.position());
}
System.out.println("Extended recording to " + recordedId + " at " + position);
}