tencent cloud

All product documents
TDMQ for Apache Pulsar
DocumentationTDMQ for Apache PulsarGetting StartedUsing Share Subscription Mode to Consume Messages
Using Share Subscription Mode to Consume Messages
Last updated: 2024-08-19 16:08:01
Using Share Subscription Mode to Consume Messages
Last updated: 2024-08-19 16:08:01

Overview

To meet the needs of different scenarios, Pulsar supports four subscription modes: Exclusive, Shared, Failover, and Key_Shared.
1. Exclusive mode (default): A subscription can only be associated with one consumer, and only this consumer can receive all messages from the topic. If this consumer can not run normally, consumption will be stopped.
2. Shared mode: Messages are distributed to different consumers through a round robin mechanism (customizable), and each message is sent to only one consumer. When a consumer disconnects, all messages sent to him that have not been acknowledged will be reallocated and distributed to other active consumers.
3. Failover mode: When there are multiple consumers, they are ordered lexicographically, and the first consumer is initialized as the sole receiver of messages. When the consumer disconnects from the system, all messages (both unacknowledged and new ones) are distributed to the next consumer in the queue.
4. Key_Shared mode: When there are multiple consumers, messages are distributed based on their keys. Messages with the same key are distributed to the same consumer.
For more information on subscription modes, see the document Subscription Modes.
This document uses the Demo provided on the official website as an example to introduce the features and usage of the Shared subscription mode.

Directions

Step 1. Creating a Pulsar Resource in the Console

1. Log in to TDMQ for Apache Pulsar Console, and create a cluster and namespace.
2. In the left sidebar, select the Topic Management tab, and check the created cluster and namespace for the current cluster and namespace.
3. Click Create, enter the Topic name and description, keep the other options as default, click Save, and create a topic.

4. Click Add Subscription in the operation column to create two subscription relationships for the created Topic.
5. Click More > View Subscription/Consumer in the operation column to see the created subscriptions.



Step 2. Downloading the Demo and Configuring the Related Parameters

1. Download Official Demo and decompress it.
2. Modify the Constant.java parameters.

Parameter
Description
SERVICE_URL
Cluster access address, which can be viewed and copied from the Console Cluster Management Page

AUTHENTICATION
Role key, which can be copied from the Role Management


Step 3. Producing Messages

1. Go to the /simple directory and modify the SimpleProducer.java parameters.

topic: Enter the name of the created topic3. You need to provide the complete path, which is persistent://clusterid/namespace/Topic. The parts clusterid/namespace/topic can be directly copied from the Topic Management Page in the console.

2. Compile and run the SimpleProducer.java program to send messages. The result is as follows: you can see that the producer has sent 10 messages to the topic.


Step 4. Consuming Messages

1. Go to the /submodule directory and modify the SharedConsumer1.java and SharedConsumer2.java program parameters.

topic: Enter the name of the created topic. You need to provide the complete path, which is persistent://clusterid/namespace/Topic. The parts clusterid/namespace/topic can be directly copied from the Topic Management Page in the console.

.subscriptionName: Enter the subscription name for the topic, which can be viewed in the Topic Consumers interface. For SharedConsumer1, enter the subscription name sub1, and for SharedConsumer2, enter the subscription name sub2.

2. Compile and run the Message Retry Program, SharedConsumer1.java, and SharedConsumer2.java. The result is as follows:




Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon