Java Code Examples for io.aeron.Publication#BACK_PRESSURED

The following examples show how to use io.aeron.Publication#BACK_PRESSURED . 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: RecordedBasicPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void checkResult(final long result)
{
    if (result > 0)
    {
        System.out.println("yay!");
    }
    else if (result == Publication.BACK_PRESSURED)
    {
        System.out.println("Offer failed due to back pressure");
    }
    else if (result == Publication.ADMIN_ACTION)
    {
        System.out.println("Offer failed because of an administration action in the system");
    }
    else if (result == Publication.NOT_CONNECTED)
    {
        System.out.println("Offer failed because publisher is not connected to subscriber");
    }
    else if (result == Publication.CLOSED)
    {
        System.out.println("Offer failed publication is closed");
    }
    else if (result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new IllegalStateException("Offer failed due to publication reaching max position");
    }
    else
    {
        System.out.println("Offer failed due to unknown result code: " + result);
    }
}
 
Example 2
Source File: AeronNDArrayPublisher.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private void sendBuffer(DirectBuffer buffer) throws Exception {
    // Try to publish the buffer. 'offer' is a non-blocking call.
    // If it returns less than 0, the message was not sent, and the offer should be retried.
    long result;
    int tries = 0;
    while ((result = publication.offer(buffer, 0, buffer.capacity())) < 0L && tries < 5) {
        if (result == Publication.BACK_PRESSURED) {
            log.info("Offer failed due to back pressure");
        } else if (result == Publication.NOT_CONNECTED) {
            log.info("Offer failed because publisher is not connected to subscriber " + channel + " and stream "
                            + streamId);
        } else if (result == Publication.ADMIN_ACTION) {
            log.info("Offer failed because of an administration action in the system and channel" + channel
                            + " and stream " + streamId);
        } else if (result == Publication.CLOSED) {
            log.info("Offer failed publication is closed and channel" + channel + " and stream " + streamId);
        } else {
            log.info(" Offer failed due to unknown reason and channel" + channel + " and stream " + streamId);
        }



        Thread.sleep(publishRetryTimeOut);
        tries++;

    }

    if (tries >= 5 && result == 0)
        throw new IllegalStateException("Failed to send message");

}
 
Example 3
Source File: AeronNDArrayPublisher.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void sendBuffer(DirectBuffer buffer) throws Exception {
    // Try to publish the buffer. 'offer' is a non-blocking call.
    // If it returns less than 0, the message was not sent, and the offer should be retried.
    long result;
    int tries = 0;
    while ((result = publication.offer(buffer, 0, buffer.capacity())) < 0L && tries < 5) {
        if (result == Publication.BACK_PRESSURED) {
            log.info("Offer failed due to back pressure");
        } else if (result == Publication.NOT_CONNECTED) {
            log.info("Offer failed because publisher is not connected to subscriber " + channel + " and stream "
                            + streamId);
        } else if (result == Publication.ADMIN_ACTION) {
            log.info("Offer failed because of an administration action in the system and channel" + channel
                            + " and stream " + streamId);
        } else if (result == Publication.CLOSED) {
            log.info("Offer failed publication is closed and channel" + channel + " and stream " + streamId);
        } else {
            log.info(" Offer failed due to unknown reason and channel" + channel + " and stream " + streamId);
        }



        Thread.sleep(publishRetryTimeOut);
        tries++;

    }

    if (tries >= 5 && result == 0)
        throw new IllegalStateException("Failed to send message");

}
 
