SpringBoot RabbitMQ seven working modes

A simple model
  • Simple: one producer, one queue, and one consumer. The producer sends messages to the queue, and the consumer listens to the queue and consumes the messages

The Work mode
  • Work: One producer, one queue, and multiple consumers. The producer sends messages to the queue, and multiple consumers listen to the same queue for consuming messages

Publish/subscribe
  • Publish /subscribe: The publish/subscribe mode contains one producer, one switch, multiple queues, and multiple consumers. Exchanges are directly bound to queues. Producers store messages in queues bound to switches through exchanges, and consumers listen to queues and consume them

Routing patterns
  • Routing: In routing mode, messages can be sent to a specified queue according to the routing key. The Exchange and queue are bound using the routing key. The producer sends messages to the queue precisely using the Exchange and routing key. Consumers listen to queues and consume messages

The theme mode
  • Topic: Topics Mode Supports wildcard operations on top of the routing mode. Based on the wildcard operation, the switch stores the message in the matched queue, and the consumer listens to the queue and consumes it

The Header mode
  • Header: The Header mode eliminates the routing key and uses the key/value pair in the Header for matching. After a match is successful, the message is sent to the queue through the switch, and the sender can obtain and consume the message

The RPC model
  • RPC: The RPC mode is mainly used to obtain the processing result of the consumer. Usually, the producer sends the message to the consumer, and the consumer receives and consumes the message and then returns the processing result to the producer

The RabbitMQ SpringBoot integration

  • Start by creating a SpringBoot project and add the following dependencies to the POM.xml file
<dependency>

      
       org.springframework.boot
       
      
       spring-boot-starter-amqp
        
        
       
        org.springframework.boot
        
       
        spring-boot-starter-web
        
      Copy the code
  • Add the following RabbitMQ configuration to the configuration file
server:
  port: 8888  # set the port number

spring:
  rabbitmq:
    host: 127.0. 01.  Set the host for RabbitMQ
    port: 5672       Set the RabbitMQ service port
    username: guest  Set the RabbitMQ user name
    password: guest  Set the RabbitMQ password
Copy the code
  • New public constant class
public interface RabbitConstant {

    /** * Simple mode */
    String SIMPLE_QUEUE_NAME = "simple_queue";

    /** * work mode */
    String WORK_QUEUE_NAME = "work_queue";

    /** * Publish /subscribe */
    String PUBLISH_SUBSCRIBE_EXCHANGE_NAME = "publish_subscribe_exchange";
    String PUBLISH_SUBSCRIBE_FIRST_QUEUE_NAME = "publish_subscribe_first_queue";
    String PUBLISH_SUBSCRIBE_SECOND_QUEUE_NAME = "publish_subscribe_second_queue";

    /** * Routing mode */
    String ROUTING_EXCHANGE_NAME = "routing_exchange";
    String ROUTING_FIRST_QUEUE_NAME = "routing_first_queue";
    String ROUTING_SECOND_QUEUE_NAME = "routing_second_queue";
    String ROUTING_THIRD_QUEUE_NAME = "routing_third_queue";
    String ROUTING_FIRST_QUEUE_ROUTING_KEY_NAME = "routing_first_queue_routing_key";
    String ROUTING_SECOND_QUEUE_ROUTING_KEY_NAME = "routing_second_queue_routing_key";
    String ROUTING_THIRD_QUEUE_ROUTING_KEY_NAME = "routing_third_queue_routing_key";

    /** * Topics */
    String TOPICS_EXCHANGE_NAME = "topics_exchange";
    String TOPICS_FIRST_QUEUE_NAME = "topics_first_queue";
    String TOPICS_SECOND_QUEUE_NAME = "topics_second_queue";
    String TOPICS_THIRD_QUEUE_NAME = "topics_third_queue";
    String TOPICS_FIRST_QUEUE_ROUTING_KEY = "topics.first.routing.key";
    String TOPICS_SECOND_QUEUE_ROUTING_KEY = "topics.second.routing.key";
    String TOPICS_THIRD_QUEUE_ROUTING_KEY = "topics.third.routing.key";
    String TOPICS_ROUTING_KEY_FIRST_WILDCARD = "#.first.#";
    String TOPICS_ROUTING_KEY_SECOND_WILDCARD = "*.second.#";
    String TOPICS_ROUTING_KEY_THRID_WILDCARD = "*.third.*";

