Search in sources :

Example 21 with Subscription

use of io.aeron.Subscription in project Aeron by real-logic.

the class MultipleSubscribersWithFragmentAssembly method eventUnavailableImage.

/**
 * This handler is called when image is unavailable.
 *
 * @param image that has gone inactive.
 */
public static void eventUnavailableImage(final Image image) {
    final Subscription subscription = image.subscription();
    System.out.format("inactive image on %s streamId %d sessionId %x%n", subscription.channel(), subscription.streamId(), image.sessionId());
}
Also used : Subscription(io.aeron.Subscription)

Example 22 with Subscription

use of io.aeron.Subscription in project Aeron by real-logic.

the class Pong method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context ctx = new Aeron.Context();
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    if (INFO_FLAG) {
        ctx.availableImageHandler(SamplesUtil::printAvailableImage);
        ctx.unavailableImageHandler(SamplesUtil::printUnavailableImage);
    }
    final IdleStrategy idleStrategy = new BusySpinIdleStrategy();
    System.out.println("Subscribing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
    System.out.println("Publishing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
    System.out.println("Using exclusive publications " + EXCLUSIVE_PUBLICATIONS);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID);
        Publication publication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PONG_CHANNEL, PONG_STREAM_ID) : aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID)) {
        final BufferClaim bufferClaim = new BufferClaim();
        final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> pingHandler(bufferClaim, publication, buffer, offset, length, header);
        while (running.get()) {
            idleStrategy.idle(subscription.poll(fragmentHandler, FRAME_COUNT_LIMIT));
        }
        System.out.println("Shutting down...");
    }
    CloseHelper.close(driver);
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) Subscription(io.aeron.Subscription) io.aeron.logbuffer(io.aeron.logbuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Publication(io.aeron.Publication) CloseHelper(org.agrona.CloseHelper) SigInt(org.agrona.concurrent.SigInt) DirectBuffer(org.agrona.DirectBuffer) BusySpinIdleStrategy(org.agrona.concurrent.BusySpinIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BusySpinIdleStrategy(org.agrona.concurrent.BusySpinIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) Publication(io.aeron.Publication) Aeron(io.aeron.Aeron) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) BusySpinIdleStrategy(org.agrona.concurrent.BusySpinIdleStrategy) Subscription(io.aeron.Subscription)

Example 23 with Subscription

use of io.aeron.Subscription in project Aeron by real-logic.

the class BasicSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(STREAM_ID);
    final AtomicBoolean running = new AtomicBoolean(true);
    // Register a SIGINT handler for graceful shutdown.
    SigInt.register(() -> running.set(false));
    // clean up resources when this try block is finished
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
        System.out.println("Shutting down...");
    }
    CloseHelper.close(driver);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 24 with Subscription

use of io.aeron.Subscription in project Aeron by real-logic.

the class EmbeddedPingPong method runPing.

private static void runPing(final Aeron aeron) throws InterruptedException {
    System.out.println("Publishing Ping at " + PING_CHANNEL + " on stream id " + PING_STREAM_ID);
    System.out.println("Subscribing Pong at " + PONG_CHANNEL + " on stream id " + PONG_STREAM_ID);
    System.out.println("Message payload length of " + MESSAGE_LENGTH + " bytes");
    System.out.println("Using exclusive publications: " + EXCLUSIVE_PUBLICATIONS);
    final FragmentAssembler dataHandler = new FragmentAssembler(EmbeddedPingPong::pongHandler);
    try (Subscription pongSubscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID, EmbeddedPingPong::availablePongImageHandler, null);
        Publication pingPublication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PING_CHANNEL, PING_STREAM_ID) : aeron.addPublication(PING_CHANNEL, PING_STREAM_ID)) {
        System.out.println("Waiting for new image from Pong...");
        PONG_IMAGE_LATCH.await();
        System.out.format("Warming up... %d iterations of %,d messages%n", WARMUP_NUMBER_OF_ITERATIONS, WARMUP_NUMBER_OF_MESSAGES);
        for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
            roundTripMessages(dataHandler, pingPublication, pongSubscription, WARMUP_NUMBER_OF_MESSAGES);
            Thread.yield();
        }
        Thread.sleep(100);
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        do {
            HISTOGRAM.reset();
            System.out.format("Pinging %,d messages%n", NUMBER_OF_MESSAGES);
            roundTripMessages(dataHandler, pingPublication, pongSubscription, NUMBER_OF_MESSAGES);
            System.out.println("Histogram of RTT latencies in microseconds.");
            HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
        } while (barrier.await());
    }
}
Also used : Publication(io.aeron.Publication) ContinueBarrier(org.agrona.console.ContinueBarrier) Subscription(io.aeron.Subscription) FragmentAssembler(io.aeron.FragmentAssembler)

Example 25 with Subscription

use of io.aeron.Subscription in project Aeron by real-logic.

the class ReplayedBasicSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
    final FragmentHandler fragmentHandler = SamplesUtil.printAsciiMessage(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() + 2);
    try (AeronArchive archive = AeronArchive.connect(archiveCtx)) {
        final long recordingId = findLatestRecording(archive);
        final long position = 0L;
        final long length = Long.MAX_VALUE;
        final long sessionId = archive.startReplay(recordingId, position, length, CHANNEL, REPLAY_STREAM_ID);
        final String channel = ChannelUri.addSessionId(CHANNEL, (int) sessionId);
        try (Subscription subscription = archive.context().aeron().addSubscription(channel, REPLAY_STREAM_ID)) {
            SamplesUtil.subscriberLoop(fragmentHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription);
            System.out.println("Shutting down...");
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) AeronArchive(io.aeron.archive.client.AeronArchive)

Aggregations

Subscription (io.aeron.Subscription)55 Aeron (io.aeron.Aeron)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 Publication (io.aeron.Publication)19 MediaDriver (io.aeron.driver.MediaDriver)16 IdleStrategy (org.agrona.concurrent.IdleStrategy)13 FragmentAssembler (io.aeron.FragmentAssembler)12 ContinueBarrier (org.agrona.console.ContinueBarrier)10 FragmentHandler (io.aeron.logbuffer.FragmentHandler)8 TimeUnit (java.util.concurrent.TimeUnit)6 BackoffIdleStrategy (org.agrona.concurrent.BackoffIdleStrategy)5 Test (org.junit.jupiter.api.Test)5 Context (io.aeron.archive.client.AeronArchive.Context)4 io.aeron.logbuffer (io.aeron.logbuffer)4 ExecutorService (java.util.concurrent.ExecutorService)4 SigInt (org.agrona.concurrent.SigInt)4 InOrder (org.mockito.InOrder)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 DirectBuffer (org.agrona.DirectBuffer)3 CommonContext (io.aeron.CommonContext)2