Java Code Examples for io.aeron.Image#poll()

The following examples show how to use io.aeron.Image#poll() . 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: SingleNodeCluster.java    From aeron with Apache License 2.0 6 votes vote down vote up
public void onStart(final Cluster cluster, final Image snapshotImage)
{
    this.cluster = cluster;
    this.idleStrategy = cluster.idleStrategy();

    if (null != snapshotImage)
    {
        System.out.println("onStart load snapshot");
        final FragmentHandler fragmentHandler =
            (buffer, offset, length, header) -> messageCount = buffer.getInt(offset);

        while (snapshotImage.poll(fragmentHandler, 1) <= 0)
        {
            idleStrategy.idle();
        }

        System.out.println("snapshot messageCount=" + messageCount);
    }
}
 
Example 2
Source File: SequenceNumberIndexTest.java    From artio with Apache License 2.0 5 votes vote down vote up
private long indexRecord()
{
    long position = 0;
    while (position < 1)
    {
        position = publication.offer(buffer, START, fragmentLength());

        Thread.yield();
    }

    /*System.out.println("position = " + position);
    System.out.println("p = " + p);*/


    Image image = null;
    while (image == null || image.position() < position)
    {
        if (image == null)
        {
            image = subscription.imageBySessionId(publication.sessionId());
        }

        if (image != null)
        {
            image.poll(writer, 1);
        }
    }

    return position;
}
 
Example 3
Source File: AeronUtil.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
static void pipeMessages(
    final Subscription subscription, final ExclusivePublication publication, final AtomicBoolean running)
{
    final IdleStrategy idleStrategy = idleStrategy();
    final BufferClaim bufferClaim = new BufferClaim();
    final FragmentHandler dataHandler =
        (buffer, offset, length, header) ->
        {
            long result;
            while ((result = publication.tryClaim(length, bufferClaim)) <= 0)
            {
                checkPublicationResult(result);
            }

            bufferClaim
                .flags(header.flags())
                .putBytes(buffer, offset, length)
                .commit();
        };

    final Image image = subscription.imageAtIndex(0);
    final int fragmentLimit = fragmentLimit();

    while (true)
    {
        final int fragmentsRead = image.poll(dataHandler, fragmentLimit);
        if (0 == fragmentsRead)
        {
            if (image.isClosed() || !running.get())
            {
                break;
            }
        }

        idleStrategy.idle(fragmentsRead);
    }
}
 
Example 4
Source File: TestNode.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void onStart(final Cluster cluster, final Image snapshotImage)
{
    super.onStart(cluster, snapshotImage);

    if (null != snapshotImage)
    {
        activeSessionCount = cluster.clientSessions().size();

        final FragmentHandler handler =
            (buffer, offset, length, header) -> messageCount = buffer.getInt(offset);

        int fragmentCount = 0;
        while (true)
        {
            final int fragments = snapshotImage.poll(handler, 10);
            fragmentCount += fragments;

            if (snapshotImage.isClosed() || snapshotImage.isEndOfStream())
            {
                break;
            }

            idleStrategy.idle(fragments);
        }

        if (fragmentCount != SNAPSHOT_FRAGMENT_COUNT)
        {
            throw new AgentTerminationException(
                "unexpected snapshot length: expected=" + SNAPSHOT_FRAGMENT_COUNT + " actual=" + fragmentCount);
        }

        wasSnapshotLoaded = true;
    }
}
 
Example 5
Source File: EmbeddedPingPong.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void roundTripMessages(
    final FragmentHandler fragmentHandler,
    final Publication pingPublication,
    final Subscription pongSubscription,
    final long numMessages)
{
    while (!pongSubscription.isConnected())
    {
        Thread.yield();
    }

    final Image image = pongSubscription.imageAtIndex(0);

    for (long i = 0; i < numMessages; i++)
    {
        long offeredPosition;

        do
        {
            OFFER_BUFFER.putLong(0, System.nanoTime());
        }
        while ((offeredPosition = pingPublication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null)) < 0L);

        PONG_HANDLER_IDLE_STRATEGY.reset();
        do
        {
            while (image.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT) <= 0)
            {
                PONG_HANDLER_IDLE_STRATEGY.idle();
            }
        }
        while (image.position() < offeredPosition);
    }
}