Hi Everyone Could anyone help on this. We need to ...
# general
d
Hi Everyone Could anyone help on this. We need to create a consumer with multiple tenant topics with a single subscription and dynamically need to add topics to this consumer .
Copy code
String subscriptionName = "postload-task-subscription";
        String tenant1Topic = "pulsartenant_1/default/postload-task";
        String tenant2Topic = "pulsartenant_2/default/postload-task";
        String tenant3Topic = "pulsartenant_3/default/postload-task";

SubscriptionType subscriptionType = SubscriptionType.Shared;
        SubscriptionInitialPosition subscriptionMode = SubscriptionInitialPosition.Earliest;

allTopicsConsumer = (MultiTopicsConsumerImpl<byte[]>)pulsarClient.newConsumer()
        .subscriptionName(subscriptionName)
        .topic(tenant1Topic, tenant2Topic)
        .subscriptionType(subscriptionType)
        .subscriptionInitialPosition(subscriptionMode)
        .subscribe();
int count=0;
while (count==20) {
Message<byte[]> message = allTopicsConsumer.receive();
                System.out.println(new String(message.getData()));
                Thread.sleep(1000);
                    allTopicsConsumer.acknowledge(message);
                 count++;
                 allTopicsConsumer.close();

}
allTopicsConsumer = (MultiTopicsConsumerImpl<byte[]>)pulsarClient.newConsumer()
                    .subscriptionName(subscriptionName)
                    .topic(tenant1Topic, tenant2Topic, tenant3Topic)
                    .subscriptionType(subscriptionType)
                    .subscriptionInitialPosition(subscriptionMode)
                    .subscribe();

while (true) {
                Thread.sleep(1000);
                Message<byte[]> message = allTopicsConsumer.receive();
                System.out.println(new String(message.getData()));
                allTopicsConsumer.acknowledge(message);

}
If we are closing consumer after doing ack, sometime Its getting redeliver again. Wanted to close consumer gracefully and reassign with the new consumer instances . Any possible way to achieve this?