• Spring整合RabbitMQ


    一、步骤

    生产者

    ① 创建生产者工程
    ② 添加依赖
    ③ 配置整合
    ④ 编写代码发送消息

    消费者

    ① 创建消费者工程
    ② 添加依赖
    ③ 配置整合
    ④ 编写消息监听器

    二、代码

    生产者工程

    1.在生产者工程和消费者工程中都导入如下依赖

    1. org.springframework
    2. spring-context
    3. 5.1.7.RELEASE
    4. org.springframework.amqp
    5. spring-rabbit
    6. 2.1.8.RELEASE
    7. junit
    8. junit
    9. 4.12
    10. org.springframework
    11. spring-test
    12. 5.1.7.RELEASE
    13. org.apache.maven.plugins
    14. maven-compiler-plugin
    15. 3.8.0
    16. 1.8
    17. 1.8

     2.生产者和消费者 导入配置文件   rabbitmq.properties

    1. rabbitmq.host=43.143.246.208
    2. rabbitmq.port=5672
    3. rabbitmq.username=root
    4. rabbitmq.password=root
    5. rabbitmq.virtual-host=/itcast

    3.生产者核心配置文件  spring-rabbitmq-producer.xml

    1. "1.0" encoding="UTF-8"?>
    2. "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. "classpath:rabbitmq.properties"/>
    13. "connectionFactory" host="${rabbitmq.host}"
    14. port="${rabbitmq.port}"
    15. username="${rabbitmq.username}"
    16. password="${rabbitmq.password}"
    17. virtual-host="${rabbitmq.virtual-host}"/>
    18. "connectionFactory"/>
    19. "spring_queue" name="spring_queue" auto-declare="true"/>
    20. "spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>
    21. "spring_fanout_queue_2" name="spring_fanout_queue_2" auto-declare="true"/>
    22. "spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true">
    23. "spring_fanout_queue_1"/>
    24. "spring_fanout_queue_2"/>
    25. "spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/>
    26. "spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/>
    27. "spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/>
    28. "spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
    29. "root.*" queue="spring_topic_queue_star"/>
    30. "root.#" queue="spring_topic_queue_well"/>
    31. "itcast.#" queue="spring_topic_queue_well2"/>
    32. "rabbitTemplate" connection-factory="connectionFactory"/>

    4.测试类 ProducerTest

    1. @RunWith(SpringJUnit4ClassRunner.class)
    2. @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
    3. public class ProducerTest {
    4. //1.注入 RabbitTemplate
    5. @Autowired
    6. private RabbitTemplate rabbitTemplate;
    7. @Test
    8. public void testHelloWorld(){
    9. //2.发送消息
    10. rabbitTemplate.convertAndSend("spring_queue","hello world spring....");
    11. }
    12. /**
    13. * 发送fanout消息
    14. */
    15. @Test
    16. public void testFanout(){
    17. //2.发送消息
    18. rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout....");
    19. }
    20. /**
    21. * 发送topic消息
    22. */
    23. @Test
    24. public void testTopic(){
    25. //2.发送消息
    26. rabbitTemplate.convertAndSend("spring_topic_exchange","root.hehe.haha","spring topic....");
    27. }
    28. }

    5.运行结果 

     消费者工程

    1、2同上述1、2

    3、消费者核心配置文件

    1. "1.0" encoding="UTF-8"?>
    2. "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. "classpath:rabbitmq.properties"/>
    13. "connectionFactory" host="${rabbitmq.host}"
    14. port="${rabbitmq.port}"
    15. username="${rabbitmq.username}"
    16. password="${rabbitmq.password}"
    17. virtual-host="${rabbitmq.virtual-host}"/>
    18. "springQueueListener" class="com.rabbitmq.listener.SpringQueueListener"/>
    19. "connectionFactory" auto-declare="true">
    20. "springQueueListener" queue-names="spring_queue"/>

    4.类 SpringQueueListener

    1. public class SpringQueueListener implements MessageListener {
    2. @Override
    3. public void onMessage(Message message) {
    4. //打印消息
    5. System.out.println(new String(message.getBody()));
    6. }
    7. }

    5.测试

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

  • 相关阅读:
    零基础真的可以学习Python吗?答案是:可以!
    Spring修炼之旅(3)自动装配与注解开发
    Maven下载源码出现:Cannot download sources Sources not found for org.springframwork...
    C++ 命名空间(namespace)
    ASUS华硕ZenBook灵耀X逍遥UXF3000E_UX363EA原装出厂预装Win11系统工厂模式安装包
    【C++】C++入门
    三维模型3DTile格式轻量化的纹理压缩和质量关系分析
    SpringBoot集成Redis Cluster集群(附带Linux部署Redis Cluster高可用集群)
    Apache Doris 数据建模之 Aggregate Key 模型
    docker Compose 部署springboot+vue前端端分离项目
  • 原文地址:https://blog.csdn.net/m0_62639288/article/details/132902356