This is the sixth day of my participation in the August Text Challenge.More challenges in August

Previous articles have introduced the three main switch, I believe that the producer sends messages to the switch and then to the queue everyone is very familiar with, this article is mainly about the producer message acknowledgement mechanism.

Message acknowledgement mechanism for producers
  • The first step is to add enable sender message acknowledgement to the producer’s configuration file
Publisher-confirmation-type: correlated messages were forwarded to queues publisher-returns: trueCopy the code
  • Create the sender message confirmation configuration class
package com.chentawen.rabbitmqprovider.config; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author CTW */ @Configuration public class RabbitMqCallbackConfig { @Bean public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate rabbitTemplate = new RabbitTemplate(); rabbitTemplate.setConnectionFactory(connectionFactory); / open/set Mandatory, can trigger a callback function, no matter how did the news to push force the callback function is invoked rabbitTemplate. SetMandatory (true); / / confirmation message sent to the switches (Exchange) callback rabbitTemplate. SetConfirmCallback ((correlationData, ack, Cause) -> {system.out.println (" Exchange result: "); System.out.println(" correlationData: "+ correlationData); System.out.println(" success: "+ ack); System.out.println(" error cause: "+ cause); System.out.println("------------------------------"); }); / / a confirmation message to the Queue (Queue) callback rabbitTemplate. SetReturnsCallback ((returnedMessage) - > {System. Out. Println (" confirmation message to the Queue (Queue) results: "); System. The out. Println (" send a message: "+ returnedMessage. GetMessage ()); System. The out. Println (" response code: "+ returnedMessage. GetReplyCode ()); System. The out. Println (" response information: "+ returnedMessage. GetReplyText ()); System. The out. Println (" switch: "+ returnedMessage. GetExchange ()); System. The out. Println (" routing key: "+ returnedMessage. GetRoutingKey ()); System.out.println("------------------------------"); }); return rabbitTemplate; }}Copy the code
  • Problems with producers sending messages
  1. Switch not found

Modify the message sent to direct switch (MyDirectExchangeTest– not present)

/ * * * * * send a message to the direct connect switches @ return * / @ GetMapping ("/sendMessageDirectExchange ") public String sendMessageDirectExchange () { String messageId = String.valueOf(UUID.randomUUID()); String messageData = "Hello World!" ; String createTime = LocalDateTime. Now (). The format (DateTimeFormatter. OfPattern (" on dd MM yyyy years HH: MM: ss ")); Map<String, Object> map = new HashMap<>(16); map.put("messageId", messageId); map.put("messageData", messageData); map.put("createTime", createTime); / * * * name * routingKey routing key exchange switches * the content of messages sent map * / rabbitTemplate. ConvertAndSend (" MyDirectExchangeTest ", "DirectRoutingKey", map); Return "Message sent successfully!" ; }Copy the code

Console print result:

  1. Queue not found

Adds an unqueued switch to a direct switch configuration

/ * * * * statement directly connected switches unbounded queue @ return * * * / @ Bean DirectExchange MyDirectExchangeNoBindQueue () {return new DirectExchange("MyDirectExchangeNoBindQueue", true, false); }Copy the code

Send a message to the direct connect switch (MyDirectExchangeNoBindQueue – unbound queue)

/ * * * * * send a message to the direct connect switches @ return * / @ GetMapping ("/sendMessageDirectExchange ") public String sendMessageDirectExchange () { String messageId = String.valueOf(UUID.randomUUID()); String messageData = "Hello World!" ; String createTime = LocalDateTime. Now (). The format (DateTimeFormatter. OfPattern (" on dd MM yyyy years HH: MM: ss ")); Map<String, Object> map = new HashMap<>(16); map.put("messageId", messageId); map.put("messageData", messageData); map.put("createTime", createTime); / * * * name * routingKey routing key exchange switches * the content of messages sent map * / rabbitTemplate. ConvertAndSend (" MyDirectExchangeNoBindQueue ", "DirectRoutingKey", map); Return "Message sent successfully!" ; }Copy the code

Console print result:

  1. The switch and queue were not found

The message goes to the switch first, and if the switch doesn’t find it, it’s case 1

  1. Message sent successfully

Send messages to directly connected switches (MyDirectExchange– bound to queues)

/ * * * * * send a message to the direct connect switches @ return * / @ GetMapping ("/sendMessageDirectExchange ") public String sendMessageDirectExchange () { String messageId = String.valueOf(UUID.randomUUID()); String messageData = "Hello World!" ; String createTime = LocalDateTime. Now (). The format (DateTimeFormatter. OfPattern (" on dd MM yyyy years HH: MM: ss ")); Map<String, Object> map = new HashMap<>(16); map.put("messageId", messageId); map.put("messageData", messageData); map.put("createTime", createTime); / * * * name * routingKey routing key exchange switches * the content of messages sent map * / rabbitTemplate. ConvertAndSend (" MyDirectExchange ", "DirectRoutingKey", map); Return "Message sent successfully!" ; }Copy the code

Console print result:

Knowing the callback functions that will be called in these cases, you can make special processing based on your project’s needs

Update the consumer’s message confirmation mechanism later

That’s all for this episode, and we’ll keep updating