Skip to main content

RabbitMQ study notes (1) Quick use

RabbitMQ study notes (1) Quick use

Quick use:

1. First start from docker . Pay attention to the version number, port mapping 15672 and 5672, and mount it locally. If the environment variable username and password are not configured, the default is guest. If it is started successfully, open 127.0.0.1:15672 in the browser to enter. console

2. Springboot project import dependencies

       1. <dependency>

2. <groupId>org.springframework.boot</groupId>

3. <artifactId>spring-boot-starter-amqp</artifactId>

4. </dependency>



3. Simple configuration yml file

       1. spring.

2. rabbitmq.

3. #hostname

4. host: 127.0.0.1

5. #port number

6. port: 5672

7. # virtual-machine-name (for data isolation)

8. virtual-host: /noop

9. username: noop

10. password: 123456



4. Simply use the Rabbittemplate class to send messages, create a class to receive messages, and use the annotation @RabbitListener on the method.

Simple example: (send one to queue every 0.1s)

       1. @Test

2. void MQtest() throws InterruptedException {

3. String queueName ="simple.queue";

4. for(int i = 1 ;i<=50;i++) {

5. String msg = "hello,amqp,message_" + i;

6. rabbitTemplate.convertAndSend(queueName, msg);

7. Thread.sleep(100);

8. }



       1. @Component

2. public class Listenertest {

3.

4. @RabbitListener(queues = "simple.queue")

5. public void listenSimple1(String msg){

6. System.out.println("Message received:"+msg+"。");

7. }

8. }



3. When there are multiple consumers in the same queue, the default message delivery method is polling , one message per person. Regardless of the processing capacity of the consumer, message accumulation will occur. You can add the following configuration to the yml file to change it to those who can do more. .

       1.     

2. spring.

3. rabbitmq.

4. # Configure the consumer

5. listener.

6. simple.

7. # Prefetch 1 item at a time, and then fetch the next item after processing is complete.

8. prefetch: 1