Java中动态声明与绑定Rabbit MQ队列以及延迟队列的实现与使用

一 概述

通常,我们会通过Spring schemal来配置队列基础设施、队列声明以及绑定等功能,这样让我们能够很方便的通过Spring注入对象去使用它,但有不足之处就是,在项目中如果我们想要多个队列去分担不同的任务,此时我们不得不创建很多不同的Rabbit MQ Spring schemal,那么这种做法就显得太过繁琐与笨重了。反之,在Java代码里动态的去声明和绑定队列,就会方便很多了,而在schemal中我们只需引入Rabbit MQ的相关配置即可。本篇博客会讲解如何在Java代码中动态的声明与绑定队列以及延迟队列的实现。

注意:

  • 本篇博客介绍的是Java语言下的使用,客户端使用的是Spring AMQP,版本为1.7.7(详情见pom.xml);
  • 本篇博客不使用Rabbit MQ的数据对象转化,如有需要须自行实现;
  • 代码中声明的Exchange都为D型的,如果需要别的类型,可自行抽取代码。
二 配置Rabbit MQ
  • pom.xm 引入Spring AMQP
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>1.7.7.RELEASE</version>
</dependency>
  • 在 config.properties(一个自定义的属性配置文件)中配置Rabbit MQ相关,需要在Spring的schemal 中导入
# Rabbit MQ
rabbit.username=test
rabbit.password=123123
rabbit.port=5672
rabbit.host=192.168.30.218
rabbit.virtual.host=/rabbit
  • 编写applicationContext-rabbitmq.xml schemal
<?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/rabbit
           http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <rabbit:connection-factory id="rabbitMqConnectionFactory"
                               host="${rabbit.host}" port="${rabbit.port}"
                               username="${rabbit.username}"
                               password="${rabbit.password}"
                               virtual-host="${rabbit.virtual.host}"
                               channel-cache-size="300"
                               publisher-confirms="true"/>

    <rabbit:template id="rabbitAmqpTemplate" connection-factory="rabbitMqConnectionFactory"/>

    <rabbit:admin id="rabbitAdmin" connection-factory="rabbitMqConnectionFactory"/>

</beans>
  • 抽取生产者和消费者公共配置接口IRabbitMqConfig
package com.bell.rabbitmq;

/**
 * @Author: yqs
 * @Date: 2019/1/25
 * @Time: 18:33
 * Copyright © Bell All Rights Reserved.
 */
public interface IRabbitMqConfig {
   

    /**
     * queue name
     *
     * @return
     */
    String queueName();

    /**
     * queue exchange name
     *
     * @return
     */
    String queueExchangeName();

    /**
     * queue route key
     *
     * @return
     */
    String queueRouteKey();

}
  • 抽取生产者和消费者公共配置抽象类AbstractRabbitMqBase并实现IRabbitMqConfig接口,但在抽象类型不实现IRabbitMqConfig接口
package com.bell.rabbitmq;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @Author: yqs
 * @Date: 2019/1/25
 * @Time: 18:37
 * Copyright © Bell All Rights Reserved.
 */
public abstract class AbstractRabbitMqBase implements IRabbitMqConfig {
   

    @Resource
    private RabbitAdmin rabbitAdmin;

    @Resource
    private RabbitTemplate rabbitAmqpTemplate
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值