    /** * Header mode */
    String HEADER_EXCHANGE_NAME = "header_exchange";
    String HEADER_FIRST_QUEUE_NAME = "header_first_queue";
    String HEADER_SECOND_QUEUE_NAME = "header_second_queue";

    /** * RPC mode */
    String RPC_QUEUE_NAME = "rpc_queue";

}
Copy the code
  • New Controller request class (to validate results, may be added last)
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RestController
public class RabbitController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/simple")
    public void simple(a) {
        rabbitTemplate.convertAndSend(RabbitConstant.SIMPLE_QUEUE_NAME, "hello world!");
    }

    @GetMapping(value = "/work")
    public void work(a) {
        rabbitTemplate.convertAndSend(RabbitConstant.WORK_QUEUE_NAME, "work hello!");
    }

    @GetMapping(value = "/pubsub")
    public void pubsub(a) {
        rabbitTemplate.convertAndSend(RabbitConstant.PUBLISH_SUBSCRIBE_EXCHANGE_NAME, null."publish/subscribe hello");
    }

    @GetMapping(value = "/routing")
    public void routing(a) {
        // Send a message to the first queue
        rabbitTemplate.convertAndSend(RabbitConstant.ROUTING_EXCHANGE_NAME, RabbitConstant.ROUTING_FIRST_QUEUE_ROUTING_KEY_NAME, "routing hello");
    }

    @GetMapping(value = "/topics")
    public void topics(a) {
        // Send a message to the first queue that can receive the message because the queue wildcard is #.first.# and routing_key is topics.first.routing.key
        rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_FIRST_QUEUE_ROUTING_KEY, "topics hello");
        / / send a message to the second queue, queue at this time also can receive the message, because the queue wildcards for *. Second, #, and routing_key switchable viewer. Second. Routing. The key, the match is successful
        rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_SECOND_QUEUE_ROUTING_KEY, "topics hello");
        // Send a message to the third queue which cannot receive the message because the queue wildcard is *.third.* and routing_key is topics.third. Routing_key
        rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_THIRD_QUEUE_ROUTING_KEY, "topics hello");
    }

    @GetMapping(value = "/header")
    public void header(a) {
        // This message should be received by both queues, with the first queue all matching successfully and the second queue hello-value any matching successfully
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("matchAll"."YES");
        messageProperties.setHeader("hello"."world");
        Message message = new Message("header first hello".getBytes(StandardCharsets.UTF_8), messageProperties);
        rabbitTemplate.convertAndSend(RabbitConstant.HEADER_EXCHANGE_NAME, null, message);

        // This message should only be received by the second queue, the first queue all failed to match and the second queue matchall-no any succeeded
        MessageProperties messagePropertiesSecond = new MessageProperties();
        messagePropertiesSecond.setHeader("matchAll"."NO");
        Message messageSecond = new Message("header second hello".getBytes(StandardCharsets.UTF_8), messagePropertiesSecond);
        rabbitTemplate.convertAndSend(RabbitConstant.HEADER_EXCHANGE_NAME, null, messageSecond);
    }

    @GetMapping(value = "/rpc")
    public void rpc(a) {
        Object responseMsg = rabbitTemplate.convertSendAndReceive(RabbitConstant.RPC_QUEUE_NAME, "rpc hello!");
        System.out.println("rabbit rpc response message: "+ responseMsg); }}Copy the code
