Search in sources :

Example 51 with Publication

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();
            }
        }
    }
}
Also used : Publication(io.aeron.Publication) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 52 with Publication

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);
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Publication(io.aeron.Publication) AeronArchive(io.aeron.archive.client.AeronArchive) CountersReader(org.agrona.concurrent.status.CountersReader)

Aggregations

Publication (io.aeron.Publication)52 Aeron (io.aeron.Aeron)30 MediaDriver (io.aeron.driver.MediaDriver)15 Test (org.junit.jupiter.api.Test)15 Subscription (io.aeron.Subscription)14 CountersReader (org.agrona.concurrent.status.CountersReader)13 InterruptAfter (io.aeron.test.InterruptAfter)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 FragmentAssembler (io.aeron.FragmentAssembler)8 File (java.io.File)8 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)8 ContinueBarrier (org.agrona.console.ContinueBarrier)8 DirectBuffer (org.agrona.DirectBuffer)7 AeronArchive (io.aeron.archive.client.AeronArchive)6 ChannelUriStringBuilder (io.aeron.ChannelUriStringBuilder)5 ThreadingMode (io.aeron.driver.ThreadingMode)5 RecordingPos (io.aeron.archive.status.RecordingPos)4 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)4 Tests (io.aeron.test.Tests)4 CloseHelper (org.agrona.CloseHelper)4