Search in sources :

Example 6 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)

Example 7 with Subscription

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

the class Pong method main.

public static void main(final String[] args) throws Exception {
    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);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (Aeron aeron = Aeron.connect(ctx);
        Publication pongPublication = aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID);
        Subscription pingSubscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID)) {
        final FragmentAssembler dataHandler = new FragmentAssembler((buffer, offset, length, header) -> pingHandler(pongPublication, buffer, offset, length));
        while (running.get()) {
            idleStrategy.idle(pingSubscription.poll(dataHandler, FRAME_COUNT_LIMIT));
        }
        System.out.println("Shutting down...");
    }
    CloseHelper.quietClose(driver);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) BusySpinIdleStrategy(org.agrona.concurrent.BusySpinIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BusySpinIdleStrategy(org.agrona.concurrent.BusySpinIdleStrategy) Publication(io.aeron.Publication) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Example 8 with Subscription

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

the class RateSubscriber method main.

public static void main(final String[] args) throws Exception {
    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 FragmentHandler rateReporterHandler = new FragmentAssembler(rateReporterHandler(reporter));
    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, 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.quietClose(driver);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Example 9 with Subscription

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

the class SamplesUtil method printAvailableImage.

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

Example 10 with Subscription

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

the class SamplesUtil method printUnavailableImage.

/**
     * Print the information for an unavailable image to stdout.
     *
     * @param image that has gone inactive
     */
public static void printUnavailableImage(final Image image) {
    final Subscription subscription = image.subscription();
    System.out.println(String.format("Unavailable image on %s streamId=%d sessionId=%d", subscription.channel(), subscription.streamId(), image.sessionId()));
}
Also used : Subscription(io.aeron.Subscription)

Aggregations

Subscription (io.aeron.Subscription)12 Aeron (io.aeron.Aeron)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 FragmentAssembler (io.aeron.FragmentAssembler)5 Publication (io.aeron.Publication)4 MediaDriver (io.aeron.driver.MediaDriver)4 FragmentHandler (io.aeron.logbuffer.FragmentHandler)4 IdleStrategy (org.agrona.concurrent.IdleStrategy)3 ExecutorService (java.util.concurrent.ExecutorService)2 BackoffIdleStrategy (org.agrona.concurrent.BackoffIdleStrategy)2 ContinueBarrier (org.agrona.console.ContinueBarrier)2 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 BusySpinIdleStrategy (org.agrona.concurrent.BusySpinIdleStrategy)1 SigInt (org.agrona.concurrent.SigInt)1