SpringBoot RabbitMQ simple mode
  • The producer declares the queue and sends a message to the queue
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitSimpleProvider {

    @Bean
    public Queue simpleQueue(a) {
        return newQueue(RabbitConstant.SIMPLE_QUEUE_NAME); }}Copy the code
  • Consumers listen to queues and consume messages
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitSimpleConsumer {

    @RabbitHandler
    @RabbitListener(queues = RabbitConstant.SIMPLE_QUEUE_NAME)
    public void simpleListener(String context) {
        System.out.println("rabbit receiver: "+ context); }}Copy the code
  • Unit testing
@Test
public void simple(a) {
    rabbitTemplate.convertAndSend(RabbitConstant.SIMPLE_QUEUE_NAME, "hello world!");
}
Copy the code
  • In response to the results

SpringBoot RabbitMQ Work mode
  • Producers declare queues and produce messages to queues
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitWorkProvider {

    @Bean
    public Queue workQueue(a) {
        return newQueue(RabbitConstant.WORK_QUEUE_NAME); }}Copy the code
  • Consumers listen to the queue and consume messages (here are two consumers listening to the same queue)
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitWorkConsumer {

    @RabbitListener(queues = RabbitConstant.WORK_QUEUE_NAME)
    @RabbitHandler
    public void workQueueListenerFirst(String context) {
        System.out.println("rabbit workQueue listener first receiver: " + context);
    }

    @RabbitListener(queues = RabbitConstant.WORK_QUEUE_NAME)
    @RabbitHandler
    public void workQueueListenerSecond(String context) {
        System.out.println("rabbit workQueue listener second receiver: "+ context); }}Copy the code
  • Unit testing
@Test
public void work(a) {
    rabbitTemplate.convertAndSend(RabbitConstant.WORK_QUEUE_NAME, "work hello!");
}
Copy the code
  • Response result (since there are two consumers listening on the same queue, the message can only be consumed by one of them, and the default is load balanced sending the message to all consumers)

SpringBoot RabbitMQ publish/subscribe mode
  • The producer declares two queues and a FANout switch and binds the two queues to the switch
  • There are four types of switches: FANout, Direct, Topic, and header
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitPublishSubscribeProvider {

    @Bean
    public Queue pubsubQueueFirst(a) {
        return new Queue(RabbitConstant.PUBLISH_SUBSCRIBE_FIRST_QUEUE_NAME);
    }

    @Bean
    public Queue pubsubQueueSecond(a) {
        return new Queue(RabbitConstant.PUBLISH_SUBSCRIBE_SECOND_QUEUE_NAME);
    }

    @Bean
    public FanoutExchange fanoutExchange(a) {
        // Create a switch of type FANout that will send messages to all bound queues
        return new FanoutExchange(RabbitConstant.PUBLISH_SUBSCRIBE_EXCHANGE_NAME);
    }

    @Bean
    public Binding pubsubQueueFirstBindFanoutExchange(a) {
        // Queue 1 binds the switch
        return BindingBuilder.bind(pubsubQueueFirst()).to(fanoutExchange());
    }

    @Bean
    public Binding pubsubQueueSecondBindFanoutExchange(a) {
        // Bind the switch to queue 2
        returnBindingBuilder.bind(pubsubQueueSecond()).to(fanoutExchange()); }}Copy the code
  • The consumer listens to the queue and makes a purchase
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitPublishSubscribeConsumer {

    @RabbitListener(queues = RabbitConstant.PUBLISH_SUBSCRIBE_FIRST_QUEUE_NAME)
    @RabbitHandler
    public void pubsubQueueFirst(String context) {
        System.out.println("rabbit pubsub queue first receiver: " + context);
    }

    @RabbitListener(queues = RabbitConstant.PUBLISH_SUBSCRIBE_SECOND_QUEUE_NAME)
    @RabbitHandler
    public void pubsubQueueSecond(String context) {
        System.out.println("rabbit pubsub queue second receiver: "+ context); }}Copy the code
  • Unit testing
