Java Code Examples for io.aeron.Aeron#Context

The following examples show how to use io.aeron.Aeron#Context . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteParameterServerClientTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private Aeron.Context getContext() {
    if (ctx == null)
        ctx = new Aeron.Context().publicationConnectionTimeout(-1)
                        .availableImageHandler(AeronUtil::printAvailableImage)
                        .unavailableImageHandler(AeronUtil::printUnavailableImage)
                        .aeronDirectoryName(mediaDriver.aeronDirectoryName()).keepAliveInterval(1000)
                        .errorHandler(e -> log.error(e.toString(), e));
    return ctx;
}
 
Example 2
Source File: AeronNDArrayPublisher.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private void init() {
    channel = channel == null ? "aeron:udp?endpoint=localhost:40123" : channel;
    streamId = streamId == 0 ? 10 : streamId;
    publishRetryTimeOut = publishRetryTimeOut == 0 ? 3000 : publishRetryTimeOut;
    ctx = ctx == null ? ctx = new Aeron.Context() : ctx;
    init = true;
    log.info("Channel publisher" + channel + " and stream " + streamId);
}
 
Example 3
Source File: ParameterServerClientPartialTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private static Aeron.Context getContext() {
    if (ctx == null)
        ctx = new Aeron.Context().publicationConnectionTimeout(-1)
                        .availableImageHandler(AeronUtil::printAvailableImage)
                        .unavailableImageHandler(AeronUtil::printUnavailableImage)
                        .aeronDirectoryName(mediaDriver.aeronDirectoryName()).keepAliveInterval(10000)
                        .errorHandler(e -> log.error(e.toString(), e));
    return ctx;
}
 
Example 4
Source File: ParameterServerClientTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private static Aeron.Context getContext() {
    return new Aeron.Context().publicationConnectionTimeout(-1)
                    .availableImageHandler(AeronUtil::printAvailableImage)
                    .unavailableImageHandler(AeronUtil::printUnavailableImage)
                    .aeronDirectoryName(mediaDriver.aeronDirectoryName()).keepAliveInterval(1000)
                    .errorHandler(e -> log.error(e.toString(), e));
}
 
Example 5
Source File: BasicSubscriber.java    From aeron with Apache License 2.0 5 votes vote down vote up
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.printStringMessage(STREAM_ID);
    final AtomicBoolean running = new AtomicBoolean(true);

    // Register a SIGINT handler for graceful shutdown.
    SigInt.register(() -> running.set(false));

    // Create an Aeron instance using the configured Context and create a
    // Subscription on that instance that subscribes to the configured
    // channel and stream ID.
    // The Aeron and Subscription classes implement "AutoCloseable" and will automatically
    // 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.quietClose(driver);
}
 
Example 6
Source File: LargeNdArrayIpcTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private Aeron.Context getContext() {
    if (ctx == null)
        ctx = new Aeron.Context().publicationConnectionTimeout(-1)
                        .availableImageHandler(AeronUtil::printAvailableImage)
                        .unavailableImageHandler(AeronUtil::printUnavailableImage)
                        .aeronDirectoryName(mediaDriver.aeronDirectoryName()).keepAliveInterval(10000)
                        .errorHandler(err -> err.printStackTrace());
    return ctx;
}
 
Example 7
Source File: AeronNDArrayResponder.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void init() {
    ctx = ctx == null ? new Aeron.Context() : ctx;
    channel = channel == null ? "aeron:udp?endpoint=localhost:40123" : channel;
    fragmentLimitCount = fragmentLimitCount == 0 ? 5000 : fragmentLimitCount;
    streamId = streamId == 0 ? 10 : streamId;
    responseStreamId = responseStreamId == 0 ? -1 : responseStreamId;
    running = running == null ? new AtomicBoolean(true) : running;
    if (ndArrayHolder == null)
        throw new IllegalStateException("NDArray callback must be specified in the builder.");
    init.set(true);
    launched = new AtomicBoolean(false);
    log.info("Channel subscriber " + channel + " and stream id " + streamId);
}
 
Example 8
Source File: ParameterServerNodeTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private static Aeron.Context getContext() {
    return new Aeron.Context().publicationConnectionTimeout(-1)
                    .availableImageHandler(AeronUtil::printAvailableImage)
                    .unavailableImageHandler(AeronUtil::printUnavailableImage)
                    .aeronDirectoryName(mediaDriver.aeronDirectoryName()).keepAliveInterval(1000)
                    .errorHandler(e -> log.error(e.toString(), e));
}
 
Example 9
Source File: NdArrayIpcTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private Aeron.Context getContext() {
    if (ctx == null)
        ctx = new Aeron.Context().publicationConnectionTimeout(1000)
                        .availableImageHandler(image -> System.out.println(image))
                        .unavailableImageHandler(AeronUtil::printUnavailableImage)
                        .aeronDirectoryName(mediaDriver.aeronDirectoryName()).keepAliveInterval(1000)
                        .errorHandler(e -> log.error(e.toString(), e));
    return ctx;
}
 
