Java Code Examples for org.apache.pulsar.client.api.MessageId#latest()

The following examples show how to use org.apache.pulsar.client.api.MessageId#latest() . 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: PulsarKafkaSimpleConsumer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private MessageId getMessageId(long offset) {
    if (kafka.api.OffsetRequest.EarliestTime() == offset) {
        return MessageId.earliest;
    } else if (kafka.api.OffsetRequest.LatestTime() == offset) {
        return MessageId.latest;
    } else {
        return MessageIdUtils.getMessageId(offset);
    }
}
 
Example 2
Source File: ReaderHandler.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private MessageId getMessageId() throws IOException {
    MessageId messageId = MessageId.latest;
    if (isNotBlank(queryParams.get("messageId"))) {
        if (queryParams.get("messageId").equals("earliest")) {
            messageId = MessageId.earliest;
        } else if (!queryParams.get("messageId").equals("latest")) {
            messageId = MessageIdImpl.fromByteArray(Base64.getDecoder().decode(queryParams.get("messageId")));
        }
    }
    return messageId;
}
 
Example 3
Source File: CmdTopics.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String topic = validateTopicName(params);
    MessageId messageId;
    if (messageIdStr.equals("latest")) {
        messageId = MessageId.latest;
    } else if (messageIdStr.equals("earliest")) {
        messageId = MessageId.earliest;
    } else {
        messageId = validateMessageIdString(messageIdStr);
    }

    topics.createSubscription(topic, subscriptionName, messageId);
}
 
Example 4
Source File: CmdPersistentTopics.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String persistentTopic = validatePersistentTopic(params);
    MessageId messageId;
    if (messageIdStr.equals("latest")) {
        messageId = MessageId.latest;
    } else if (messageIdStr.equals("earliest")) {
        messageId = MessageId.earliest;
    } else {
        messageId = validateMessageIdString(messageIdStr);
    }

    persistentTopics.createSubscription(persistentTopic, subscriptionName, messageId);
}
 
Example 5
Source File: MockTypedMessageBuilder.java    From singer with Apache License 2.0 4 votes vote down vote up
@Override
public MessageId send() throws PulsarClientException {
  return MessageId.latest;
}
 
Example 6
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private CompletableFuture<? extends Subscription> getNonDurableSubscription(String subscriptionName,
        MessageId startMessageId, long startMessageRollbackDurationSec) {
    log.info("[{}][{}] Creating non-durable subscription at msg id {}", topic, subscriptionName, startMessageId);

    synchronized (ledger) {
        // Create a new non-durable cursor only for the first consumer that connects
        PersistentSubscription subscription = subscriptions.get(subscriptionName);

        if (subscription == null) {
            MessageIdImpl msgId = startMessageId != null ? (MessageIdImpl) startMessageId
                    : (MessageIdImpl) MessageId.latest;

            long ledgerId = msgId.getLedgerId();
            long entryId = msgId.getEntryId();
            // Ensure that the start message id starts from a valid entry.
            if (ledgerId >= 0 && entryId >= 0
                    && msgId instanceof BatchMessageIdImpl) {
                // When the start message is relative to a batch, we need to take one step back on the previous
                // message,
                // because the "batch" might not have been consumed in its entirety.
                // The client will then be able to discard the first messages if needed.
                entryId = msgId.getEntryId() - 1;
            }

            Position startPosition = new PositionImpl(ledgerId, entryId);
            ManagedCursor cursor = null;
            try {
                cursor = ledger.newNonDurableCursor(startPosition, subscriptionName);
            } catch (ManagedLedgerException e) {
                return FutureUtil.failedFuture(e);
            }

            subscription = new PersistentSubscription(this, subscriptionName, cursor, false);
            subscriptions.put(subscriptionName, subscription);
        }

        if (startMessageRollbackDurationSec > 0) {
            long timestamp = System.currentTimeMillis()
                    - TimeUnit.SECONDS.toMillis(startMessageRollbackDurationSec);
            CompletableFuture<Subscription> subscriptionFuture = new CompletableFuture<>();
            final Subscription finalSubscription = subscription;
            subscription.resetCursor(timestamp).handle((s, ex) -> {
                if (ex != null) {
                    log.warn("[{}] Failed to reset cursor {} position at timestamp {}", topic, subscriptionName,
                            startMessageRollbackDurationSec);
                }
                subscriptionFuture.complete(finalSubscription);
                return null;
            });
            return subscriptionFuture;
        } else {
            return CompletableFuture.completedFuture(subscription);
        }
    }
}
 
Example 7
Source File: AdminApiOffloadTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void testOffload(String topicName, String mlName) throws Exception {
    LedgerOffloader offloader = mock(LedgerOffloader.class);
    when(offloader.getOffloadDriverName()).thenReturn("mock");

    doReturn(offloader).when(pulsar).getManagedLedgerOffloader(any(), any());

    CompletableFuture<Void> promise = new CompletableFuture<>();
    doReturn(promise).when(offloader).offload(any(), any(), any());

    MessageId currentId = MessageId.latest;
    try (Producer<byte[]> p = pulsarClient.newProducer().topic(topicName).enableBatching(false).create()) {
        for (int i = 0; i < 15; i++) {
            currentId = p.send("Foobar".getBytes());
        }
    }

    ManagedLedgerInfo info = pulsar.getManagedLedgerFactory().getManagedLedgerInfo(mlName);
    Assert.assertEquals(info.ledgers.size(), 2);

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.NOT_RUN);

    admin.topics().triggerOffload(topicName, currentId);

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.RUNNING);

    try {
        admin.topics().triggerOffload(topicName, currentId);
        Assert.fail("Should have failed");
    } catch (ConflictException e) {
        // expected
    }

    // fail first time
    promise.completeExceptionally(new Exception("Some random failure"));

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.ERROR);
    Assert.assertTrue(admin.topics().offloadStatus(topicName).lastError.contains("Some random failure"));

    // Try again
    doReturn(CompletableFuture.completedFuture(null))
        .when(offloader).offload(any(), any(), any());

    admin.topics().triggerOffload(topicName, currentId);

    Assert.assertEquals(admin.topics().offloadStatus(topicName).status,
                        LongRunningProcessStatus.Status.SUCCESS);
    MessageIdImpl firstUnoffloaded = admin.topics().offloadStatus(topicName).firstUnoffloadedMessage;
    // First unoffloaded is the first entry of current ledger
    Assert.assertEquals(firstUnoffloaded.getLedgerId(), info.ledgers.get(1).ledgerId);
    Assert.assertEquals(firstUnoffloaded.getEntryId(), 0);

    verify(offloader, times(2)).offload(any(), any(), any());
}