io.aeron.Publication Java Examples

The following examples show how to use io.aeron.Publication. 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: ControlSession.java    From aeron with Apache License 2.0 6 votes vote down vote up
ControlSession(
    final long controlSessionId,
    final long correlationId,
    final long connectTimeoutMs,
    final String invalidVersionMessage,
    final ControlSessionDemuxer demuxer,
    final Publication controlPublication,
    final ArchiveConductor conductor,
    final CachedEpochClock cachedEpochClock,
    final ControlResponseProxy controlResponseProxy,
    final Authenticator authenticator,
    final ControlSessionProxy controlSessionProxy)
{
    this.controlSessionId = controlSessionId;
    this.correlationId = correlationId;
    this.connectTimeoutMs = connectTimeoutMs;
    this.invalidVersionMessage = invalidVersionMessage;
    this.demuxer = demuxer;
    this.controlPublication = controlPublication;
    this.conductor = conductor;
    this.cachedEpochClock = cachedEpochClock;
    this.controlResponseProxy = controlResponseProxy;
    this.authenticator = authenticator;
    this.controlSessionProxy = controlSessionProxy;
    this.activityDeadlineMs = cachedEpochClock.time() + connectTimeoutMs;
}
 
Example #2
Source File: ControlResponseProxy.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static void checkResult(final ControlSession session, final long result)
{
    if (result == Publication.NOT_CONNECTED)
    {
        session.abort();
        throw new ArchiveException(
            "response publication is not connected: " + session, AeronException.Category.WARN);
    }

    if (result == Publication.CLOSED)
    {
        session.abort();
        throw new ArchiveException("response publication is closed: " + session);
    }

    if (result == Publication.MAX_POSITION_EXCEEDED)
    {
        session.abort();
        throw new ArchiveException("response publication at max position: " + session);
    }
}
 
Example #3
Source File: Pong.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void pingHandler(
    final BufferClaim bufferClaim,
    final Publication pongPublication,
    final DirectBuffer buffer,
    final int offset,
    final int length,
    final Header header)
{
    PING_HANDLER_IDLE_STRATEGY.reset();
    while (pongPublication.tryClaim(length, bufferClaim) <= 0)
    {
        PING_HANDLER_IDLE_STRATEGY.idle();
    }

    bufferClaim
        .flags(header.flags())
        .putBytes(buffer, offset, length)
        .commit();
}
 
Example #4
Source File: ReplaySession.java    From aeron with Apache License 2.0 6 votes vote down vote up
private boolean hasPublicationAdvanced(final long position, final int alignedLength)
{
    if (position > 0)
    {
        termOffset += alignedLength;
        replayPosition += alignedLength;

        if (replayPosition >= replayLimit)
        {
            state(State.INACTIVE);
        }

        return true;
    }
    else if (Publication.CLOSED == position || Publication.NOT_CONNECTED == position)
    {
        onError("stream closed before replay is complete");
    }

    return false;
}
 
Example #5
Source File: FileSender.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    if (args.length != 1)
    {
        System.out.println("Filename to be sent must be supplied as a command line argument");
        System.exit(1);
    }

    try (Aeron aeron = Aeron.connect();
        Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        while (!publication.isConnected())
        {
            Thread.sleep(1);
        }

        final File file = new File(args[0]);
        final UnsafeBuffer buffer = new UnsafeBuffer(IoUtil.mapExistingFile(file, "sending"));
        final long correlationId = aeron.nextCorrelationId();

        sendFileCreate(publication, correlationId, buffer.capacity(), file.getName());
        streamChunks(publication, correlationId, buffer);
    }
}
 
Example #6
Source File: FileSender.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static void sendFileCreate(
    final Publication publication, final long correlationId, final int length, final String filename)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    buffer.putInt(VERSION_OFFSET, VERSION, LITTLE_ENDIAN);
    buffer.putInt(TYPE_OFFSET, FILE_CREATE_TYPE, LITTLE_ENDIAN);
    buffer.putLong(CORRELATION_ID_OFFSET, correlationId, LITTLE_ENDIAN);
    buffer.putLong(FILE_LENGTH_OFFSET, length, LITTLE_ENDIAN);

    final int msgLength = FILE_NAME_OFFSET + buffer.putStringUtf8(FILE_NAME_OFFSET, filename);

    long result;
    while ((result = publication.offer(buffer, 0, msgLength)) < 0)
    {
        checkResult(result);
        Thread.yield();
    }
}
 
