Search in sources :

Example 16 with Subscription

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

the class RateSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the task is interrupted
 * @throws ExecutionException if the {@link Future} has an error.
 */
public static void main(final String[] args) throws InterruptedException, ExecutionException {
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final ExecutorService executor = Executors.newFixedThreadPool(1);
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), SamplesUtil::printRate);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> {
        reporter.halt();
        running.set(false);
    });
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        final Future<?> future = executor.submit(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        reporter.run();
        System.out.println("Shutting down...");
        future.get();
    }
    executor.shutdown();
    if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
        System.out.println("Warning: not all tasks completed promptly");
    }
    CloseHelper.close(driver);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 17 with Subscription

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

the class EmbeddedThroughput method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the thread is interrupted during linger.
 */
public static void main(final String[] args) throws InterruptedException {
    loadPropertiesFiles(args);
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);
    try (MediaDriver mediaDriver = MediaDriver.launch();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
        executor.execute(reporter);
        executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
        do {
            System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
            printingActive = true;
            long backPressureCount = 0;
            for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
                OFFER_BUFFER.putLong(0, i);
                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0) {
                    backPressureCount++;
                    idleStrategy.idle();
                }
            }
            System.out.println("Done streaming. backPressureRatio=" + ((double) backPressureCount / NUMBER_OF_MESSAGES));
            if (LINGER_TIMEOUT_MS > 0) {
                System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
                Thread.sleep(LINGER_TIMEOUT_MS);
            }
            printingActive = false;
        } while (barrier.await());
        running.set(false);
        reporter.halt();
        executor.shutdown();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) IdleStrategy(org.agrona.concurrent.IdleStrategy) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentPublication(io.aeron.ConcurrentPublication) Publication(io.aeron.Publication) ContinueBarrier(org.agrona.console.ContinueBarrier) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 18 with Subscription

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

the class RateSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws InterruptedException if the task is interrupted
 * @throws ExecutionException if the {@link Future} has an error.
 */
public static void main(final String[] args) throws InterruptedException, ExecutionException {
    System.out.println("Subscribing to " + CHANNEL + " on stream id " + STREAM_ID);
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final ExecutorService executor = Executors.newFixedThreadPool(1);
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(SamplesUtil::printAvailableImage).unavailableImageHandler(SamplesUtil::printUnavailableImage);
    if (EMBEDDED_MEDIA_DRIVER) {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }
    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), SamplesUtil::printRate);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> {
        reporter.halt();
        running.set(false);
    });
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        final Future<?> future = executor.submit(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
        reporter.run();
        System.out.println("Shutting down...");
        future.get();
    }
    executor.shutdown();
    if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
        System.out.println("Warning: not all tasks completed promptly");
    }
    CloseHelper.close(driver);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 19 with Subscription

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

the class MultipleSubscribersWithFragmentAssembly method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n", CHANNEL, STREAM_ID_1, STREAM_ID_2);
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage).unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);
    final FragmentAssembler assembler1 = new FragmentAssembler(reassembledMessage1(STREAM_ID_1));
    final FragmentAssembler assembler2 = new FragmentAssembler(reassembledMessage2(STREAM_ID_2));
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
        Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2)) {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
        int idleCount = 0;
        while (running.get()) {
            final int fragmentsRead1 = subscription1.poll(assembler1, FRAGMENT_COUNT_LIMIT);
            final int fragmentsRead2 = subscription2.poll(assembler2, FRAGMENT_COUNT_LIMIT);
            if ((fragmentsRead1 + fragmentsRead2) == 0) {
                idleStrategy.idle(idleCount++);
            } else {
                idleCount = 0;
            }
        }
        System.out.println("Shutting down...");
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Example 20 with Subscription

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

the class MultipleSubscribersWithFragmentAssembly method eventAvailableImage.

/**
 * Print the information for an available image to stdout.
 *
 * @param image that has been created.
 */
public static void eventAvailableImage(final Image image) {
    final Subscription subscription = image.subscription();
    System.out.format("new image on %s streamId %x sessionId %x from %s%n", subscription.channel(), subscription.streamId(), image.sessionId(), image.sourceIdentity());
}
Also used : Subscription(io.aeron.Subscription)

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