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

RabbitMQ will be used for RabbitMQ, and Direct Exchange will be used for RabbitMQ.

Topic Exchange

The topic switch, described briefly above, delivers messages to the corresponding queue according to certain rules. So let’s just say briefly what is this rule?

In Direct Exchange, the routing key can be composed of any rule. In the topic switch, the routing key must be a string of words separated by dots. These words can be arbitrary and you can name them according to your business. The switch and queue binding also has two more wildcards.

Introduction to wildcards
  • * matches any word
  • # matches any one or more words
Write RabbitMQ examples
  • Producer projects create topic switches
package com.chentawen.rabbitmqprovider.config; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author: CTW * @Date: Create in 2021/8/3 21:03 */ @configuration Public class TopicExchangeConfig {/** * declare the topic switch ** @return */ @bean TopicExchange MyTopicExchange() { return new TopicExchange("MyTopicExchange", true, false); } A ** @queue MyTopicQueueA() {return new Queue("MyTopicQueueA", true); } @bean Queue MyTopicQueueB() {return new Queue("MyTopicQueueB", true); 1 ** @return */ @bean Binding bindingDirect1() {return BindingBuilder.bind(MyTopicQueueA()).to(MyTopicExchange()).with("MyTopicQueue.A"); } /** * @bean Binding bindingDirect2() {return BindingBuilder.bind(MyTopicQueueB()).to(MyTopicExchange()).with("MyTopicQueue.#"); }}Copy the code
  • The producer project creates the message sending interface
/ * * * send messages to subject switches - method 1 * @ return * / @ GetMapping (" sendMessageTopicExchange1 ") public String sendMessageTopicExchange1 () {  String messageId = String.valueOf(UUID.randomUUID()); String messageData = "Hello World! sendMessageTopicExchange1"; 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 (" MyTopicExchange ", "MyTopicQueue.A", map); Return "Message sent successfully!" ; } / * * * send a message to the theme switches - method 2 * @ return * / @ GetMapping (" sendMessageTopicExchange2 ") public String sendMessageTopicExchange2 ()  { String messageId = String.valueOf(UUID.randomUUID()); String messageData = "Hello World! sendMessageTopicExchange2"; 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 (" MyTopicExchange ", "MyTopicQueue.B", map); Return "Message sent successfully!" ; }Copy the code
  • The consumer project creates A message receive listener class (listener queue: mytopicQueue.a)
package com.chentawen.rabbitmqconsumer.listener; //package com.chentawen.springbootall.config.rabbitlistener; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.util.Map; /** * @Author: CTW * @Date: create in 2021/8/2 22:23 */ @Component @RabbitListener(queues = "MyTopicQueue.A") public class TopicReceiver { @rabbithandler public void process(Map MessageData) {system.out.println (" Rabbitmq - Consumer1 received the message: " + MessageData.toString()); }}Copy the code
  • The consumer project creates a message receive listener class (listener queue: mytopicQueue.b)
package com.chentawen.rabbitmqconsumer.listener; //package com.chentawen.springbootall.config.rabbitlistener; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.util.Map; /** * @Author: CTW * @Date: create in 2021/8/2 22:23 */ @Component @RabbitListener(queues = "MyTopicQueue.B") public class TopicReceiver2 { @rabbithandler public void process(Map MessageData) {system.out.println (" Rabbitmq - Consumer2 received the message: " + MessageData.toString()); }}Copy the code
  • Start the project, send a message using the Postman access interface, and watch the consumer project console

Access interface 1: It can be seen that consumers receive messages from queue A and queue B and send messages frequently, but the order of consumption may be different

I don’t know if you can understand it, but LET me analyze it for you.

The routing key of interface 1 is: Mytopicqueue. A, corresponding to the routing key value mytopicQueue. A bound between the switch and queue A, So queue A can receive bindingBuilder.bind (MyTopicQueueA()).to(MyTopicExchange()).with(” mytopicQueue.a “)

The switch is bound to queue B with the route key MyTopicQueue.#, which matches any one or more words Bindingbuilder.bind (MyTopicQueueB()).to(MyTopicExchange()).with(“MyTopicQueue.#”)

Access interface 2: You can see that the consumer only received messages for queue B

The switch is bound to queue B with the route key MyTopicQueue.#. Bindingbuilder.bind (MyTopicQueueB()).to(MyTopicExchange()).with(“MyTopicQueue.#”)

  • What happens if you send an unmatched routing key

Consumers who request this interface will not receive the message because the message cannot find the routing key, so the message will be discarded later

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