tencent cloud

Feedback

Using Message Retry and Dead Letter Queue

Last updated: 2024-08-19 16:04:12

    Overview

    A retry Topic is a Topic designed to ensure that messages are consumed normally. If no normal response is received after a message is consumed by the consumer for the first time, it will enter the retry Topic. When a certain number of retries are reached, the retries are stopped and the messages will be delivered to the dead letter Topic.
    When messages enter the dead-letter queue, it indicates that the TDMQ for Apache Pulsar can no longer process it automatically. At this point, human intervention is generally required to handle these messages. You can write a dedicated client to subscribe to the dead letter Topic to process such messages.
    For more details on message retry and dead letter, please see the document Message Retry and Dead Letter Mechanisms.
    This document uses the Demo provided on the official website as an example to introduce the features and usage of the message retry and dead letter queue.

    Directions

    Step 1. Creating a Pulsar Resource in the Console

    1. Log in to the TDMQ for Apache Pulsar Console, and create a cluster and namespace.
    2. In the left sidebar, select Topic Management, and check the cluster and namespace created in the introductory course for the current cluster and namespace.
    3. Click Create, enter the Topic name and description, keep other options as default, click Save, and create a topic.
    
    4. Click Add Subscription in the operation column of the created Topic, enter the subscription name, enable automatically create retry & dead-letter queues, and select the default suffix Apache****Pulsar SDK (upper case) . This creates a subscription relationship for the created Topic and enables retry & dead-letter queues.
    
    5. After submission, you can see the automatically created retry queue and dead-letter queue by the system.
    
    6. 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 the 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. Enter the /deadletter directory and modify the SimpleProducer.java 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.
    
    2. Compile and run the SimpleProducer.java program to send messages. The result is as follows:
    
    3. Log in to TDMQ for Apache Pulsar console. On the message query page, you can see the messages produced in the topic.
    

    Step 4. Consuming Messages

    1. Modify the parameters in RetryConsumer.java.
    
    .retryLetterTopic: retry topic name.
    .deadLetterTopic: dead letter topic name.
    .topic: topic name.
    You need to provide the complete path of the above 3 topics, 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. It can be viewed on the Topic consumer interface.
    
    2. Compile and run the message retry program RetryConsumer.java. The result is as follows:
    
    3. Log in to the TDMQ for Apache Pulsar console. On the message query page, it is observed that the retry topic displays 2 messages, and the dead letter topic displays 1 message, indicating that after 2 attempts of message delivery, it was moved to the dead letter queue.
    
    
    

    Step 5. Consuming Dead Letter Messages

    1. Modify the parameters in DeadConsumer.java.
    
    .topic: Enter the dead letter topic name. 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. It can be viewed on the Topic consumer interface.
    
    2. Compile and run the message retry program RetryConsumer.java. The results are as follows:
    
    3. Log in to the TDMQ for Apache Pulsar console. On the dead letter topic consumer page, if the number of heaped messages turns to 0, it indicates that the messages have been consumed.
    
    
    
    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