Example #7
Source File: FileSender.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static void checkResult(final long result)
{
    if (result == Publication.CLOSED)
    {
        throw new IllegalStateException("Connection has been closed");
    }

    if (result == Publication.NOT_CONNECTED)
    {
        throw new IllegalStateException("Connection is no longer available");
    }

    if (result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new IllegalStateException("Publication failed due to max position being reached");
    }
}
 
Example #8
Source File: EmbeddedPingPong.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static Thread startPong(final Aeron aeron)
{
    return new Thread(() ->
    {
        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);

        try (Subscription pingSubscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID);
            Publication pongPublication = 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, pongPublication, buffer, offset, length, header);

            while (RUNNING.get())
            {
                PING_HANDLER_IDLE_STRATEGY.idle(pingSubscription.poll(fragmentHandler, FRAME_COUNT_LIMIT));
            }

            System.out.println("Shutting down...");
        }
    });
}
 
Example #9
Source File: ArchiveProxy.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Create a proxy with a {@link Publication} for sending control message requests.
 *
 * @param publication         publication for sending control messages to an archive.
 * @param retryIdleStrategy   for what should happen between retry attempts at offering messages.
 * @param nanoClock           to be used for calculating checking deadlines.
 * @param connectTimeoutNs    for for connection requests.
 * @param retryAttempts       for offering control messages before giving up.
 * @param credentialsSupplier for the AuthConnectRequest
 */
public ArchiveProxy(
    final Publication publication,
    final IdleStrategy retryIdleStrategy,
    final NanoClock nanoClock,
    final long connectTimeoutNs,
    final int retryAttempts,
    final CredentialsSupplier credentialsSupplier)
{
    this.publication = publication;
    this.retryIdleStrategy = retryIdleStrategy;
    this.nanoClock = nanoClock;
    this.connectTimeoutNs = connectTimeoutNs;
    this.retryAttempts = retryAttempts;
    this.credentialsSupplier = credentialsSupplier;
}
 
Example #10
Source File: EmbeddedPingPong.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void pingHandler(
    final BufferClaim bufferClaim,
    final Publication pongPublication,
    final DirectBuffer buffer,
    final int offset,
    final int length,
    final Header header)
{
    PING_HANDLER_IDLE_STRATEGY.reset();
    while (pongPublication.tryClaim(length, bufferClaim) <= 0)
    {
        PING_HANDLER_IDLE_STRATEGY.idle();
    }

    bufferClaim
        .flags(header.flags())
        .putBytes(buffer, offset, length)
        .commit();
}
 
Example #11
Source File: FileSender.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void sendChunk(
    final Publication publication,
    final BufferClaim bufferClaim,
    final long correlationId,
    final UnsafeBuffer fileBuffer,
    final int chunkOffset,
    final int chunkLength)
{
    long result;
    while ((result = publication.tryClaim(CHUNK_PAYLOAD_OFFSET + chunkLength, bufferClaim)) < 0)
    {
        checkResult(result);
        Thread.yield();
    }

    final MutableDirectBuffer buffer = bufferClaim.buffer();
    final int offset = bufferClaim.offset();

    buffer.putInt(offset + VERSION_OFFSET, VERSION, LITTLE_ENDIAN);
    buffer.putInt(offset + TYPE_OFFSET, FILE_CHUNK_TYPE, LITTLE_ENDIAN);
    buffer.putLong(offset + CORRELATION_ID_OFFSET, correlationId, LITTLE_ENDIAN);
    buffer.putLong(offset + CHUNK_OFFSET_OFFSET, chunkOffset, LITTLE_ENDIAN);
    buffer.putLong(offset + CHUNK_LENGTH_OFFSET, chunkLength, LITTLE_ENDIAN);
    buffer.putBytes(offset + CHUNK_PAYLOAD_OFFSET, fileBuffer, chunkOffset, chunkLength);

    bufferClaim.commit();
}
 