@Test
public void pubsub(a) {
    rabbitTemplate.convertAndSend(RabbitConstant.PUBLISH_SUBSCRIBE_EXCHANGE_NAME, null."publish/subscribe hello");
}
Copy the code
  • In response to the results

SpringBoot RabbitMQ routing mode
  • The producer declares three queues and a direct switch, binds the three queues to the switch, and sets the route between the switch and the queue
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitRoutingProvider {

    @Bean
    public Queue rabbitRoutingFirstQueue(a) {
        return new Queue(RabbitConstant.ROUTING_FIRST_QUEUE_NAME);
    }

    @Bean
    public Queue rabbitRoutingSecondQueue(a) {
        return new Queue(RabbitConstant.ROUTING_SECOND_QUEUE_NAME);
    }

    @Bean
    public Queue rabbitRoutingThirdQueue(a) {
        return new Queue(RabbitConstant.ROUTING_THIRD_QUEUE_NAME);
    }

    @Bean
    public DirectExchange directExchange(a) {
        // Create a direct switch that represents the exact same queue as this exchange to send messages to the routing_key
        return new DirectExchange(RabbitConstant.ROUTING_EXCHANGE_NAME);
    }

    @Bean
    public Binding routingFirstQueueBindDirectExchange(a) {
        // Bind queue 1 to the direct switch and set routing_key to routing_first_queue_ROUTing_key
        returnBindingBuilder.bind(rabbitRoutingFirstQueue()).to(directExchange()).with(RabbitConstant.ROUTING_FIRST_QUEUE_ROUTING_KEY_ NAME); }@Bean
    public Binding routingSecondQueueBindDirectExchange(a) {
        // Bind queue 2 to the direct switch and set routing_key to routing_second_queue_routing_key
        returnBindingBuilder.bind(rabbitRoutingSecondQueue()).to(directExchange()).with(RabbitConstant.ROUTING_SECOND_QUEUE_ROUTING_KE Y_NAME); }@Bean
    public Binding routingThirdQueueBindDirectExchange(a) {
        // Bind queue three to the direct switch and set routing_key to routing_third_queue_routing_key
        return BindingBuilder.bind(rabbitRoutingThirdQueue()).to(directExchange()).with(RabbitConstant.ROUTING_THIRD_QUEUE_ROUTING_KEY_NAME);
    }

}
Copy the code
  • The consumer listens to the queue and makes a purchase
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitRoutingConsumer {

    @RabbitListener(queues = RabbitConstant.ROUTING_FIRST_QUEUE_NAME)
    @RabbitHandler
    public void routingFirstQueueListener(String context) {
        System.out.println("rabbit routing queue first receiver: " + context);
    }

    @RabbitListener(queues = RabbitConstant.ROUTING_SECOND_QUEUE_NAME)
    @RabbitHandler
    public void routingSecondQueueListener(String context) {
        System.out.println("rabbit pubsub queue second receiver: " + context);
    }

    @RabbitListener(queues = RabbitConstant.ROUTING_THIRD_QUEUE_NAME)
    @RabbitHandler
    public void routingThirdQueueListener(String context) {
        System.out.println("rabbit pubsub queue third receiver: "+ context); }}Copy the code
  • Unit testing
@Test
public void routing(a) {
    // Send a message to the first queue
    rabbitTemplate.convertAndSend(RabbitConstant.ROUTING_EXCHANGE_NAME, RabbitConstant.ROUTING_FIRST_QUEUE_ROUTING_KEY_NAME, "routing hello");
}
Copy the code
  • In response to the results

