当前位置:   article > 正文

RabbitMQ 入门篇之——Spring整合rabbitmq_spring整合rabbitmq的实现 用配置文件的方式,不是springboot的方式

spring整合rabbitmq的实现 用配置文件的方式,不是springboot的方式

目录

第一步:引入依赖

第二步:编写生产者的配置文件 

第三步:编写消费者的代码

第四步:编写消费者的监听器类

单元测试类

监听器类

第五步:编写消费者的配置文件

测试:

1.简单队列

2.fanout类型

3.direct类型

4.topic类型


 

为了后面讲rabbitmq的一些高级特性,这里讲下spring如何整合rabbitmq 

第一步:引入依赖

spring-context、spring-rabbit以及单元测试的相关依赖 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>MyStudy</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>rabbitmq</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-context</artifactId>
  16. <version>5.2.9.RELEASE</version>
  17. </dependency>
  18. <!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
  19. <dependency>
  20. <groupId>org.springframework.amqp</groupId>
  21. <artifactId>spring-rabbit</artifactId>
  22. <version>2.2.11.RELEASE</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>junit</groupId>
  26. <artifactId>junit</artifactId>
  27. <version>4.12</version>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
  30. <dependency>
  31. <groupId>org.hamcrest</groupId>
  32. <artifactId>hamcrest-core</artifactId>
  33. <version>1.3</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-test</artifactId>
  38. <version>5.1.7.RELEASE</version>
  39. </dependency>
  40. </dependencies>
  41. </project>

编写rabbitmq的连接属性:

rabbitmq.properties

  1. rabbitmq.host=127.0.0.1
  2. rabbitmq.port=5672
  3. rabbitmq.username=cjian
  4. rabbitmq.password=111111
  5. rabbitmq.virtual-host=vhost_cjian

 

第二步:编写生产者的配置文件 

  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. <!--定义管理交换机、队列-->
  21. <rabbit:admin connection-factory="connectionFactory"/>
  22. <!--定义rabbitTemple对象-->
  23. <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
  24. <!--定义持久化队列,不存在则自动创建,不绑定交换机则绑定到默认交换机,默认交换机为direct,名字为”“,路由键为队列的名称
  25. auto-declare:是否自动创建
  26. -->
  27. <!--简单队列
  28. id:bean的名称
  29. name:queue的名称
  30. auto-declare:是否自动创建
  31. durable:是否持久化
  32. auto-delete:当最后一个消费者和该队列断开连接后,自动删除队列
  33. -->
  34. <rabbit:queue id="spring_queue" name ="spring_queue" auto-declare="true" />
  35. <!--fanout类型的交换机,所有队列都可获得消息-->
  36. <rabbit:queue id="spring_fanout_queue1" name = "spring_fanout_queue1" auto-declare="true"/>
  37. <rabbit:queue id="spring_fanout_queue2" name = "spring_fanout_queue2" auto-declare="true"/>
  38. <!--定义fanout类型交换机,并绑定队列-->
  39. <rabbit:fanout-exchange id="spring_fanout_exchange" name ="spring_fanout_exchange" auto-declare="true">
  40. <rabbit:bindings>
  41. <rabbit:binding queue="spring_fanout_queue1"></rabbit:binding>
  42. <rabbit:binding queue="spring_fanout_queue2"></rabbit:binding>
  43. </rabbit:bindings>
  44. </rabbit:fanout-exchange>
  45. <!--direct类型-->
  46. <rabbit:queue id="spring_direct_queue1" name="spring_direct_queue1" auto-delete="true"/>
  47. <rabbit:queue id="spring_direct_queue2" name="spring_direct_queue2" auto-delete="true"/>
  48. <rabbit:direct-exchange name="spring_direct_exchange" id="spring_direct_exchange" >
  49. <rabbit:bindings>
  50. <rabbit:binding queue="spring_direct_queue1" key="info"></rabbit:binding>
  51. <rabbit:binding queue="spring_direct_queue2" key="error"></rabbit:binding>
  52. <rabbit:binding queue="spring_direct_queue2" key="info"></rabbit:binding>
  53. <rabbit:binding queue="spring_direct_queue2" key="warning"></rabbit:binding>
  54. </rabbit:bindings>
  55. </rabbit:direct-exchange>
  56. <!--topic类型-->
  57. <rabbit:queue id="spring_topic_queue1" name = "spring_topic_queue1" auto-declare="true" />
  58. <rabbit:queue id="spring_topic_queue2" name = "spring_topic_queue2" auto-declare="true"/>
  59. <rabbit:queue id="spring_topic_queue3" name = "spring_topic_queue3" auto-declare="true"/>
  60. <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange">
  61. <rabbit:bindings>
  62. <!-- #:0或多个单词 ,*:刚好一个单词-->
  63. <rabbit:binding pattern="cjian.#" queue="spring_topic_queue1"></rabbit:binding>
  64. <rabbit:binding pattern="cjian.*" queue="spring_topic_queue2"></rabbit:binding>
  65. <rabbit:binding pattern="test.#" queue="spring_topic_queue3"></rabbit:binding>
  66. </rabbit:bindings>
  67. </rabbit:topic-exchange>
  68. </beans>