Example #12
Source File: FileSender.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void streamChunks(final Publication publication, final long correlationId, final UnsafeBuffer buffer)
{
    final BufferClaim bufferClaim = new BufferClaim();
    final int fileLength = buffer.capacity();
    final int maxChunkLength = publication.maxPayloadLength() - CHUNK_PAYLOAD_OFFSET;
    int chunkOffset = 0;

    while (chunkOffset < fileLength)
    {
        final int chunkLength = Math.min(maxChunkLength, fileLength - chunkOffset);
        sendChunk(publication, bufferClaim, correlationId, buffer, chunkOffset, chunkLength);
        chunkOffset += chunkLength;
    }
}
 
Example #13
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);
    }
}
 
Example #14
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 #15
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 #16
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 #17
Source File: ArchiveCreator.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void offerToPosition(final Publication publication, final long minimumPosition)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    for (int i = 0; publication.position() < minimumPosition; i++)
    {
        final int length = buffer.putStringWithoutLengthAscii(0, MESSAGE_PREFIX + i);

        while (publication.offer(buffer, 0, length) <= 0)
        {
            Thread.yield();
            checkInterruptStatus();
        }
    }
}
 
Example #18
Source File: ArchiveCreator.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void createRecording(
    final Aeron aeron, final AeronArchive aeronArchive, final long startPosition, final long targetPosition)
{
    final int initialTermId = 7;
    recordingNumber++;
    final ChannelUriStringBuilder uriBuilder = new ChannelUriStringBuilder()
        .media("udp")
        .endpoint("localhost:" + recordingNumber)
        .termLength(TERM_LENGTH);

    if (startPosition > 0)
    {
        uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH);
    }

    try (Publication publication = aeronArchive.addRecordedExclusivePublication(uriBuilder.build(), STREAM_ID))
    {
        final CountersReader counters = aeron.countersReader();
        final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
        final long recordingId = RecordingPos.getRecordingId(counters, counterId);

        System.out.println(
            "recordingId=" + recordingId +
            " position " + publication.position() +
            " to " + targetPosition);

        offerToPosition(publication, targetPosition);
        awaitPosition(counters, counterId, publication.position());

        aeronArchive.stopRecording(publication);
    }
}
 
Example #19
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void checkResult(final long result)
{
    if (result == Publication.CLOSED || result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new AeronException("unexpected publication state: " + result);
    }
}
 
Example #20
Source File: ServiceProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void checkResult(final long result)
{
    if (result == Publication.CLOSED || result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new AeronException("unexpected publication state: " + result);
    }
}
 
Example #21
Source File: ConsensusModuleProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void checkResult(final long result)
{
    if (result == Publication.NOT_CONNECTED ||
        result == Publication.CLOSED ||
        result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new AeronException("unexpected publication state: " + result);
    }
}
 
Example #22
Source File: SnapshotTaker.java    From aeron with Apache License 2.0 5 votes vote down vote up
protected static void checkResult(final long result)
{
    if (result == Publication.NOT_CONNECTED ||
        result == Publication.CLOSED ||
        result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new AeronException("unexpected publication state: " + result);
    }
}
 
Example #23
Source File: Common.java    From aeron with Apache License 2.0 5 votes vote down vote up
static void offerToPosition(final Publication publication, final String prefix, final long minimumPosition)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    for (int i = 0; publication.position() < minimumPosition; i++)
    {
        final int length = buffer.putStringWithoutLengthAscii(0, prefix + i);

        while (publication.offer(buffer, 0, length) <= 0)
        {
            Tests.yield();
        }
    }
}
 
Example #24
Source File: Common.java    From aeron with Apache License 2.0 5 votes vote down vote up
static void offer(final Publication publication, final int count, final String prefix)
{
    final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();

    for (int i = 0; i < count; i++)
    {
        final int length = buffer.putStringWithoutLengthAscii(0, prefix + i);

        while (publication.offer(buffer, 0, length) <= 0)
        {
            Tests.yield();
        }
    }
}
 