SpringBoot RabbitMQ theme mode
  • The producer declares three queues and one topic switch. The queues bind to the topic switch and set the routing key wildcard. If the routing key meets the wildcard requirement between the switch and the queue, the message is stored to the queue
  • # wildcards can match one or more words, * wildcards can match one word; If the wildcard of the routing key between the Exchange and the queue is #.hello.#, it indicates that all the routing keys with hello words in the middle meet the conditions and the message is stored in the queue
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitTopicProvider {

    @Bean
    public Queue topicFirstQueue(a) {
        return new Queue(RabbitConstant.TOPICS_FIRST_QUEUE_NAME);
    }

    @Bean
    public Queue topicSecondQueue(a) {
        return new Queue(RabbitConstant.TOPICS_SECOND_QUEUE_NAME);
    }

    @Bean
    public Queue topicThirdQueue(a) {
        return new Queue(RabbitConstant.TOPICS_THIRD_QUEUE_NAME);
    }

    @Bean
    public TopicExchange topicExchange(a) {
        // Create a topic type switch that will send messages to a queue with a routing_key wildcard match
        return new TopicExchange(RabbitConstant.TOPICS_EXCHANGE_NAME);
    }

    @Bean
    public Binding topicFirstQueueBindExchange(a) {
        // Queue 1 bind topic type switches and set the routing_key wildcard to #.first.#
        return BindingBuilder.bind(topicFirstQueue()).to(topicExchange()).with(RabbitConstant.TOPICS_ROUTING_KEY_FIRST_WILDCARD);
    }

    @Bean
    public Binding topicSecondQueueBindExchange(a) {
        // Queue two binds the topic type switch and sets the routing_key wildcard to.second.#
        return BindingBuilder.bind(topicSecondQueue()).to(topicExchange()).with(RabbitConstant.TOPICS_ROUTING_KEY_SECOND_WILDCARD);
    }

    @Bean
    public Binding topicThirdQueueBindExchange(a) {
        // Queue three bind topic switches and set the routing_key wildcard to *.third.*
        returnBindingBuilder.bind(topicThirdQueue()).to(topicExchange()).with(RabbitConstant.TOPICS_ROUTING_KEY_THRID_WILDCARD); }}Copy the code
  • The consumer listens to the queue and makes a purchase
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitTopicsConsumer {

    @RabbitListener(queues = RabbitConstant.TOPICS_FIRST_QUEUE_NAME)
    @RabbitHandler
    public void topicFirstQueue(String context) {
        System.out.println("rabbit topics queue first receiver: " + context);
    }

    @RabbitListener(queues = RabbitConstant.TOPICS_SECOND_QUEUE_NAME)
    @RabbitHandler
    public void topicSecondQueue(String context) {
        System.out.println("rabbit topics queue second receiver: " + context);
    }

    @RabbitListener(queues = RabbitConstant.TOPICS_THIRD_QUEUE_NAME)
    @RabbitHandler
    public void topicThirdQueue(String context) {
        System.out.println("rabbit topics queue third receiver: "+ context); }}Copy the code
  • Unit testing
@Test
public void topics(a) {
    // Send a message to the first queue that can receive the message because the queue wildcard is #.first.# and routing_key is topics.first.routing.key
    rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_FIRST_QUEUE_ROUTING_KEY, "topics hello");
    / / send a message to the second queue, queue at this time also can receive the message, because the queue wildcards for *. Second, #, and routing_key switchable viewer. Second. Routing. The key, the match is successful
    rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_SECOND_QUEUE_ROUTING_KEY, "topics hello");
    // Send a message to the third queue which cannot receive the message because the queue wildcard is *.third.* and routing_key is topics.third. Routing_key
    rabbitTemplate.convertAndSend(RabbitConstant.TOPICS_EXCHANGE_NAME, RabbitConstant.TOPICS_THIRD_QUEUE_ROUTING_KEY, "topics hello");
    }
Copy the code
  • In response to the results

