Spring整合RabbitMQ详细解读
Spring整合RabbitMQ的步骤
使用Spring进行整合,可以使代码变得更加简单,逻辑更加清晰
代码示例骨架
在一个工程中创建了两个模块,一个是生产者一个是消费者,使用Spring整合Rabbit主要使用的就是配置文件XML的形式。
POM.xml文件中的依赖包
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.1.9.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.amqp</groupId>
- <artifactId>spring-rabbit</artifactId>
- <version>2.2.22.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>5.1.8.RELEASE</version>
- </dependency>
生产者模块代码实现
(1)首先我们要配置其连接工厂的信息,使用rabbitmq.properties配置
- rabbitmq.host=localhost
- rabbitmq.port=5672
- rabbitmq.username=Harmony
- rabbitmq.password=888888
- rabbitmq.virtual-host=/HarmonyOS
(2)spring-rabbitmq-producer.xml
- <?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">
- <!--
- 关于spring与rabbit之前的整合xml参数配置
- id: bean的名称
- name: queue的名称
- auto-declare: 自动创建
- auto-delete: 自动删除,最后一个消费者与该队列断开后,自动删除队列
- exclusive: 是否独占
- durable: 是否持久化
- -->
-
- <!-- 1.加载配置文件-->
- <context:property-placeholder location="classpath:rabbitmq.properties"/>
-
- <!-- 2.定义rabbitmq connectionFactory -->
- <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
- port="${rabbitmq.port}"
- username="${rabbitmq.username}"
- password="${rabbitmq.password}"
- virtual-host="${rabbitmq.virtual-host}"/>
-
- <!-- 3.定义管理交换机、队列-->
- <rabbit:admin connection-factory="connectionFactory"/>
-
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~简单模式~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
- <!-- 4.1 简单模式 定义持久化队列,不存在则自动创建 -->
- <!-- 不绑定到交换机则绑定到默认交换机,默认交换机类型为direct,名字为:"",路由键为队列的名称 -->
- <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
-
-
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
- <!-- 4.2 定义广播交换机中的持久化队列,不存在则自动创建-->
- <rabbit:queue id="spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>
- <rabbit:queue id="spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/>
-
- <!-- 定义广播类型交换机;并绑定上述两个队列 -->
- <rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
- <rabbit:bindings>
- <rabbit:binding queue="spring_fanout_queue_1"/>
- <rabbit:binding queue="spring_fanout_queue_2"/>
- </rabbit:bindings>
- </rabbit:fanout-exchange>
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
-
-
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 路由模式 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
- <!-- 4.3 定义路由交换机的队列-->
- <rabbit:queue id="spring_routing_queue1" name="spring_routing_queue1" auto-declare="true"/>
- <rabbit:queue id="spring_routing_queue2" name="spring_routing_queue2" auto-declare="true"/>
-
- <!-- 定义路由类型交换机;并绑定上述两个队列 -->
- <rabbit:direct-exchange id="spring_routing_exchange" name="spring_routing_exchange" auto-declare="true">
- <rabbit:bindings>
- <rabbit:binding queue="spring_routing_queue1" key="route1"/>
- <rabbit:binding queue="spring_routing_queue2" key="route2"/>
- </rabbit:bindings>
- </rabbit:direct-exchange>
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
-
-
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 通配符模式 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
- <!-- 4.4 通配符 定义topics交换机的3个队列-->
- <rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
- <rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
- <rabbit:queue id="spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/>
-
- <!-- 定义通配符类型交换机;并绑定上述3个队列 -->
- <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
- <rabbit:bindings>
- <rabbit:binding pattern="heima.*" queue="spring_topic_queue_star"/>
- <rabbit:binding pattern="heima.#" queue="spring_topic_queue_well"/>
- <rabbit:binding pattern="itcast.#" queue="spring_topic_queue_well2"/>
- </rabbit:bindings>
- </rabbit:topic-exchange>
- <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
-
- <!-- 5.定义rabbitTemplate对象操作可以在代码中方便发送消息-->
- <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
- </beans>
(3)生产者各种模式发送消息,我们在测试模块中发送
- /**
- * 使用了Spring整合rabbitmq:
- * 使用XML配置约定好交换机与队列的绑定方式
- * 注入RabbitTemplate
- * 使用其convertAndSend()方法发送消息
- */
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
- public class ProductTest {
-
- // spring提供的rabbit模板对象
- @Autowired
- private RabbitTemplate rabbitTemplate;
-
- // 简单模式
- @Test
- public void testHelloWorld() {
- rabbitTemplate.convertAndSend("spring_queue","Hello_World");
- }
-
- // 发送fanout消息 广播
- @Test
- public void testFanout() {
- rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout...");
- }
-
- // 发送routing
- @Test
- public void testRouting() {
- rabbitTemplate.convertAndSend("spring_routing_exchange","route2","spring route1...");
- }
-
- // 发送topics消息
- @Test
- public void testTopics() {
- rabbitTemplate.convertAndSend(
- "spring_topic_exchange",
- "heima.he.ha",
- "spring topic...");
- }
- }
Spring 提供 RabbitTemplate 简化发送消息 API
简单使用只要注入 RabbitTemplate对象,用其调用convertAndSend()方法。
消费者模块代码实现
(1)模块间的配置文件不能共用,所以也要配置rabbitmq.properties
- rabbitmq.host=localhost
- rabbitmq.port=5672
- rabbitmq.username=Harmony
- rabbitmq.password=888888
- rabbitmq.virtual-host=/HarmonyOS
(2) spring-rabbitmq-consumer.xml
- <?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">
- <!--加载配置文件-->
- <context:property-placeholder location="classpath:rabbitmq.properties"/>
-
- <!-- 定义rabbitmq connectionFactory -->
- <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
- port="${rabbitmq.port}"
- username="${rabbitmq.username}"
- password="${rabbitmq.password}"
- virtual-host="${rabbitmq.virtual-host}"/>
-
- <bean id="springQueueListener" class="com.Harmony.rabbitmq.listener.SpringQueueListener"/>
- <bean id="fanoutListener1" class="com.Harmony.rabbitmq.listener.FanoutListener1"/>
- <bean id="fanoutListener2" class="com.Harmony.rabbitmq.listener.FanoutListener2"/>
- <bean id="topicListenerStar" class="com.Harmony.rabbitmq.listener.TopicListenerStar"/>
- <bean id="topicListenerWell" class="com.Harmony.rabbitmq.listener.TopicListenerWell"/>
- <bean id="topicListenerWell2" class="com.Harmony.rabbitmq.listener.TopicListenerWell2"/>
- <bean id="routeListener1" class="com.Harmony.rabbitmq.listener.routeListener1"/>
- <bean id="routeListener2" class="com.Harmony.rabbitmq.listener.routeListener2"/>
-
- <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
- <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
- <rabbit:listener ref="fanoutListener1" queue-names="spring_fanout_queue_1"/>
- <rabbit:listener ref="fanoutListener2" queue-names="spring_fanout_queue_2"/>
- <rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/>
- <rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/>
- <rabbit:listener ref="topicListenerWell2" queue-names="spring_topic_queue_well2"/>
- <rabbit:listener ref="routeListener1" queue-names="spring_routing_queue1"/>
- <rabbit:listener ref="routeListener2" queue-names="spring_routing_queue2"/>
- </rabbit:listener-container>
- </beans>
(3)实现MessageListener接口,重写onMessage()方法
- public class FanoutListener1 implements MessageListener {
-
- // 回调方法
- @Override
- public void onMessage(Message message) {
- //打印信息
- System.out.println(new String(message.getBody()));
- }
- }
创建实现类,实现MessageListener接口,重写onMessage()方法,下述的几个类的写发都和上面一样。
(4)开启测试
在控制台打印内容我们已经在(3)中使用onMessage()方法处理好了,现在只要将这些不同类型的消费者开启监听即可,即可以随意一点,写一个死循环一直监听,直至不需要了终止程序即可!
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
- public class ConsumerTest {
- @Test
- public void Test() {
- boolean flag = true;
- while(flag) {
- }
- }
- }
运行结果
依次运行生产者中的测试,可以发现有相应的消息被加入了队列中(此时还未启动消费者!)
执行消费者的测试,取出消息,如下,
显然开启后,消息就被全部取出了,Consumers那一列都为1,表示每一个队列都有一个消费者!
控制台打印结果
I can feel you forgetting me。。 有一种默契叫做我不理你,你就不理我