Search in sources :

Example 6 with BackoffIdleStrategy

use of org.agrona.concurrent.BackoffIdleStrategy in project Aeron by real-logic.

the class SimpleSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
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.printf("Received message (%s) to stream %d from session %x term id %x term offset %d (%d@%d)%n", 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();
    // 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...");
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Aeron(io.aeron.Aeron) Subscription(io.aeron.Subscription) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) SigInt(org.agrona.concurrent.SigInt) IdleStrategy(org.agrona.concurrent.IdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) FragmentHandler(io.aeron.logbuffer.FragmentHandler) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Example 7 with BackoffIdleStrategy

use of org.agrona.concurrent.BackoffIdleStrategy in project aeron by real-logic.

the class SimpleSubscriber method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
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.printf("Received message (%s) to stream %d from session %x term id %x term offset %d (%d@%d)%n", 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();
    // 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...");
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Aeron(io.aeron.Aeron) Subscription(io.aeron.Subscription) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) SigInt(org.agrona.concurrent.SigInt) IdleStrategy(org.agrona.concurrent.IdleStrategy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) FragmentHandler(io.aeron.logbuffer.FragmentHandler) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron)

Aggregations

BackoffIdleStrategy (org.agrona.concurrent.BackoffIdleStrategy)7 IdleStrategy (org.agrona.concurrent.IdleStrategy)7 Subscription (io.aeron.Subscription)5 Aeron (io.aeron.Aeron)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 FragmentAssembler (io.aeron.FragmentAssembler)2 ConfigurationException (io.aeron.driver.exceptions.ConfigurationException)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)2 TimeUnit (java.util.concurrent.TimeUnit)2 ControllableIdleStrategy (org.agrona.concurrent.ControllableIdleStrategy)2 SigInt (org.agrona.concurrent.SigInt)2 RecordingEventsAdapter (io.aeron.archive.client.RecordingEventsAdapter)1 IOException (java.io.IOException)1