tencent cloud

Feedback

Subscription Mode

Last updated: 2022-06-29 15:16:26

    In order to meet the needs of different use cases, TDMQ for Pulsar supports four subscription modes: exclusive, shared, failover, and key_shared.

    Exclusive Mode

    Exclusive mode (default): A subscription can be associated with only one consumer. Only this consumer can receive all messages in the topic, and if it fails, consumption will stop.

    In the exclusive subscription mode, only one consumer in a subscription can consume messages in the topic. If multiple consumers subscribe, an error will be reported. This mode is suitable for globally sequential consumption scenarios.

    // Construct a consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer()
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`, which can be copied from **Topic Management**
    .topic("persistent://pulsar-xxx/sdk_java/topic1")
    // You need to create a subscription on the topic details page in the console and enter the subscription name here
    .subscriptionName("sub_topic1")
    // Declare the exclusive mode as the consumption mode
    .subscriptionType(SubscriptionType.Exclusive)
    .subscribe();

    If multiple consumers are started, an error will be reported.

    Shared Mode

    Messages are distributed to different consumers through a customizable round robin mechanism, with each message going to only one consumer. When a consumer is disconnected, any messages delivered to it but not acknowledged are redistributed to other active consumers.

    // Construct a consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer()
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`, which can be copied from **Topic Management**
    .topic("persistent://pulsar-xxx/sdk_java/topic1")
    // You need to create a subscription on the topic details page in the console and enter the subscription name here
    .subscriptionName("sub_topic1")
    // Declare the shared mode as the consumption mode
    .subscriptionType(SubscriptionType.Shared)
    .subscribe();

    There can multiple consumers in the shared mode.

    Failover Mode

    If there are multiple consumers, they will be sorted lexicographically, and the first consumer will be initialized to be the only one who can receive messages. When the first consumer is disconnected, all messages (unacknowledged and subsequent) will be distributed to the next consumer in the queue.

    // Construct a consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer()
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`, which can be copied from **Topic Management**
    .topic("persistent://pulsar-xxx/sdk_java/topic1")
    // You need to create a subscription on the topic details page in the console and enter the subscription name here
    .subscriptionName("sub_topic1")
    // Declare the failover mode as the consumption mode
    .subscriptionType(SubscriptionType.Failover)
    .subscribe();

    There can be multiple consumers in the failover mode.

    Key_Shared Mode

    If there are multiple consumers, messages will be distributed by key, and messages with the same key will only be distributed to the same consumer.

    // Set the key when sending messages
    MessageId msgId = producer.newMessage()
    // Message content
    .value(value.getBytes(StandardCharsets.UTF_8))
    // Set the key here. Messages with the same key will only be distributed to the same consumer.
    .key("youKey1")
    .send();
    // Construct a consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer()
    // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`, which can be copied from **Topic Management**
    .topic("persistent://pulsar-xxx/sdk_java/topic1")
    // You need to create a subscription on the topic details page in the console and enter the subscription name here
    .subscriptionName("sub_topic1")
    // Declare the key_shared mode as the consumption mode
    .subscriptionType(SubscriptionType.Key_Shared)
    .subscribe();

    There can be multiple consumers in the key_shared mode.

    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 avaliable.

    7x24 Phone Support