1, Basic knowledge
(1) Provider
Message producer is the procedure of message delivery.
(2) Consumer
Message consumers are message receiving programs
(3) Messaging when message queuing is not used
(4) Message passing method after using message queue
(5) What is message queuing
A queue is like a warehouse or store where goods are stored. It is a transfer station between a factory that produces goods and a user who purchases goods
(6) What's in the queue
In MQ, information flows from your application to the queue of MQ. All information can exist in only one queue. The queue can store a lot of information, because it is an unlimited cache area, provided that the storage space you set is large enough
(7) Relationship between queue and Application
Multiple producers can send messages to the same queue, and multiple messagers can get messages from only one message pair.
2, Writing RabbitMQ entry case based on SpringBoot
(1) Create project
Visit the Spring official website to create the SpringBoot project https://start.spring.io/
(2) Add RabbitMQ related dependency
Pom.xml introduces RabbitMQ related dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
(3) Add profile information
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=user_mq spring.rabbitmq.password=123456 spring.rabbitmq.virtual-host=/user_mq
3, Coding
(1) Create RabbitMQ message queue
/** * Create message queue * @Configuration Used to define configuration classes, * The defined configuration class can replace the xml file, and is generally used in conjunction with the @ Bean annotation * @Configuration Annotations are mainly marked on a class, which is equivalent to < beans > in the xml configuration file * @Bean Annotation is mainly marked on a method, which is equivalent to < bean > in xml configuration file * SpringBoot At startup, the @ Configuration annotation will be used to initialize all classes, and the @ Bean annotation method will be executed */ @Configuration public class QueueConfig { /** * Create queue * Note to import org.springframework.amqp.core.Queue package */ @Bean public Queue createQueue(){ // Set the name of the queue return new Queue("hello-queue"); } }
This completes the MQ configuration class and declares a message queue 'hello queue'
(2) Message sending
/** * message sender */ @Service public class SenderDay3 { // Inject Spring to provide a template class to operate RabbitMq @Autowired private AmqpTemplate amqpTemplate; /** * send message */ public void send(String msg){ // Send message to message queue //Parameter 1: identify which message queue to send messages to //Parameter 2: message amqpTemplate.convertAndSend("hello-queue",msg); } }
(3) Message reception
/** * Message Receiver * It generally refers to various components, that is, when our class does not belong to various classifications (not belong to @ Controller, @ Services, etc.), * We can use @ Component to annotate this class */ @Component public class ReceiverDay3 { /** * The method of receiving message adopts the monitoring mechanism of message queue * How message receivers receive messages in RabbitMQ * It uses message queue listeners to listen to message queues, * If there is a change in the message queue, the event of the message listener will be generated, and the method will be triggered to receive the message after the message event is generated. * Use the annotation @ RabbitListener to make our defined method the "emissary" of RabbitMQ. Once the message queue changes, the defined "emissary" will respond * queues Parameter is used to monitor the message queue by name. One or more messages can be monitored at the same time */ @RabbitListener(queues = "hello-queue") public void process(String msg){ System.out.print("Receiver Receive hello-queue News:"+msg); } }
The completed simple message queue has been completed. You can send messages to the message queue, get messages from the message receiver and process your own business according to the message content. You can also view the created message queue through the UI interface of RabbitMQ.