Example 4
Source File: MultiplePublishersWithFragmentation.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args)
{
    System.out.println(
        "Publishing to " + CHANNEL + " on stream id " + STREAM_ID_1 + " and stream id " + STREAM_ID_2);

    try (Aeron aeron = Aeron.connect();
        Publication publication1 = aeron.addPublication(CHANNEL, STREAM_ID_1);
        Publication publication2 = aeron.addPublication(CHANNEL, STREAM_ID_2))
    {
        int j = 1;
        int k = 1;
        final String message1 = "Hello World! " + j;
        BUFFER_1.putBytes(0, message1.getBytes());
        final String message2 = "Hello World! " + k;
        BUFFER_2.putBytes(0, message2.getBytes());

        while (j <= 5000 || k <= 5000)
        {
            boolean offerStatus1 = false;
            boolean offerStatus2 = false;
            long result1;
            long result2;

            while (!(offerStatus1 || offerStatus2))
            {
                if (j <= 5000)
                {
                    result1 = publication1.offer(BUFFER_1, 0, BUFFER_1.capacity());
                    if (result1 < 0L)
                    {
                        if (result1 == Publication.BACK_PRESSURED)
                        {
                            System.out.println(" Offer failed due to back pressure for stream id " + STREAM_ID_1);
                        }
                        else if (result1 == Publication.NOT_CONNECTED)
                        {
                            System.out.println(" Offer failed because publisher is not yet " +
                                "connected to subscriber for stream id " + STREAM_ID_1);
                        }
                        else
                        {
                            System.out.println(" Offer failed due to unknown reason");
                        }

                        offerStatus1 = false;
                    }
                    else
                    {
                        j++;
                        offerStatus1 = true;
                        System.out.println("Successfully sent data on stream id " +
                            STREAM_ID_1 + " and data length " + BUFFER_1.capacity() + " at offset " + result1);
                    }
                }

                if (k <= 5000)
                {
                    result2 = publication2.offer(BUFFER_2, 0, BUFFER_2.capacity());
                    if (result2 < 0L)
                    {
                        if (result2 == Publication.BACK_PRESSURED)
                        {
                            System.out.println(" Offer failed because publisher is not yet " +
                                "connected to subscriber for stream id " + STREAM_ID_2);
                        }
                        else if (result2 == Publication.NOT_CONNECTED)
                        {
                            System.out.println(
                                "Offer failed - publisher is not yet connected to subscriber" + STREAM_ID_2);
                        }
                        else
                        {
                            System.out.println("Offer failed due to unknown reason");
                        }
                        offerStatus2 = false;
                    }
                    else
                    {
                        k++;
                        offerStatus2 = true;
                        System.out.println("Successfully sent data on stream id " + STREAM_ID_2 +
                            " and data length " + BUFFER_2.capacity() + " at offset " + result2);
                    }
                }
            }
        }

        System.out.println("Done sending total messages for stream id " +
            STREAM_ID_1 + " = " + (j - 1) + " and stream id " + STREAM_ID_2 + " = " + (k - 1));
    }
}
 
Example 5
Source File: SimplePublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    // Allocate enough buffer size to hold maximum message length
    // The UnsafeBuffer class is part of the Agrona library and is used for efficient buffer management
    final UnsafeBuffer buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(512, BitUtil.CACHE_LINE_LENGTH));

    // The channel (an endpoint identifier) to send the message to
    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("Publishing to " + channel + " on stream id " + streamId);

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

    // Create an Aeron instance with client-provided context configuration and connect to the
    // media driver, and create a Publication.  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, streamId))
    {
        final String message = "Hello World! ";
        final byte[] messageBytes = message.getBytes();
        buffer.putBytes(0, messageBytes);

        // Wait for 5 seconds to connect to a subscriber
        final long deadlineNs = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
        while (!publication.isConnected())
        {
            if ((deadlineNs - System.nanoTime()) < 0)
            {
                System.out.println("Failed to connect to subscriber");
                return;
            }

            Thread.sleep(1);
        }

        // Try to publish the buffer. 'offer' is a non-blocking call.
        // If it returns less than 0, the message was not sent, and the offer should be retried.
        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");
            }
            else if (result == Publication.MAX_POSITION_EXCEEDED)
            {
                System.out.println("Offer failed due to publication reaching max position");
            }
            else
            {
                System.out.println(" Offer failed due to unknown reason");
            }
        }
        else
        {
            System.out.println(" yay !!");
        }

        System.out.println("Done sending.");
    }
}
 
Example 6
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);
}