Skip to content

Commit e8a7be9

Browse files
Align the SimplestCase single host sample with the multi host example. (#235)
- make the "from the start" position argument conform to the contract (AeronArchive.NULL_POSITION) - wait for the counter to become available instead of assuming that it is immediately available ( as it sometimes resulted in error when attempted to read too early). - fix shut down behaviour by attempting to close the resources even if the exception occurred. It used to hang forever by not executing close logic on exception.
1 parent 20746d5 commit e8a7be9

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

archive-core/src/main/java/com/aeroncookbook/archive/SimplestCase.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ public class SimplestCase
6262
public static void main(final String[] args)
6363
{
6464
final SimplestCase simplestCase = new SimplestCase();
65-
simplestCase.setup();
66-
LOGGER.info("Writing");
67-
simplestCase.write();
68-
LOGGER.info("Reading");
69-
simplestCase.read();
70-
simplestCase.cleanUp();
65+
try
66+
{
67+
simplestCase.setup();
68+
LOGGER.info("Writing");
69+
simplestCase.write();
70+
LOGGER.info("Reading");
71+
simplestCase.read();
72+
}
73+
finally
74+
{
75+
simplestCase.cleanUp();
76+
}
7177
}
7278

7379
private void cleanUp()
@@ -85,7 +91,7 @@ private void read()
8591
.aeron(aeron)))
8692
{
8793
final long recordingId = findLatestRecording(reader, channel, streamCapture);
88-
final long position = 0L;
94+
final long position = AeronArchive.NULL_POSITION;
8995
final long length = Long.MAX_VALUE;
9096

9197
final long sessionId = reader.startReplay(recordingId, position, length, channel, streamReplay);
@@ -128,7 +134,12 @@ private void write()
128134

129135
final long stopPosition = publication.position();
130136
final CountersReader countersReader = aeron.countersReader();
131-
final int counterId = RecordingPos.findCounterIdBySession(countersReader, publication.sessionId());
137+
int counterId = RecordingPos.findCounterIdBySession(countersReader, publication.sessionId());
138+
while (CountersReader.NULL_COUNTER_ID == counterId)
139+
{
140+
idleStrategy.idle();
141+
counterId = RecordingPos.findCounterIdBySession(countersReader, publication.sessionId());
142+
}
132143

133144
while (countersReader.getCounterValue(counterId) < stopPosition)
134145
{

0 commit comments

Comments
 (0)