SpringBoot RabbitMQ header mode
  • The producer declares the queue and creates the HeaderExchange switch, binding the queue to the switch via headers
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitHeaderProvider {

    @Bean
    public Queue headerFirstQueue(a) {
        return new Queue(RabbitConstant.HEADER_FIRST_QUEUE_NAME);
    }

    @Bean
    public Queue headerSecondQueue(a) {
        return new Queue(RabbitConstant.HEADER_SECOND_QUEUE_NAME);
    }

    @Bean
    public HeadersExchange headersExchange(a) {
        return new HeadersExchange(RabbitConstant.HEADER_EXCHANGE_NAME);
    }

    @Bean
    public Binding headerFirstQueueBindExchange(a) {
        Map<String, Object> headersMap = new HashMap<>(8);
        headersMap.put("matchAll"."YES");
        headersMap.put("hello"."world");

        return BindingBuilder.bind(headerFirstQueue()).to(headersExchange()).whereAll(headersMap).match();
    }

    @Bean
    public Binding headerSecondQueueBindExchange(a) {
        Map<String, Object> headersMap = new HashMap<>(8);
        headersMap.put("matchAll"."NO");
        headersMap.put("hello"."world");

        returnBindingBuilder.bind(headerSecondQueue()).to(headersExchange()).whereAny(headersMap).match(); }}Copy the code
  • Header Details about switch and queue binding

  • The consumer listens to the queue and makes a purchase
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitHeaderConsumer {

    @RabbitListener(queues = RabbitConstant.HEADER_FIRST_QUEUE_NAME)
    @RabbitHandler
    public void headerFirstQueue(String context) {
        System.out.println("rabbit header queue first receiver: " + context);
    }

    @RabbitListener(queues = RabbitConstant.HEADER_SECOND_QUEUE_NAME)
    @RabbitHandler
    public void headerSecondQueue(String context) {
        System.out.println("rabbit header queue second receiver: "+ context); }}Copy the code
  • Unit testing
@Test
public void header(a) {
    // This message should be received by both queues, with the first queue all matching successfully and the second queue hello-value any matching successfully
    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setHeader("matchAll"."YES");
    messageProperties.setHeader("hello"."world");
    Message message = new Message("header first hello".getBytes(StandardCharsets.UTF_8), messageProperties);
    rabbitTemplate.convertAndSend(RabbitConstant.HEADER_EXCHANGE_NAME, null, message);

    // This message should only be received by the second queue, the first queue all failed to match and the second queue matchall-no any succeeded
    MessageProperties messagePropertiesSecond = new MessageProperties();
    messagePropertiesSecond.setHeader("matchAll"."NO");
    Message messageSecond = new Message("header second hello".getBytes(StandardCharsets.UTF_8), messagePropertiesSecond);
    rabbitTemplate.convertAndSend(RabbitConstant.HEADER_EXCHANGE_NAME, null, messageSecond);
}
Copy the code
  • In response to the results

SpringBoot RabbitMQ RPC mode
  • Producer declaration queue
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitRpcProvider {

    @Bean
    public Queue rpcQueue(a) {
        return newQueue(RabbitConstant.RPC_QUEUE_NAME); }}Copy the code
  • The consumer listens to the queue and makes a purchase
import com.example.rabbitmq.constant.RabbitConstant;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitRpcConsumer {

    @RabbitListener(queues = RabbitConstant.RPC_QUEUE_NAME)
    @RabbitHandler
    public String rpcQueue(String context) {
        System.out.println("rabbit rpc queue receiver: " + context);
        return "copy that!"; }}Copy the code
  • Unit testing
@Test
public void rpc(a) {
    Object responseMsg = rabbitTemplate.convertSendAndReceive(RabbitConstant.RPC_QUEUE_NAME, "rpc hello!");
    System.out.println("rabbit rpc response message: " + responseMsg);
}
Copy the code
  • In response to the results

Exchange switches

  • Fanout switch: If a FANout switch is used to send messages, all queues bound to the FANout switch can receive the messages

  • Direct switch: Sends messages through the Direct switch. The queue with the same routing key in the queue bound to the Direct switch can receive messages

  • Topic switch: After a topic switch sends a message, the queue bound to the topic switch whose routing key is matched with a wildcard can receive the message

Gitee SpringBoot RabbitMQ working mode repository link