Example 10
Source File: RateSubscriber.java    From aeron with Apache License 2.0 4 votes vote down vote up
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 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.quietClose(driver);
}
 
Example 11
Source File: Pong.java    From aeron with Apache License 2.0 4 votes vote down vote up
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.quietClose(driver);
}
 
Example 12
Source File: MultipleSubscribersWithFragmentAssembly.java    From aeron with Apache License 2.0 4 votes vote down vote up
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 dataHandler1 = new FragmentAssembler(reassembledStringMessage1(STREAM_ID_1));
    final FragmentAssembler dataHandler2 = new FragmentAssembler(reassembledStringMessage2(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(dataHandler1, FRAGMENT_COUNT_LIMIT);
            final int fragmentsRead2 = subscription2.poll(dataHandler2, FRAGMENT_COUNT_LIMIT);

            if ((fragmentsRead1 + fragmentsRead2) == 0)
            {
                idleStrategy.idle(idleCount++);
            }
            else
            {
                idleCount = 0;
            }
        }

        System.out.println("Shutting down...");
    }
}
 
Example 13
Source File: DefaultLibraryScheduler.java    From artio with Apache License 2.0 4 votes vote down vote up
public void configure(final Aeron.Context aeronContext)
{
    aeronContext.useConductorAgentInvoker(true);
}
 
Example 14
Source File: SimpleSubscriber.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args)
{
    // Maximum number of message fragments to receive during a single 'poll' operation
    final int fragmentLimitCount = 10;

    // The channel (an endpoint identifier) to receive messages from
    final String channel = "aeron:udp?endpoint=localhost:40123";

    // A unique identifier for a stream within a channel. Stream ID 0 is reserved
    // for internal use and should not be used by applications.
    final int streamId = 10;

    System.out.println("Subscribing to " + channel + " on stream id " + streamId);

    final AtomicBoolean running = new AtomicBoolean(true);
    // Register a SIGINT handler for graceful shutdown.
    SigInt.register(() -> running.set(false));

    // dataHandler method is called for every new datagram received
    final FragmentHandler fragmentHandler =
        (buffer, offset, length, header) ->
        {
            final byte[] data = new byte[length];
            buffer.getBytes(offset, data);

            System.out.println(String.format(
                "Received message (%s) to stream %d from session %x term id %x term offset %d (%d@%d)",
                new String(data), streamId, header.sessionId(),
                header.termId(), header.termOffset(), length, offset));

            // Received the intended message, time to exit the program
            running.set(false);
        };

    // Create a context, needed for client connection to media driver
    // A separate media driver process need to run prior to running this application
    final Aeron.Context ctx = new Aeron.Context();

    // Create an Aeron instance with client-provided context configuration, connect to the
    // media driver, and add a subscription for the given channel and stream using the supplied
    // dataHandler method, which will be called with new messages as they are received.
    // The Aeron and Subscription classes implement AutoCloseable, and will automatically
    // clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription = aeron.addSubscription(channel, streamId))
    {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(
            100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));

        // Try to read the data from subscriber
        while (running.get())
        {
            // poll delivers messages to the dataHandler as they arrive
            // and returns number of fragments read, or 0
            // if no data is available.
            final int fragmentsRead = subscription.poll(fragmentHandler, fragmentLimitCount);
            // Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
            // use if no messages were received.
            idleStrategy.idle(fragmentsRead);
        }

        System.out.println("Shutting down...");
    }
}
 
Example 15
Source File: AeronNDArraySubscriber.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Start a subscriber in another thread
 * based on the given parameters
 * @param context the context to use
 * @param host the host opName to bind to
 * @param port the port to bind to
 * @param callback the call back to use for the subscriber
 * @param streamId the stream id to subscribe to
 * @return the subscriber reference
 */
public static AeronNDArraySubscriber startSubscriber(Aeron.Context context, String host, int port,
                NDArrayCallback callback, int streamId, AtomicBoolean running) {

    AeronNDArraySubscriber subscriber = AeronNDArraySubscriber.builder().streamId(streamId).ctx(context)
                    .channel(AeronUtil.aeronChannel(host, port)).running(running).ndArrayCallback(callback).build();


    Thread t = new Thread(() -> {
        try {
            subscriber.launch();
        } catch (Exception e) {
            e.printStackTrace();
        }

    });

    t.start();


    return subscriber;
}
 
