RabbitMQ message queue code case (Demo detailed explanation)

Hello, I’m Chenxi. I’m glad you can read it. This article organizes the basic use cases of message queues, shares new knowledge, and makes progress together!

Article Directory


1. Preface

In interviews, you will often ask why you use message queues

The interviewer's more expected answer is the real application scenario of your project, what problems have been solved using MQ, what benefits it brings, and if there are no drawbacks, etc.

Insert picture description here

The three core points of the message queue: decoupling, asynchronous, and peak clipping.

Reference article: Detailed explanation of message queue function (decoupling, asynchronous, peak clipping)

The message queue is also designed to the producer, the consumer principle can be simply understood

Reference article: Producer consumer problem-detailed code (Java multithreading)


Two, code analysis

How to use rabbitmq

First create a configuration class, create some queues, and give the queue names

@Configuration
public class RabbitMqConfig {
    
    

   @Bean
    public Queue queue1(){
    
    
        return new Queue("test1");
    }
   @Bean
    public Queue queue2(){
    
    
        return new Queue("test2");
    }

    @Bean
    public Queue queue3(){
    
    
       return new Queue("test3");
    }
}

Create an amqpTemplate object in the controller layer, this object can pass relevant information, it can send the information to the relevant queue

Then there are consumers who listen to the queue can consume the sent information

@RestController
public class SendDemoController {
    
    
    @Autowired
    private AmqpTemplate amqpTemplate;
    
    @RequestMapping("/multiSend")
    public String multiSend(){
    
    
        StringBuilder times=new StringBuilder();
        for(int i=0;i<10;i++){
    
    
            long time=System.nanoTime();
            amqpTemplate.convertAndSend("test1","第"+i+"次发送的时间:"+time);
            times.append(time+"<br>");
        }
        return times.toString();
    }

    @RequestMapping("/multi2MultiSend")
    public String mutil2MutilSend(){
    
    
        StringBuilder times=new StringBuilder();
        for(int i=0;i<10;i++){
    
    
            long time=System.nanoTime();
            amqpTemplate.convertAndSend("test1","第"+i+"次发送的时间:"+time);
            amqpTemplate.convertAndSend("test2","第"+i+"次发送的时间:"+time);
            times.append(time+"<br>");
        }
        return times.toString();
    }
   //测试发送string类型的消息
    @RequestMapping("/sendTest3")
    public void sendTestString(){
    
    
        StringBuilder a = new StringBuilder();
        for (int i=0;i<10;i++){
    
    
            String uuid = UUID.randomUUID().toString();
            amqpTemplate.convertAndSend("test3","第"+i+"次发送的时间:"+uuid);
            a.append(uuid+"<br>");
        }
    }

  //测试发送int类型的消息
    @RequestMapping("/sendTest33")
    public void sendTestInteger(){
    
    
        StringBuilder a = new StringBuilder();
        for (int i=0;i<10;i++){
    
    
            amqpTemplate.convertAndSend("test3",1);
        }
    }

}

Consumer 1 listens to the queue test1, and the output is on the console after consumption

@RabbitListener can be marked on the class, and it needs to be used with the @RabbitHandler annotation.
@RabbitListener marked on the class means that when a message is received, it will be handed over to the @RabbitHandler method.

@Component
@RabbitListener(queues = "test1")
public class Receiver1 {
    
    

    @RabbitHandler
    public void  recevier(String msg){
    
    
        System.out.println("Test1 receiver1:"+msg);
    }

}

First test our first access path

Insert picture description here
The output of the console is as follows

Test1 receiver1:0次发送的时间:88337654992000
Test1 receiver1:1次发送的时间:88337661695100
Test1 receiver1:2次发送的时间:88337663620800
Test1 receiver1:3次发送的时间:88337664685500
Test1 receiver1:4次发送的时间:88337671656700
Test1 receiver1:5次发送的时间:88337673557700
Test1 receiver1:6次发送的时间:88337675688700
Test1 receiver1:7次发送的时间:88337676873100
Test1 receiver1:8次发送的时间:88337679355700
Test1 receiver1:9次发送的时间:88337681630300

Consumer 2 monitor

@Component
@RabbitListener(queues = "test2")
public class Receiver2 {
    
    

    @RabbitHandler
    public void receiver(String msg){
    
    
        System.out.println("Test2 receiver2:"+msg);
    }
}

The test accesses the second path, and sends information to the queues test1 and test2. The
Insert picture description here
output of the console consumer 1 and consumer 2 represents the successful consumption of the consumer
Insert picture description here

If you overload different methods in a listening queue, you can consume string type or Integer type as follows

At this time, the queue monitors different types of messages and can identify by itself—>and then different output statements

Which method is used for processing, according to the parameter type after MessageConverter conversion

@Component
@RabbitListener(queues="test3")
public class Receiver3 {
    
    

    @RabbitHandler
    public void receiver3(String msg){
    
    
        System.out.println("消费者3接受的消息是"+msg);
    }

    @RabbitHandler
    public void receiver3(Integer msg){
    
    
        System.out.println(msg);
    }
}

The test visits the third path and sends information to the queue test3. Observe that the information is sent to the String type at the controller layer

Insert picture description here
The test visits the fourth path and sends information to the queue test3. Observe the controller layer. The output of this information to the Integer type
Insert picture description here
console is as follows. You can see that the string type and integer type are consumed in turn.

Insert picture description here
Project configuration file: The above code is recommended for beginners to practice it by themselves

server.port=8099
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

I believe you must have understood the basic operation process of the entire project. This article is just a basic application practice case. In fact, rabbitmq has many details and foundations worthy of our deep learning. See you in the next issue!


The best investment is to invest in yourself.

Insert picture description here

2020.09.12 Autumn is a harvest season, see you from a higher level!

Guess you like

Origin blog.csdn.net/weixin_45393094/article/details/108553523