Make sure messages goes to a specific consumer

238 views Asked by At

I have multiple consumers subscribed to the same Pulsar topic. How do I make sure certain messages go to specific consumers?

The closest thing I understand is the key-shared consumer type. However, they seem to group the messages by a hash-range and then choose a consumer at random to send the messages to.

1

There are 1 answers

0
David Kjerrumgaard On

If you use the Key-Shared subscription type, you can specify which hash range a particular consumer will be assigned with the KeySharedPolicySticky policy.

Consumer<String> consumer = getClient().newConsumer(Schema.STRING)
            .topic("topicName")
            .subscriptionName("sub1")
            .subscriptionType(SubscriptionType.Key_Shared)
            .keySharedPolicy(KeySharedPolicy.KeySharedPolicySticky
                    .stickyHashRange()
                      .ranges(Range.of(0, 100), Range.of(1000, 2000)))
            .subscribe();