Example 16
Source File: StreamingPublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    if (MESSAGE_LENGTH < SIZE_OF_LONG)
    {
        throw new IllegalArgumentException("Message length must be at least " + SIZE_OF_LONG + " bytes");
    }

    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
    final Aeron.Context context = new Aeron.Context();

    if (EMBEDDED_MEDIA_DRIVER)
    {
        context.aeronDirectoryName(driver.aeronDirectoryName());
    }

    final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), StreamingPublisher::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(1);

    executor.execute(reporter);

    // Connect to media driver and add publication to send messages on the configured channel and stream ID.
    // The Aeron and Publication classes implement AutoCloseable, and will automatically
    // clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(context);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
        final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();

        do
        {
            printingActive = true;

            System.out.format(
                "%nStreaming %,d messages of%s size %d bytes to %s on stream id %d%n",
                NUMBER_OF_MESSAGES,
                RANDOM_MESSAGE_LENGTH ? " random" : "",
                MESSAGE_LENGTH,
                CHANNEL,
                STREAM_ID);

            long backPressureCount = 0;

            for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
            {
                final int length = LENGTH_GENERATOR.getAsInt();

                OFFER_BUFFER.putLong(0, i);
                idleStrategy.reset();
                while (publication.offer(OFFER_BUFFER, 0, length, null) < 0L)
                {
                    // The offer failed, which is usually due to the publication
                    // being temporarily blocked.  Retry the offer after a short
                    // spin/yield/sleep, depending on the chosen IdleStrategy.
                    backPressureCount++;
                    idleStrategy.idle();
                }

                reporter.onMessage(length);
            }

            System.out.println(
                "Done streaming. Back pressure ratio " + ((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());
    }

    reporter.halt();
    executor.shutdown();
    CloseHelper.quietClose(driver);
}
 
Example 17
Source File: BasicPublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    System.out.println("Publishing to " + CHANNEL + " on stream id " + STREAM_ID);

    // If configured to do so, create an embedded media driver within this application rather
    // than relying on an external one.
    final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;

    final Aeron.Context ctx = new Aeron.Context();
    if (EMBEDDED_MEDIA_DRIVER)
    {
        ctx.aeronDirectoryName(driver.aeronDirectoryName());
    }

    // Connect a new Aeron instance to the media driver and create a publication on
    // the given channel and stream ID.
    // The Aeron and Publication classes implement "AutoCloseable" and will automatically
    // clean up resources when this try block is finished
    try (Aeron aeron = Aeron.connect(ctx);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID))
    {
        for (long i = 0; i < NUMBER_OF_MESSAGES; 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);

            if (result < 0L)
            {
                if (result == Publication.BACK_PRESSURED)
                {
                    System.out.println("Offer failed due to back pressure");
                }
                else if (result == Publication.NOT_CONNECTED)
                {
                    System.out.println("Offer failed because publisher is not connected to subscriber");
                }
                else if (result == Publication.ADMIN_ACTION)
                {
                    System.out.println("Offer failed because of an administration action in the system");
                }
                else if (result == Publication.CLOSED)
                {
                    System.out.println("Offer failed publication is closed");
                    break;
                }
                else if (result == Publication.MAX_POSITION_EXCEEDED)
                {
                    System.out.println("Offer failed due to publication reaching max position");
                    break;
                }
                else
                {
                    System.out.println("Offer failed due to unknown reason: " + result);
                }
            }
            else
            {
                System.out.println("yay!");
            }

            if (!publication.isConnected())
            {
                System.out.println("No active subscribers detected");
            }

            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        }

        System.out.println("Done sending.");

        if (LINGER_TIMEOUT_MS > 0)
        {
            System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
            Thread.sleep(LINGER_TIMEOUT_MS);
        }
    }

    CloseHelper.quietClose(driver);
}
 
Example 18
Source File: EngineConfiguration.java    From artio with Apache License 2.0 4 votes vote down vote up
public Aeron.Context aeronContextClone()
{
    return aeronContextClone;
}
 
Example 19
Source File: AeronNDArrayResponder.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Start a subscriber in another thread
 * based on the given parameters
 * @param context the context to use
 * @param host the host opName to bind to
 * @param port the port to bind to
 * @param callback the call back to use for the subscriber
 * @param streamId the stream id to subscribe to
 * @return the subscriber reference
 */
public static AeronNDArrayResponder startSubscriber(Aeron.Context context, String host, int port,
                NDArrayHolder callback, int streamId) {
    if (callback == null)
        throw new IllegalArgumentException("NDArrayHolder must be specified");


    final AtomicBoolean running = new AtomicBoolean(true);


    AeronNDArrayResponder subscriber = AeronNDArrayResponder.builder().streamId(streamId).ctx(context)
                    .channel(String.format("aeron:udp?endpoint=%s:%d", host, port)).running(running)
                    .ndArrayHolder(callback).build();


    Thread t = new Thread(() -> {
        try {
            subscriber.launch();
        } catch (Exception e) {
            log.error("",e);
        }

    });

    t.start();

    return subscriber;
}
 
Example 20
Source File: EngineScheduler.java    From artio with Apache License 2.0 2 votes vote down vote up
/**
 * Used to configure the aeron context object. This can be hooked in order to
 * switch the Aeron Client into Invoking mode, or inject a Media Driver
 *
 * @param aeronContext the context of the Aeron client being used by this Engine instance.
 */
void configure(Aeron.Context aeronContext);