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

Fanout Exchange (Fanout Exchange) RabbitMQ (Fanout Exchange) RabbitMQ (Fanout Exchange)

Fanout Exchange

The fan-shaped switch is simpler than the first two because it does not have a routing key. If you bind a routing key, it is invalid. The switch receives a message sent by the producer and sends it directly to the bound queue.

Write RabbitMQ examples
  • Producer projects create sector switches
package com.chentawen.rabbitmqprovider.config; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.FanoutExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author: CTW * @Date: Create in 2021/8/4 21:38 */ @configuration Public class FanoutExchangeConfig {/** * declare the sector switch ** @return */ @bean FanoutExchange MyFanoutExchange() { return new FanoutExchange("MyFanoutExchange", true, false); } /** * Queue MyFanoutQueueA() {return new Queue("MyFanoutQueueA", true); MyFanoutQueueB() {return new Queue("MyFanoutQueueB", true); } @bean Binding bindingFanoutA() {return BindingBuilder.bind(MyFanoutQueueA()).to(MyFanoutExchange()); } @bean Binding bindingFanoutB() {return BindingBuilder.bind(MyFanoutQueueB()).to(MyFanoutExchange()); }}Copy the code
  • The producer project creates the message sending interface
/ * * * * * send a message to the fan switches @ return * / @ GetMapping (" sendMessageFanoutExchange ") public String sendMessageFanoutExchange () { String messageId = String.valueOf(UUID.randomUUID()); String messageData = "Hello World! sendMessageFanoutExchange"; 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 (" MyFanoutExchange ", null, map); Return "Message sent successfully!" ; }Copy the code
  • The consumer project creates a message receive listener class

Listening queue 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 21:25 */ @Component @RabbitListener(queues = "MyFanoutQueueA") public class FanoutReceiver { @rabbithandler public void process(Map MessageData) {system.out.println (" Rabbitmq - Consumer1 received the message: " + MessageData.toString()); }}Copy the code

Listening queue 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 21:25 */ @Component @RabbitListener(queues = "MyFanoutQueueB") public class FanoutReceiver2 { @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

You can see that queues A and B both receive messages, as long as queues are bound to the switch

So far, the introduction of the three main switches has been completed. The following mainly introduces several situations that may happen after the producer pushes the message, and how to deal with them, and the message confirmation of consumers (manual/automatic)

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