Addressing mq message reliability delivery addresses this problem in three ways

1. Message producers

2. The message is in the queue

3. Message consumers


As shown in figure:


1. The sender loses data

Transaction mode

// Set channel to transaction mode
channel.tsSelect(); 

// Commit the transaction
channel.txCommit(); 

// Transaction rollback
channel.txRooback(); 

// Transaction mode, start a transaction and send a message.
// The transaction is rolled back, and if there is no exception, the transaction is committed
// This approach results in throughput degradation. So the next way is mostly on the line
Copy the code


Confirm pattern

Confirm mode, which assigns a unique ID to each message and returns an ACK notification to the sender if the message is sent to the specified queue. If the queue does not receive the message, it returns a NACK telling you to retry// Set channel to Confirm mode
channel.confirmSelect(); 

if (channel.waitForCofirms()){ 
    // Message sent successfully
}
Copy the code


2. Data is lost in the message queue

This configuration can be used in conjunction with confirm. You can send an Ack signal to the producer after the message is persisted to the disk. So how do you persist? It's easy, by the way, to do the following two steps1.Set the durable identifier of queue to "durable"true, represents a persistent queue2.Set deliveryMode= when sending a message2
Copy the code

Routing to ensure

Switch to queue mandtory = true + ReturnListener Specifies the backup switch of the switchCopy the code

Message persistence

// Queue persistence
@Bean 
public Queue logUserQueue(a) { 
    return new Queue("log.user.queue.name".true); 
    } 
    
// Switch persistence
@Bean public DirectExchange logUserExchange(a) { 
    return new DirectExchange("log.user.exchange".true.false); 
} 

// Message persistence
public static Message objToMsg(Object obj) { 
    if (null == obj) { 
        return null; 
    } 
    Message message = MessageBuilder.withBody(JsonUtil.objToStr(obj).getBytes()).build(); 
    message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
    // Message persistence
    message.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_JSON); 
    return message; 
} 

// 4Note:1, persist the message, do not persist the queue, the server restarts, the message will still disappear2If the number of messages stored by the message middleware is full and reaches the capacity threshold of the message queue, the middleware will refuse to receive messages from producers3, message acknowledgement mechanism to confirm the successful delivery of message to message middleware4If the consumer fails to consume the message, the manual implementation of ACK (dead-letter queue) core: through the message confirmation mechanism, the message will be deleted after the successful delivery or consumptionCopy the code


3. Consumers lose data

This generally occurs in the case of automatic consumer confirmation, although consumers receive a message, automatically sent ACK, at this time the server is down, at this time the message is lost solution: shut down automatic ACK, using manual ACKCopy the code