use of io.aeron.Publication in project Aeron by real-logic.
the class RecordingDescriptorCollectorTest method createRecordings.
private void createRecordings(final AeronArchive aeronArchive, final int numRecordings) {
final UnsafeBuffer message = new UnsafeBuffer("this is some data".getBytes());
for (int i = 0; i < numRecordings; i++) {
try (Publication publication = aeronArchive.addRecordedPublication("aeron:ipc?ssc=true", 10000 + i)) {
long expectedPosition;
while ((expectedPosition = publication.offer(message, 0, message.capacity())) < 0) {
Tests.yield();
}
long recordingId;
while ((recordingId = aeronArchive.findLastMatchingRecording(0, "aeron:ipc", publication.streamId(), publication.sessionId())) == Aeron.NULL_VALUE) {
Tests.yield();
}
while (expectedPosition < aeronArchive.getRecordingPosition(recordingId)) {
Tests.yield();
}
}
}
}
use of io.aeron.Publication in project Aeron by real-logic.
the class RecordedBasicPublisher method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread sleep delay is interrupted.
*/
public static void main(final String[] args) throws InterruptedException {
System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
// Create a unique response stream id so not to clash with other archive clients.
final AeronArchive.Context archiveCtx = new AeronArchive.Context().controlResponseStreamId(AeronArchive.Configuration.controlResponseStreamId() + 1);
try (AeronArchive archive = AeronArchive.connect(archiveCtx)) {
archive.startRecording(CHANNEL, STREAM_ID, SourceLocation.LOCAL);
try (Publication publication = archive.context().aeron().addPublication(CHANNEL, STREAM_ID)) {
final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
// Wait for recording to have started before publishing.
final CountersReader counters = archive.context().aeron().countersReader();
int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
while (CountersReader.NULL_COUNTER_ID == counterId) {
if (!running.get()) {
return;
}
idleStrategy.idle();
counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());
}
final long recordingId = RecordingPos.getRecordingId(counters, counterId);
System.out.println("Recording started: recordingId = " + recordingId);
for (int i = 0; i < NUMBER_OF_MESSAGES && running.get(); i++) {
final String message = "Hello World! " + i;
final byte[] messageBytes = message.getBytes();
BUFFER.putBytes(0, messageBytes);
System.out.print("Offering " + i + "/" + NUMBER_OF_MESSAGES + " - ");
final long result = publication.offer(BUFFER, 0, messageBytes.length);
checkResult(result);
final String errorMessage = archive.pollForErrorResponse();
if (null != errorMessage) {
throw new IllegalStateException(errorMessage);
}
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
idleStrategy.reset();
while (counters.getCounterValue(counterId) < publication.position()) {
if (!RecordingPos.isActive(counters, counterId, recordingId)) {
throw new IllegalStateException("recording has stopped unexpectedly: " + recordingId);
}
idleStrategy.idle();
}
} finally {
System.out.println("Done sending.");
archive.stopRecording(CHANNEL, STREAM_ID);
}
}
}
Aggregations