Spring整合RabbitMQ详细解读

Spring整合RabbitMQ的步骤

代码示例骨架

POM.xml文件中的依赖包

生产者模块代码实现

消费者模块代码实现

运行结果


Spring整合RabbitMQ的步骤

使用Spring进行整合,可以使代码变得更加简单,逻辑更加清晰

代码示例骨架

在一个工程中创建了两个模块,一个是生产者一个是消费者,使用Spring整合Rabbit主要使用的就是配置文件XML的形式。

POM.xml文件中的依赖包

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-context</artifactId>
  4. <version>5.1.9.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.amqp</groupId>
  8. <artifactId>spring-rabbit</artifactId>
  9. <version>2.2.22.RELEASE</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>4.12</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework</groupId>
  19. <artifactId>spring-test</artifactId>
  20. <version>5.1.8.RELEASE</version>
  21. </dependency>

生产者模块代码实现

(1)首先我们要配置其连接工厂的信息,使用rabbitmq.properties配置

  1. rabbitmq.host=localhost
  2. rabbitmq.port=5672
  3. rabbitmq.username=Harmony
  4. rabbitmq.password=888888
  5. rabbitmq.virtual-host=/HarmonyOS

(2)spring-rabbitmq-producer.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/rabbit
  11. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
  12. <!--
  13. 关于spring与rabbit之前的整合xml参数配置
  14. id: bean的名称
  15. name: queue的名称
  16. auto-declare: 自动创建
  17. auto-delete: 自动删除,最后一个消费者与该队列断开后,自动删除队列
  18. exclusive: 是否独占
  19. durable: 是否持久化
  20. -->
  21. <!-- 1.加载配置文件-->
  22. <context:property-placeholder location="classpath:rabbitmq.properties"/>
  23. <!-- 2.定义rabbitmq connectionFactory -->
  24. <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
  25. port="${rabbitmq.port}"
  26. username="${rabbitmq.username}"
  27. password="${rabbitmq.password}"
  28. virtual-host="${rabbitmq.virtual-host}"/>
  29. <!-- 3.定义管理交换机、队列-->
  30. <rabbit:admin connection-factory="connectionFactory"/>
  31. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~简单模式~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  32. <!-- 4.1 简单模式 定义持久化队列,不存在则自动创建 -->
  33. <!-- 不绑定到交换机则绑定到默认交换机,默认交换机类型为direct,名字为:"",路由键为队列的名称 -->
  34. <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>
  35. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  36. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  37. <!-- 4.2 定义广播交换机中的持久化队列,不存在则自动创建-->
  38. <rabbit:queue id="spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>
  39. <rabbit:queue id="spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/>
  40. <!-- 定义广播类型交换机;并绑定上述两个队列 -->
  41. <rabbit:fanout-exchange id="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
  42. <rabbit:bindings>
  43. <rabbit:binding queue="spring_fanout_queue_1"/>
  44. <rabbit:binding queue="spring_fanout_queue_2"/>
  45. </rabbit:bindings>
  46. </rabbit:fanout-exchange>
  47. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  48. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 路由模式 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  49. <!-- 4.3 定义路由交换机的队列-->
  50. <rabbit:queue id="spring_routing_queue1" name="spring_routing_queue1" auto-declare="true"/>
  51. <rabbit:queue id="spring_routing_queue2" name="spring_routing_queue2" auto-declare="true"/>
  52. <!-- 定义路由类型交换机;并绑定上述两个队列 -->
  53. <rabbit:direct-exchange id="spring_routing_exchange" name="spring_routing_exchange" auto-declare="true">
  54. <rabbit:bindings>
  55. <rabbit:binding queue="spring_routing_queue1" key="route1"/>
  56. <rabbit:binding queue="spring_routing_queue2" key="route2"/>
  57. </rabbit:bindings>
  58. </rabbit:direct-exchange>
  59. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  60. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 通配符模式 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  61. <!-- 4.4 通配符 定义topics交换机的3个队列-->
  62. <rabbit:queue id="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
  63. <rabbit:queue id="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
  64. <rabbit:queue id="spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/>
  65. <!-- 定义通配符类型交换机;并绑定上述3个队列 -->
  66. <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
  67. <rabbit:bindings>
  68. <rabbit:binding pattern="heima.*" queue="spring_topic_queue_star"/>
  69. <rabbit:binding pattern="heima.#" queue="spring_topic_queue_well"/>
  70. <rabbit:binding pattern="itcast.#" queue="spring_topic_queue_well2"/>
  71. </rabbit:bindings>
  72. </rabbit:topic-exchange>
  73. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  74. <!-- 5.定义rabbitTemplate对象操作可以在代码中方便发送消息-->
  75. <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
  76. </beans>