第三步:编写消费者的代码

  1. package com.cjian.rabbitmq.spring_rabbit;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. /**
  9. * @description:
  10. * @author: cWX969834
  11. * @time: 2021/1/22 16:36
  12. */
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
  15. public class Producer {
  16. @Autowired
  17. private RabbitTemplate rabbitTemplate;
  18. @Test
  19. public void testSpringQueue() {
  20. rabbitTemplate.convertAndSend("spring_queue", "hello spring rabbitmq");
  21. }
  22. @Test
  23. public void testSpringFanoutQueue() {
  24. rabbitTemplate.convertAndSend("spring_fanout_exchange","", "hello spring rabbitmq_fanout");
  25. }
  26. @Test
  27. public void testSpringTopicQueue() {
  28. //1接收
  29. //rabbitTemplate.convertAndSend("spring_topic_exchange","cjian.name.age","hello spring rabbitmq_topic");
  30. //1、2接收
  31. rabbitTemplate.convertAndSend("spring_topic_exchange","cjian.hello","hello spring rabbitmq_topic");
  32. }
  33. }

生产者发送消息简单,主要就是配置文件需要配置

第四步:编写消费者的监听器类

这里简单实现下:除了类名不一样外,其他都大同小异了,就不一一贴了

单元测试类

  1. package com.cjian.rabbitmq.spring_rabbit;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.test.context.ContextConfiguration;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6. /**
  7. * @description:
  8. * @author: CJ
  9. * @time: 2021/1/25 10:17
  10. */
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
  13. public class Consumer {
  14. @Test
  15. public void test(){
  16. while(true){
  17. }
  18. }
  19. }

监听器类

  1. package com.cjian.rabbitmq.spring_rabbit;
  2. import org.springframework.amqp.core.Message;
  3. import org.springframework.amqp.core.MessageListener;
  4. /**
  5. * @description: 需要实现MessageListener接口,并重写onMessage方法
  6. * @author: CJ
  7. * @time: 2021/1/25 9:40
  8. */
  9. public class SpringQueueListener implements MessageListener {
  10. @Override
  11. public void onMessage(Message message) {
  12. System.out.println("简单队列:"new String(message.getBody()));
  13. }
  14. }

第五步:编写消费者的配置文件

  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. <!--给spring容器注册监听器bean-->
  21. <!--简单类型-->
  22. <bean id = "springQueueListener" class="com.cjian.rabbitmq.spring_rabbit.SpringQueueListener"/>
  23. <!--fanout-->
  24. <bean id = "springFanoutListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringFanoutListener1"/>
  25. <bean id = "springFanoutListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringFanoutListener2"/>
  26. <!--direct-->
  27. <bean id = "springDirectListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringDirectListener1"/>
  28. <bean id = "springDirectListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringDirectListener2"/>
  29. <!--topic-->
  30. <bean id = "springTopicListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener1"/>
  31. <bean id = "springTopicListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener2"/>
  32. <bean id = "springTopicListener3" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener3"/>
  33. <!--绑定监听器与队列的关系-->
  34. <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
  35. <!--简单类型-->
  36. <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
  37. <!--fanout-->
  38. <rabbit:listener ref="springFanoutListener1" queue-names="spring_fanout_queue1"/>
  39. <rabbit:listener ref="springFanoutListener2" queue-names="spring_fanout_queue2"/>
  40. <!--topic-->
  41. <rabbit:listener ref="springTopicListener1" queue-names="spring_topic_queue1"/>
  42. <rabbit:listener ref="springTopicListener2" queue-names="spring_topic_queue2"/>
  43. <rabbit:listener ref="springTopicListener3" queue-names="spring_topic_queue2"/>
  44. <!--direct 类型-->
  45. <rabbit:listener ref="springDirectListener1" queue-names="spring_direct_queue1"/>
  46. <rabbit:listener ref="springDirectListener2" queue-names="spring_direct_queue2"/>
  47. </rabbit:listener-container>
  48. </beans>

测试:

1.简单队列

发送消息:

收到消息:

2.fanout类型

收到消息:

3.direct类型

收到消息:

4.topic类型

收到消息:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/139107
推荐阅读
相关标签