Step 1: Introduce dependencies

Spring-context, Spring-Rabbit, and unit test dependencies

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < the parent > < artifactId > MyStudy < / artifactId > < groupId > org. Example < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < artifactId > the rabbitmq < / artifactId > < dependencies > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - the context < / artifactId > < version > 5.2.9. RELEASE < / version > </dependency> <! -- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit --> <dependency> < the groupId > org. Springframework. It < / groupId > < artifactId > spring - rabbit < / artifactId > < version > 2.2.11. RELEASE < / version > </dependency> <groupId> <groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <! -- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core --> <dependency> <groupId>org.hamcrest</groupId> < artifactId > hamcrest - core < / artifactId > < version > 1.3 < / version > < / dependency > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - test < / artifactId > < version > 5.1.7. RELEASE < / version > </dependency> </dependencies> </project>Copy the code

Write rabbitMQ connection properties:

rabbitmq.properties

The rabbitmq. Host = 127.0.0.1 rabbitmq. Port = 5672 the rabbitmq. Username = cjian rabbitmq. Password = 111111 rabbitmq.virtual-host=vhost_cjianCopy the code

Step 2: Write the producer’s configuration file

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <! - loading configuration file - > < context: the property - placeholder location = "classpath: / rabbitmq. Properties" / > <! Rabbitmq connectionFactory --> <rabbit: connectionFactory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <! <rabbit:admin connection-factory="connectionFactory"/> <! <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/> <! If no switch is bound, it will be automatically created. If no switch is bound, it will be bound to the default switch. The default switch name is Direct, and the routing key is the queue name. -- Simple queue ID: Bean name Name: Queue name Auto-Declare: Automatic Creation durable: Persistent Auto-delete: When the last consumer disconnects from the queue, the queue is automatically deleted --> < Rabbit :queue ID ="spring_queue" name ="spring_queue" auto-declare="true" /> <! -- Switch of fanout type, All queues receive messages --> <rabbit:queue ID =" spring_FANout_queue1 "name =" spring_FANout_queue1" auto-declare="true"/> <rabbit:queue id="spring_fanout_queue2" name = "spring_fanout_queue2" auto-declare="true"/> <! -- Define a FANout switch, --> <rabbit: FANout-exchange ID ="spring_fanout_exchange" name ="spring_fanout_exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="spring_fanout_queue1"></rabbit:binding> <rabbit:binding queue="spring_fanout_queue2"></rabbit:binding> </rabbit:bindings> </rabbit:fanout-exchange> <! -- Direct type --> <rabbit:queue ID ="spring_direct_queue1" name="spring_direct_queue1" auto-delete="true"/> <rabbit:queue id="spring_direct_queue2" name="spring_direct_queue2" auto-delete="true"/> <rabbit:direct-exchange name="spring_direct_exchange" id="spring_direct_exchange" > <rabbit:bindings> <rabbit:binding queue="spring_direct_queue1" key="info"></rabbit:binding> <rabbit:binding queue="spring_direct_queue2" key="error"></rabbit:binding> <rabbit:binding queue="spring_direct_queue2" key="info"></rabbit:binding> <rabbit:binding queue="spring_direct_queue2" key="warning"></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <! -- Topic type --> <rabbit:queue ID ="spring_topic_queue1" name ="spring_topic_queue1" auto-declare="true" /> <rabbit:queue id="spring_topic_queue2" name = "spring_topic_queue2" auto-declare="true"/> <rabbit:queue id="spring_topic_queue3" name = "spring_topic_queue3" auto-declare="true"/> <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange"> <rabbit:bindings> <! -- # : 0 or more words, * : #" queue="spring_topic_queue1"></rabbit:binding> <rabbit:binding pattern="cjian.*" queue="spring_topic_queue2"></rabbit:binding> <rabbit:binding pattern="test.#" queue="spring_topic_queue3"></rabbit:binding> </rabbit:bindings> </rabbit:topic-exchange> </beans>Copy the code

Step 3: Code the consumer

package com.cjian.rabbitmq.spring_rabbit; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @description: * @author: cWX969834 * @time: 2021/1/22 16:36 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml") public class Producer { @Autowired private RabbitTemplate rabbitTemplate; @Test public void testSpringQueue() { rabbitTemplate.convertAndSend("spring_queue", "hello spring rabbitmq"); } @Test public void testSpringFanoutQueue() { rabbitTemplate.convertAndSend("spring_fanout_exchange","", "hello spring rabbitmq_fanout"); } @test public void testSpringTopicQueue() {//1 receive //rabbitTemplate.convertAndSend("spring_topic_exchange","cjian.name.age","hello spring rabbitmq_topic"); / / 1, 2, receiving rabbitTemplate. ConvertAndSend (" spring_topic_exchange ", "cjian. Hello," "hello spring rabbitmq_topic"); }}Copy the code

The producer sends messages simply because the configuration file needs to be configured

Step 4: Write the consumer’s listener class

Here is a simple implementation: in addition to the class name is not the same, other things are similar, not a paste

Unit test class

package com.cjian.rabbitmq.spring_rabbit;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
/**
 * @description:
 * @author: CJ
 * @time: 2021/1/25 10:17
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class Consumer {
    @Test
    public void test(){
        while(true){
 
        }
    }
 
}
Copy the code

Listener class

package com.cjian.rabbitmq.spring_rabbit; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; * @author: CJ * @time: 2021/1/25 9:40 */ public class SpringQueueListener implements MessageListener { @Override public void onMessage(Message Message) {system.out.println (" Simple queue: "new String(message.getBody())); }}Copy the code

Step 5: Write a consumer profile

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <! - loading configuration file - > < context: the property - placeholder location = "classpath: the rabbitmq. Properties" / > <! Rabbitmq connectionFactory --> <rabbit: connectionFactory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <! Register the spring container listener bean--> <! -- a simple type - > < bean id = "springQueueListener" class = "com. Cjian. The rabbitmq. Spring_rabbit. SpringQueueListener" / > <! --fanout--> <bean id = "springFanoutListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringFanoutListener1"/> <bean id  = "springFanoutListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringFanoutListener2"/> <! --direct--> <bean id = "springDirectListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringDirectListener1"/> <bean id  = "springDirectListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringDirectListener2"/> <! --topic--> <bean id = "springTopicListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener1"/> <bean id = "springTopicListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener2"/> <bean id = "springTopicListener3"  class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener3"/> <! --> <rabbit: listen-container Connection-factory ="connectionFactory" auto-declare="true"> <! -- Simple type --> <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/> <! --fanout--> <rabbit:listener ref="springFanoutListener1" queue-names="spring_fanout_queue1"/> <rabbit:listener ref="springFanoutListener2" queue-names="spring_fanout_queue2"/> <! --topic--> <rabbit:listener ref="springTopicListener1" queue-names="spring_topic_queue1"/> <rabbit:listener ref="springTopicListener2" queue-names="spring_topic_queue2"/> <rabbit:listener ref="springTopicListener3" queue-names="spring_topic_queue2"/> <! -- Direct --> <rabbit:listener ref="springDirectListener1" queue-names="spring_direct_queue1"/> <rabbit:listener ref="springDirectListener2" queue-names="spring_direct_queue2"/> </rabbit:listener-container> </beans>Copy the code

Testing:

1. Simple queues

Send a message:

Received a message:

2. The fanout type

Received a message:

3. Direct type

Received a message:

4. The topic type

Received a message: