• 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. }

  • 相关阅读:
    ORA-12560:TNS:协议适配器错误
    vscode在windows环境不能使用终端安装依赖
    130. 如何使 SAP UI5 SmartField 在运行时渲染成超链接的形式并支持跳转
    RAID5的配置流程及模拟硬盘损坏
    亚马逊鲲鹏测评系统:全自动注册下单及留评
    Flask 上下文是什么 ?
    水果店圈子:水果店月收入大概是多少,我想开水果店不知道利润有百分之几
    spring 中的资源文件加载
    AIGC笔记--基于DDPM实现图片生成
    Python入门自学进阶-Web框架——25、DjangoAdmin项目应用-分页与过滤
  • 原文地址:https://blog.csdn.net/m0_62639288/article/details/132902356