Spring整合RabbitMQ

2023-09-15 16:45:59

一、步骤

生产者

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

消费者

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

二、代码

生产者工程

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

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>2.1.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

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

rabbitmq.host=43.143.246.208
rabbitmq.port=5672
rabbitmq.username=root
rabbitmq.password=root
rabbitmq.virtual-host=/itcast

3.生产者核心配置文件  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">
    <!--加载配置文件-->
    <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}"/>
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--定义持久化队列,不存在则自动创建;不绑定到交换机则绑定到默认交换机
    默认交换机类型为direct,名字为:"",路由键为队列的名称
    -->
    <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <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>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~通配符;*匹配一个单词,#匹配多个单词 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <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"/>

    <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding pattern="root.*" queue="spring_topic_queue_star"/>
            <rabbit:binding pattern="root.#" queue="spring_topic_queue_well"/>
            <rabbit:binding pattern="itcast.#" queue="spring_topic_queue_well2"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

4.测试类 ProducerTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {
    //1.注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testHelloWorld(){
        //2.发送消息
        rabbitTemplate.convertAndSend("spring_queue","hello world spring....");
    }
    /**
     * 发送fanout消息
     */
    @Test
    public void testFanout(){
        //2.发送消息

        rabbitTemplate.convertAndSend("spring_fanout_exchange","","spring fanout....");
    }


    /**
     * 发送topic消息
     */
    @Test
    public void testTopic(){
        //2.发送消息

        rabbitTemplate.convertAndSend("spring_topic_exchange","root.hehe.haha","spring topic....");
    }
}

5.运行结果 

 消费者工程

1、2同上述1、2

3、消费者核心配置文件

<?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}"/>
    <!-- 定义SpringQueueListener -->
    <bean id="springQueueListener" class="com.rabbitmq.listener.SpringQueueListener"/>
<!--    <bean id="fanoutListener1" class="com.rabbitmq.listener.FanoutListener1"/>-->
<!--    <bean id="fanoutListener2" class="com.rabbitmq.listener.FanoutListener2"/>-->
<!--    <bean id="topicListenerStar" class="com.rabbitmq.listener.TopicListenerStar"/>-->
<!--    <bean id="topicListenerWell" class="com.rabbitmq.listener.TopicListenerWell"/>-->
<!--    <bean id="topicListenerWell2" class="com.rabbitmq.listener.TopicListenerWell2"/>-->

    <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
        <!-- 定义SpringQueueListener 监听哪个队列-->
        <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-container>
</beans>

4.类 SpringQueueListener

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

5.测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-comsumer.xml")
public class ConsumerTest {
    @Test
    public void test1(){
        boolean flag = true;
        while (true){

        }
    }

}

更多推荐

yamot:一款功能强大的基于Web的服务器安全监控工具

关于yamotyamot是一款功能强大的基于Web的服务器安全监控工具,专为只有少量服务器的小型环境构建。yamot只会占用非常少的资源,并且几乎可以在任何设备上运行。该工具适用于Linux或BSD,当前版本暂不支持Windows平台。比如说,广大研究人员可以使用yamot来监控在家运行的RaspberryPi服务器。

leetcode21合并两个有序链表

题目:将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。示例1:输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,3,4,4]示例2:输入:l1=[],l2=[]输出:[]示例3:输入:l1=[],l2=[0]输出:[0]提示:两个链表的节点数目范围是[0,5

web端程序访问过慢时如何判断问题

当Web端程序访问过慢时,可以按照以下步骤进行问题判断和排查:1.确认问题:确认Web端程序访问真的过慢,可以通过以下方法进行测试:在不同浏览器和设备上测试访问速度,以排除个别设备或浏览器的问题。在不同时间段进行测试,以排除网络高峰期的影响。确认网络连接是否稳定,可以尝试在多个网络环境下进行测试。2.检查服务器性能:如

3D医学影像PACS系统源代码

一、系统概述3D医学影像PACS系统,它集影像存储服务器、影像诊断工作站及RIS报告系统于一身,主要有图像处理模块、影像数据管理模块、RIS报告模块、光盘存档模块、DICOM通讯模块、胶片打印输出等模块组成,具有完善的影像数据库管理功能,强大的图像后处理功能,提高了临床诊断准确率。二、三维影像重建支持三维影像处理功能;

代码签名:保护你的软件的安全性和完整性

代码签名是一种数字签名技术,用于保护软件的完整性和身份。它通过使用一个密钥对软件代码进行签名,确保代码在下载和安装过程中没有被篡改。代码签名证书是一种数字证书,用于证明代码签名者的身份和代码的完整性。以下是代码签名证书如何保护您的软件的详细说明:1,确保软件的完整性:代码签名证书可以确保您的软件在下载和安装过程中没有被

【cmake开发(5)】cmake 设置常规变量、环境变量、内置变量;cmake 带参数编译和 -D 选项; c++源码通过-D 选项的宏定义进行条件编译

文章目录一、CMake变量的定义1.1定义常规变量1.2打印变量1.3环境变量1.4持久缓存1.5持久缓存原理1.6内置变量二、带参数编译2.1我们可以看到了-D选项,一般配合option命令2.2c++源码通过-D选项的宏定义进行条件编译参考在【cmake开发(3)】中,我们设置了makeinstall安装目录。这就

「网页开发|后端开发|Flask」08 python接口开发快速入门:技术选型&写一个HelloWorld接口

本文主要介绍为网站搭建后端时的技术选型考虑,以及通过写一个简单的HelloWorld接口快速了解前端和后端交互的流程。文章目录本系列前文传送门一、场景说明二、后端语言技术选型三、后端框架技术选型Django特点Flask特点FastAPI特点Tarnado特点四、用Flask先来个最简单的HelloWorld五、在前端

静态代理和动态代理

一、静态代理代理模式(ProxyPattern)是一种结构型设计模式,它的概念很简单,它通过创建一个代理对象来控制对原始对象的访问。代理模式主要涉及两个角色:代理角色和真实角色。代理类负责代理真实类,为真实类提供控制访问的功能,真实类则完成具体的业务逻辑。这样,当我们不方便或者不能直接访问真实对象时,可以通过代理对象来

Mysql----锁

文章目录锁概述全局锁全局锁概述全局锁操作表级锁表级锁表锁表级锁元数据锁表级锁意向锁行级锁行级锁行锁行级锁间隙锁&临键锁锁概述是什么是计算机协调多个进程或线程并发访问某一资源的机制。意义在数据库中,数据是一种供许多用户共享的资源。如何保证数据并发访问的一致性、有效性是所有数据库必须解决的一个问题,锁冲突也是影响数据库并发

大数据之-Flink学习笔记

FlinkApacheFlink—数据流上的有状态计算。ApacheFlink是一个框架和分布式处理引擎,用于对无界和有界数据流进行有状态计算处理。任何类型的数据都以事件流的形式生成。信用卡交易、传感器测量、机器日志或网站或移动应用程序2上的用户交互,所有这些数据都以流的形式生成。数据可以作为无界或有界流进行处理。无界

U盘格式化后数据能恢复吗?详细答案看这里!

“U盘格式化之后数据还可以恢复吗?这真的困扰了我好久!之前由于u盘中病毒,不得已将它格式化了,但是我还有好多视频、图片都保存在里面,这该怎么办呢?”小小的u盘,可是给我们带来了很多的便利的。在互联网时代,u盘的作用也是越来越大。但不否认的是,在使用u盘的过程中,我们也会遇到各种各样的问题,有时候我们不得已需要将u盘格式

热文推荐