Mi Galaxy EMQ Book

The Dead Letter Queue is same as a normal queue, except that it is created to isolate and analyze messages that another queue (source queue) has not successfully processed. See the [

 createqueue () ](create_delete_purge_Queue.md) API for methods for creating dead letter queues, which is no different than creating a normal queue; a dead letter queue cannot invoke the 

[

 deadmessage (), Deadmessagebatch ](deadMessage.md) APIs. All other operations are the same as for normal queue operations.

To send a message from the source queue that was not successfully processed to the dead letter queue, we added the concept of "redrive policy" for the source queue, with detailed definitions and APIs as follows:

setQueueRedrivePolicy()


Add a redrive policy for the (source) queue.

Method parameter SetQueueRedrivePolicyRequest

queueName : String : required

Source Queue name

redrivePolicy : RedrivePolicy : required

Each queue can only set one redrive policy.

- dlqName : String : required

The name of the dead letter queue.

- maxReceiveTime : int : required

The maximum received number of times received from the source queue. The value range is [1, 100]. Messages received exceeding this number are sent from the source queue to the dead letter queue.

Note: If a redrive policy is set, but the dead letter queue is deleted, all of the source queue's redrive policies for the dead letter queue are invalid. The source queue and dead letter queues should not be topic/priority, and the source queue is not a dead letter queue for other queues, and the dead letter queue cannot have a redrive policy. To view the current queue's redrive policy, use the [

 getqueueinfo () ](QueueInfo.md) API.

Method return SetQueueRedrivePolicyResponse

queueName : String : required

Source Queue name

redrivePolicy : RedrivePolicy : required

Redrive policy.

removeQueueRedrivePolicy()


Remove the redrive policy for the queue.

Method parameter RemoveQueueRedrivePolicyRequest

queueName : String : required

Source Queue name

Receiving messages from dead letter queues


Use the [

 receivemessage () ](receiveMessage.md) API. The received message attributes additionally contains the following:
  1. sourceQueueName: Name of source queue
  2. sourceTag: After message was received by the Tag in the source queue, the condition of arrival in the redrive expires
  3. deadTimestamp: The time at which the message was declared dead in the source queue (no longer received by the corresponding tag from the source queue)
  4. originalMessageID: The original MessageID of the message, that is, the MessageID in the source queue
  5. originalReceiveCount: The number of times the message was received in the source queue

Because a dead letter queue may have multiple source queues, to make it easier for users to distinguish different source queue messages, we define a user attribute for messages from the source queue: The name is "DLQ.sourceQueue", the type is "STRING", and the value is the name of the source queue. Users can use this attribute to filter messages from a specific source queue in a dead letter queue.

Note: DLQ.sourceQueue is a user attribute keyword reserved by the system. Users should not use the attribute name.

Send a message to the dead letter queue


There are two ways to "send" messages to the dead-letter queue. One way is to use to send messages to normal queues, using the [

sendMessage(), sendMessageBatch](sendMessage.md) APIs. Messages sent this way are received without the above attributes. Another way is to invoke the 

[

deadMessage(), deadMessageBatch](deadMessage.md) APIs from the source queue. This process of sending a message can be understood as (source) to another type of the message's ACK, when the user detects that some of the received messages are invalid, the user can actively send these messages to the dead-letter queue for subsequent analysis.

Multi-reader/Tag Related


The redrive policy is valid for all tags, but the tag is independent. When some of the messages received in the source queue are sent to the dead letter queue, they will not received again by a receiver using the tag, while a receiver using another tag is still able to receive these messages.