Example #25
Source File: ArchiveProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
private boolean offerWithTimeout(final int length, final AgentInvoker aeronClientInvoker)
{
    retryIdleStrategy.reset();

    final long deadlineNs = nanoClock.nanoTime() + connectTimeoutNs;
    while (true)
    {
        final long result = publication.offer(buffer, 0, MessageHeaderEncoder.ENCODED_LENGTH + length);
        if (result > 0)
        {
            return true;
        }

        if (result == Publication.CLOSED)
        {
            throw new ArchiveException("connection to the archive has been closed");
        }

        if (result == Publication.MAX_POSITION_EXCEEDED)
        {
            throw new ArchiveException("offer failed due to max position being reached");
        }

        if (deadlineNs - nanoClock.nanoTime() < 0)
        {
            return false;
        }

        if (null != aeronClientInvoker)
        {
            aeronClientInvoker.invoke();
        }

        retryIdleStrategy.idle();
    }
}
 
Example #26
Source File: RecordingEventsProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
void started(
    final long recordingId,
    final long startPosition,
    final int sessionId,
    final int streamId,
    final String channel,
    final String sourceIdentity)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + RecordingStartedEncoder.BLOCK_LENGTH +
        RecordingStartedEncoder.channelHeaderLength() + channel.length() +
        RecordingStartedEncoder.sourceIdentityHeaderLength() + sourceIdentity.length();

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            recordingStartedEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .recordingId(recordingId)
                .startPosition(startPosition)
                .sessionId(sessionId)
                .streamId(streamId)
                .channel(channel)
                .sourceIdentity(sourceIdentity);

            bufferClaim.commit();
            break;
        }
        else if (Publication.NOT_CONNECTED == result)
        {
            break;
        }

        checkResult(result);
    }
    while (--attempts > 0);
}
 
Example #27
Source File: RecordingEventsProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
void stopped(final long recordingId, final long startPosition, final long stopPosition)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + RecordingStoppedEncoder.BLOCK_LENGTH;
    int attempts = SEND_ATTEMPTS;

    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            recordingStoppedEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .recordingId(recordingId)
                .startPosition(startPosition)
                .stopPosition(stopPosition);

            bufferClaim.commit();
            break;
        }
        else if (Publication.NOT_CONNECTED == result)
        {
            break;
        }

        checkResult(result);
    }
    while (--attempts > 0);
}
 
Example #28
Source File: RecordingEventsProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void checkResult(final long result)
{
    if (result == Publication.CLOSED)
    {
        throw new ArchiveException("recording events publication is closed");
    }

    if (result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new ArchiveException("recording events publication at max position");
    }
}
 
Example #29
Source File: ArchiveProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
private boolean offer(final int length)
{
    retryIdleStrategy.reset();

    int attempts = retryAttempts;
    while (true)
    {
        final long result = publication.offer(buffer, 0, MessageHeaderEncoder.ENCODED_LENGTH + length);
        if (result > 0)
        {
            return true;
        }

        if (result == Publication.CLOSED)
        {
            throw new ArchiveException("connection to the archive has been closed");
        }

        if (result == Publication.NOT_CONNECTED)
        {
            throw new ArchiveException("connection to the archive is no longer available");
        }

        if (result == Publication.MAX_POSITION_EXCEEDED)
        {
            throw new ArchiveException("offer failed due to max position being reached");
        }

        if (--attempts <= 0)
        {
            return false;
        }

        retryIdleStrategy.idle();
    }
}
 
Example #30
Source File: ControlResponseProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
void attemptSendSignal(
    final long controlSessionId,
    final long correlationId,
    final long recordingId,
    final long subscriptionId,
    final long position,
    final RecordingSignal recordingSignal,
    final Publication controlPublication)
{
    final int messageLength = MESSAGE_HEADER_LENGTH + RecordingSignalEventEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = controlPublication.tryClaim(messageLength, bufferClaim);
        if (result > 0)
        {
            final MutableDirectBuffer buffer = bufferClaim.buffer();
            final int bufferOffset = bufferClaim.offset();

            recordingSignalEventEncoder
                .wrapAndApplyHeader(buffer, bufferOffset, messageHeaderEncoder)
                .controlSessionId(controlSessionId)
                .correlationId(correlationId)
                .recordingId(recordingId)
                .subscriptionId(subscriptionId)
                .position(position)
                .signal(recordingSignal);

            bufferClaim.commit();
            break;
        }
    }
    while (--attempts > 0);
}