use of org.apache.pulsar.broker.service.Subscription in project incubator-pulsar by apache.
the class PersistentTopic method getNonDurableSubscription.
private CompletableFuture<? extends Subscription> getNonDurableSubscription(String subscriptionName, MessageId startMessageId) {
CompletableFuture<Subscription> subscriptionFuture = new CompletableFuture<>();
log.info("[{}][{}] Creating non-durable subscription at msg id {}", topic, subscriptionName, startMessageId);
// Create a new non-durable cursor only for the first consumer that connects
Subscription subscription = subscriptions.computeIfAbsent(subscriptionName, name -> {
MessageIdImpl msgId = startMessageId != null ? (MessageIdImpl) startMessageId : (MessageIdImpl) MessageId.latest;
long ledgerId = msgId.getLedgerId();
long entryId = msgId.getEntryId();
if (msgId instanceof BatchMessageIdImpl) {
// The client will then be able to discard the first messages in the batch.
if (((BatchMessageIdImpl) msgId).getBatchIndex() >= 0) {
entryId = msgId.getEntryId() - 1;
}
}
Position startPosition = new PositionImpl(ledgerId, entryId);
ManagedCursor cursor = null;
try {
cursor = ledger.newNonDurableCursor(startPosition);
} catch (ManagedLedgerException e) {
subscriptionFuture.completeExceptionally(e);
}
return new PersistentSubscription(this, subscriptionName, cursor);
});
if (!subscriptionFuture.isDone()) {
subscriptionFuture.complete(subscription);
} else {
// failed to initialize managed-cursor: clean up created subscription
subscriptions.remove(subscriptionName);
}
return subscriptionFuture;
}
Aggregations