Spring整合RabbitMQ(包含生产者和消费者)

  1. 生产者

  2. 创建一个MAVEN项目spring-exchange-producer作为消息队列的生产者

  3. 导入相关的依赖坐标

     <dependencies>
         <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
         <dependency>
             <groupId>com.rabbitmq</groupId>
             <artifactId>amqp-client</artifactId>
             <version>5.6.0</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
         <dependency>
             <groupId>org.springframework.amqp</groupId>
             <artifactId>spring-rabbit</artifactId>
             <version>2.1.4.RELEASE</version>
         </dependency>
    </dependencies>
    
  4. 创建spring配置文件applicationContext.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: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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    
        <!--设置连接工厂,配置基本参数-->
        <rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="guest" password="guest" virtual-host="/test"></rabbit:connection-factory>
    
        <!--
            fanout-exchange,direct-exchange,topic-exchange
            声明一个名为topicExchange的topic交换机,如果该交换机不存在,则自动创建
        -->
        <rabbit:topic-exchange name="topicExchange" auto-declare="true"></rabbit:topic-exchange>
    
        <!-- spring为我们分装了RabbitTemplate对象来简化生产者发送数据的过程,对常用的方法进行封装 -->
        <rabbit:template id="template" connection-factory="connectionFactory" exchange="topicExchange"></rabbit:template>
    
        <!-- 在生产者中配置template对象,用于发送数据 -->
        <bean id="newsProducer" class="com.kangswx.rabbitmq.exchange.NewsProducer">
            <property name="rabbitTemplate" ref="template"/>
        </bean>
    
        <!-- 所有产生的数据在rabbit可视化控制台中展示 -->
        <rabbit:admin connection-factory="connectionFactory"/>
    </beans>
    
  5. 创建需要传递的实体类(必须要实现序列化接口)

    package com.kangswx.rabbitmq.domain;
    
    import java.io.Serializable;
    import java.util.Date;
    
    public class News implements Serializable {
    
        private String source;
        private String title;
        private Date createTime;
        private String content;
    
        public News() {
        }
    
        public News(String source, String title, Date createTime, String content) {
            this.source = source;
            this.title = title;
            this.createTime = createTime;
            this.content = content;
        }
    
        public String getSource() {
            return source;
        }
    
        public void setSource(String source) {
            this.source = source;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
    }
    
  6. 创建生产者类

    package com.kangswx.rabbitmq.exchange;
    
    import com.kangswx.rabbitmq.domain.News;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.util.Date;
    
    public class NewsProducer {
    
        private RabbitTemplate rabbitTemplate = null;
    
        public void sendNews(String routingKey, News news){
            //convertAndSend用于向exchange发送数据
            //第一个参数是routingKey,第二个参数是传送的对象,可以是字符串,byte数组或者任何实现了序列化接口的对象
            rabbitTemplate.convertAndSend(routingKey, news);
            System.out.println("新闻发送成功");
        }
    
        public static void main(String[] args) {
            //手动初始化Spring的IOC容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            //IOC容器中获取对象
            NewsProducer newsProducer = (NewsProducer) ctx.getBean("newsProducer");
    
            newsProducer.sendNews("us.20190101", new News("新华社", "特朗普又又又退群了", new Date(), "国际新闻内容"));
            newsProducer.sendNews("china.20190101", new News("凤凰TV", "XXX企业荣登世界500强", new Date(), "国内新闻内容"));
        }
    
        public RabbitTemplate getRabbitTemplate() {
            return rabbitTemplate;
        }
    
        public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
            this.rabbitTemplate = rabbitTemplate;
        }
    
    }
    
  7. 消费者

  8. 创建一个MAVEN项目spring-exchange-consumer作为消息队列的消费者

  9. 导入相关的依赖坐标(与生产者一致)

    <dependencies>
            <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
            <dependency>
                <groupId>com.rabbitmq</groupId>
                <artifactId>amqp-client</artifactId>
                <version>5.6.0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
            <dependency>
                <groupId>org.springframework.amqp</groupId>
                <artifactId>spring-rabbit</artifactId>
                <version>2.1.4.RELEASE</version>
            </dependency>
        </dependencies>
    
  10. 创建spring配置文件applicationContext.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: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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    
        <!--设置连接工厂,配置基本参数-->
        <rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="guest" password="guest" virtual-host="/test"></rabbit:connection-factory>
    
        <!-- 所有产生的数据在rabbit可视化控制台中展示 -->
        <rabbit:admin connection-factory="connectionFactory"/>
    
        <!-- 创建队列 -->
        <rabbit:queue name="topicQueue" auto-declare="true" auto-delete="false" durable="false" exclusive="false"></rabbit:queue>
    
        <!-- 交换机与队列绑定,并指定筛选条件 -->
        <rabbit:topic-exchange name="topicExchange" auto-declare="true">
            <rabbit:bindings>
                <rabbit:binding queue="topicQueue" pattern="us.*"></rabbit:binding>
            </rabbit:bindings>
        </rabbit:topic-exchange>
    
        <!-- 启动消费者后,Spring底层自动监听对应的topicQueue数据,一旦有新的消息传进来,
        自动传入到consumer的recv的News参数中,之后程序对news做进一步的处理-->
        <rabbit:listener-container connection-factory="connectionFactory">
            <rabbit:listener ref="consumer" method="recv" queue-names="topicQueue"/>
        </rabbit:listener-container>
    
        <!-- 消费者类 -->
        <bean name="consumer" class="com.kangswx.rabbitmq.consumer.NewsConsumer"></bean>
    </beans>
    
  11. 创建消息实体类(实体类的包名与类名必须与生产者一致,且必须实现序列化接口)

    package com.kangswx.rabbitmq.domain;
    
    import java.io.Serializable;
    import java.util.Date;
    
    public class News implements Serializable {
    
        private String source;
        private String title;
        private Date createTime;
        private String content;
    
        public News() {
        }
    
        public News(String source, String title, Date createTime, String content) {
            this.source = source;
            this.title = title;
            this.createTime = createTime;
            this.content = content;
        }
    
        public String getSource() {
            return source;
        }
    
        public void setSource(String source) {
            this.source = source;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public Date getCreateTime() {
            return createTime;
        }
    
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
    }
    
  12. 创建消费者类

    package com.kangswx.rabbitmq.consumer;
    
    import com.kangswx.rabbitmq.domain.News;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class NewsConsumer {
    
        public void recv(News news){
            System.out.println("接收到最新消息: " + news.getTitle() + ":" + news.getSource());
        }
    
        public static void main(String[] args) {
            //手动初始化Spring的IOC容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        }
    }
    
  13. 如果消费者需要解绑之前已经绑定的队列,需要在RabbitMQ管理端页面手动操作

  14. spring整合RabbitMQ生产者代码

  15. spring整合RabbitMQ消费者代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值