Java Code Examples for org.apache.pulsar.client.api.SubscriptionType#Shared

The following examples show how to use org.apache.pulsar.client.api.SubscriptionType#Shared . 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: MultiTopicsConsumerImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void redeliverUnacknowledgedMessages(Set<MessageId> messageIds) {
    if (messageIds.isEmpty()) {
        return;
    }

    checkArgument(messageIds.stream().findFirst().get() instanceof TopicMessageIdImpl);

    if (conf.getSubscriptionType() != SubscriptionType.Shared) {
        // We cannot redeliver single messages if subscription type is not Shared
        redeliverUnacknowledgedMessages();
        return;
    }
    removeExpiredMessagesFromQueue(messageIds);
    messageIds.stream().map(messageId -> (TopicMessageIdImpl)messageId)
        .collect(Collectors.groupingBy(TopicMessageIdImpl::getTopicPartitionName, Collectors.toSet()))
        .forEach((topicName, messageIds1) ->
            consumers.get(topicName)
                .redeliverUnacknowledgedMessages(messageIds1.stream()
                    .map(mid -> mid.getInnerMessageId()).collect(Collectors.toSet())));
    resumeReceivingFromPausedConsumersIfNeeded();
}
 
Example 2
Source File: BrokerClientIntegrationTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@DataProvider
public Object[][] subType() {
    return new Object[][] { { SubscriptionType.Shared }, { SubscriptionType.Failover } };
}
 
Example 3
Source File: NegativeAcksTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@DataProvider(name = "variations")
public static Object[][] variations() {
    return new Object[][] {
            // batching / partitions / subscription-type / redelivery-delay-ms / ack-timeout
            { false, false, SubscriptionType.Shared, 100, 0 },
            { false, false, SubscriptionType.Failover, 100, 0 },
            { false, true, SubscriptionType.Shared, 100, 0 },
            { false, true, SubscriptionType.Failover, 100, 0 },
            { true, false, SubscriptionType.Shared, 100, 0 },
            { true, false, SubscriptionType.Failover, 100, 0 },
            { true, true, SubscriptionType.Shared, 100, 0 },
            { true, true, SubscriptionType.Failover, 100, 0 },

            { false, false, SubscriptionType.Shared, 0, 0 },
            { false, false, SubscriptionType.Failover, 0, 0 },
            { false, true, SubscriptionType.Shared, 0, 0 },
            { false, true, SubscriptionType.Failover, 0, 0 },
            { true, false, SubscriptionType.Shared, 0, 0 },
            { true, false, SubscriptionType.Failover, 0, 0 },
            { true, true, SubscriptionType.Shared, 0, 0 },
            { true, true, SubscriptionType.Failover, 0, 0 },

            { false, false, SubscriptionType.Shared, 100, 1000 },
            { false, false, SubscriptionType.Failover, 100, 1000 },
            { false, true, SubscriptionType.Shared, 100, 1000 },
            { false, true, SubscriptionType.Failover, 100, 1000 },
            { true, false, SubscriptionType.Shared, 100, 1000 },
            { true, false, SubscriptionType.Failover, 100, 1000 },
            { true, true, SubscriptionType.Shared, 100, 1000 },
            { true, true, SubscriptionType.Failover, 100, 1000 },

            { false, false, SubscriptionType.Shared, 0, 1000 },
            { false, false, SubscriptionType.Failover, 0, 1000 },
            { false, true, SubscriptionType.Shared, 0, 1000 },
            { false, true, SubscriptionType.Failover, 0, 1000 },
            { true, false, SubscriptionType.Shared, 0, 1000 },
            { true, false, SubscriptionType.Failover, 0, 1000 },
            { true, true, SubscriptionType.Shared, 0, 1000 },
            { true, true, SubscriptionType.Failover, 0, 1000 },
    };
}
 
Example 4
Source File: ConsumerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
public void redeliverUnacknowledgedMessages(Set<MessageId> messageIds) {
    if (messageIds.isEmpty()) {
        return;
    }

    checkArgument(messageIds.stream().findFirst().get() instanceof MessageIdImpl);

    if (conf.getSubscriptionType() != SubscriptionType.Shared
            && conf.getSubscriptionType() != SubscriptionType.Key_Shared) {
        // We cannot redeliver single messages if subscription type is not Shared
        redeliverUnacknowledgedMessages();
        return;
    }
    ClientCnx cnx = cnx();
    if (isConnected() && cnx.getRemoteEndpointProtocolVersion() >= ProtocolVersion.v2.getNumber()) {
        int messagesFromQueue = removeExpiredMessagesFromQueue(messageIds);
        Iterable<List<MessageIdImpl>> batches = Iterables.partition(
            messageIds.stream()
                .map(messageId -> (MessageIdImpl)messageId)
                .collect(Collectors.toSet()), MAX_REDELIVER_UNACKNOWLEDGED);
        MessageIdData.Builder builder = MessageIdData.newBuilder();
        batches.forEach(ids -> {
            List<MessageIdData> messageIdDatas = ids.stream()
                .filter(messageId -> !processPossibleToDLQ(messageId))
                .map(messageId -> {
                        builder.setPartition(messageId.getPartitionIndex());
                        builder.setLedgerId(messageId.getLedgerId());
                        builder.setEntryId(messageId.getEntryId());
                        return builder.build();
                    }).collect(Collectors.toList());
            if (!messageIdDatas.isEmpty()) {
                ByteBuf cmd = Commands.newRedeliverUnacknowledgedMessages(consumerId, messageIdDatas);
                cnx.ctx().writeAndFlush(cmd, cnx.ctx().voidPromise());
                messageIdDatas.forEach(MessageIdData::recycle);
            }
        });
        if (messagesFromQueue > 0) {
            increaseAvailablePermits(cnx, messagesFromQueue);
        }
        builder.recycle();
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] [{}] Redeliver unacked messages and increase {} permits", subscription, topic,
                    consumerName, messagesFromQueue);
        }
        return;
    }
    if (cnx == null || (getState() == State.Connecting)) {
        log.warn("[{}] Client Connection needs to be established for redelivery of unacknowledged messages", this);
    } else {
        log.warn("[{}] Reconnecting the client to redeliver the messages.", this);
        cnx.ctx().close();
    }
}
 
Example 5
Source File: ConsumerBase.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private boolean isCumulativeAcknowledgementAllowed(SubscriptionType type) {
    return SubscriptionType.Shared != type && SubscriptionType.Key_Shared != type;
}