(3)生产者各种模式发送消息,我们在测试模块中发送

  1. /**
  2. * 使用了Spring整合rabbitmq:
  3. * 使用XML配置约定好交换机与队列的绑定方式
  4. * 注入RabbitTemplate
  5. * 使用其convertAndSend()方法发送消息
  6. */
  7. @RunWith(SpringJUnit4ClassRunner.class)
  8. @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
  9. public class ProductTest {
  10. // spring提供的rabbit模板对象
  11. @Autowired
  12. private RabbitTemplate rabbitTemplate;
  13. // 简单模式
  14. @Test
  15. public void testHelloWorld() {
  16. rabbitTemplate.convertAndSend("spring_queue","Hello_World");
  17. }
  18. // 发送fanout消息 广播
  19. @Test
  20. public void testFanout() {
  21. rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout...");
  22. }
  23. // 发送routing
  24. @Test
  25. public void testRouting() {
  26. rabbitTemplate.convertAndSend("spring_routing_exchange","route2","spring route1...");
  27. }
  28. // 发送topics消息
  29. @Test
  30. public void testTopics() {
  31. rabbitTemplate.convertAndSend(
  32. "spring_topic_exchange",
  33. "heima.he.ha",
  34. "spring topic...");
  35. }
  36. }

Spring 提供 RabbitTemplate 简化发送消息 API

简单使用只要注入 RabbitTemplate对象,用其调用convertAndSend()方法。

消费者模块代码实现

(1)模块间的配置文件不能共用,所以也要配置rabbitmq.properties

  1. rabbitmq.host=localhost
  2. rabbitmq.port=5672
  3. rabbitmq.username=Harmony
  4. rabbitmq.password=888888
  5. rabbitmq.virtual-host=/HarmonyOS

(2) spring-rabbitmq-consumer.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. https://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/rabbit
  11. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
  12. <!--加载配置文件-->
  13. <context:property-placeholder location="classpath:rabbitmq.properties"/>
  14. <!-- 定义rabbitmq connectionFactory -->
  15. <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
  16. port="${rabbitmq.port}"
  17. username="${rabbitmq.username}"
  18. password="${rabbitmq.password}"
  19. virtual-host="${rabbitmq.virtual-host}"/>
  20. <bean id="springQueueListener" class="com.Harmony.rabbitmq.listener.SpringQueueListener"/>
  21. <bean id="fanoutListener1" class="com.Harmony.rabbitmq.listener.FanoutListener1"/>
  22. <bean id="fanoutListener2" class="com.Harmony.rabbitmq.listener.FanoutListener2"/>
  23. <bean id="topicListenerStar" class="com.Harmony.rabbitmq.listener.TopicListenerStar"/>
  24. <bean id="topicListenerWell" class="com.Harmony.rabbitmq.listener.TopicListenerWell"/>
  25. <bean id="topicListenerWell2" class="com.Harmony.rabbitmq.listener.TopicListenerWell2"/>
  26. <bean id="routeListener1" class="com.Harmony.rabbitmq.listener.routeListener1"/>
  27. <bean id="routeListener2" class="com.Harmony.rabbitmq.listener.routeListener2"/>
  28. <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
  29. <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
  30. <rabbit:listener ref="fanoutListener1" queue-names="spring_fanout_queue_1"/>
  31. <rabbit:listener ref="fanoutListener2" queue-names="spring_fanout_queue_2"/>
  32. <rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/>
  33. <rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/>
  34. <rabbit:listener ref="topicListenerWell2" queue-names="spring_topic_queue_well2"/>
  35. <rabbit:listener ref="routeListener1" queue-names="spring_routing_queue1"/>
  36. <rabbit:listener ref="routeListener2" queue-names="spring_routing_queue2"/>
  37. </rabbit:listener-container>
  38. </beans>

(3)实现MessageListener接口,重写onMessage()方法

  1. public class FanoutListener1 implements MessageListener {
  2. // 回调方法
  3. @Override
  4. public void onMessage(Message message) {
  5. //打印信息
  6. System.out.println(new String(message.getBody()));
  7. }
  8. }

创建实现类,实现MessageListener接口,重写onMessage()方法,下述的几个类的写发都和上面一样。

 

 (4)开启测试

在控制台打印内容我们已经在(3)中使用onMessage()方法处理好了,现在只要将这些不同类型的消费者开启监听即可,即可以随意一点,写一个死循环一直监听,直至不需要了终止程序即可!

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
  3. public class ConsumerTest {
  4. @Test
  5. public void Test() {
  6. boolean flag = true;
  7. while(flag) {
  8. }
  9. }
  10. }

运行结果

依次运行生产者中的测试,可以发现有相应的消息被加入了队列中(此时还未启动消费者!

执行消费者的测试,取出消息,如下,

显然开启后,消息就被全部取出了,Consumers那一列都为1,表示每一个队列都有一个消费者!

 

控制台打印结果

文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树首页概览120777 人正在系统学习中
posted @ 2023-06-20 14:12  离人怎挽_wdj  阅读(113)  评论(0编辑  收藏  举报