Karlson
Karlson

Reputation: 3048

Is it possible for Multiple Consumers to consume same message on a Pulsar Topic?

Let's say for that one has a Pulsar Producer for a persistent topic topic1 (namespace and tenant are not relevant for the question).

And let's say we have multiple consumers for the same topic (topic1) with different subscription names.

Is it possible to configure the consumers to receive same message? So for example if message msg1 is sent to the topic both consumer1 and consumer2 receive this message?

Both consumers and producer are written in Java but programming language is not important.

Clarification

The current behavior observed with multiple subscriptions on the same topic with multiple subscriptions is that each of the subscribers do not receive all messages that have been published to the topic. I need to receive all messages from the topic.

Upvotes: 3

Views: 3854

Answers (2)

Chris Bartholomew
Chris Bartholomew

Reputation: 1120

Yes, you just need to use multiple subscriptions on the topic.

I wrote a blog post on this exact topic: "Subscriptions: Multiple Groups of Consumers on a Pulsar Topic".

Upvotes: 3

Ming L.
Ming L.

Reputation: 401

Yes. It is possible for multiple consumers to receive the same copy of messages from a topic. Subscription determines how messages are delivered to consumers. What you need is each consumer has own Exclusive subscription. Here is the code example in Java.

String topicName = "your-tenant/namespace/topic1"
Consumer consumer1 = client.newConsumer()
            .topic(topicName)
            .subscriptionName("my-subscription1")
            .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
            .subscriptionType(SubscriptionType.Exclusive)
            .subscribe();

// create another consumer
Consumer consumer2 = client.newConsumer()
            .topic(topicName)
            .subscriptionName("my-subscription2")
            .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)
            .subscriptionType(SubscriptionType.Exclusive)
            .subscribe();

// two consumers receive messages in alternate from the same topic
while (true) {
        Message msgFromConsumer1 = consumer1.receive();

        Message msgFromConsumer2 = consumer2.receive();
}

Upvotes: 4

Related Questions