RabbitAdmin底层实现就是从Spring容器中获取Exchange、Binding、Routingkey以及Queue的@声明
然后使用RabbitTemplate的execute方法执行对应的声明、修改、删除等一系列RabbitMQ基础功能操作
例如添加一个交换机、删除一个绑定、清空一个队列里的消息等

注意:autoStartup必须设置为true,否则Spring容器不会加载RabbitAdmin类

需导入的依赖

<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

创建RabbitAdmin,使用@Bean将其注入到spring容器中

package com.dwz.spring;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.dwz.spring.*")
public class RabbitMQConfig { @Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses("127.0.0.1:5672");
connectionFactory.setVirtualHost("/vhost_dwz");
connectionFactory.setUsername("root_dwz");
connectionFactory.setPassword("123456");
return connectionFactory;
} @Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
System.err.println("RabbitAdmin启动了。。。");
//设置启动spring容器时自动加载这个类(这个参数现在默认已经是true,可以不用设置)
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
}

在test演示其相关方法

package com.dwz.spring;
import java.util.HashMap; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class TestDwz {
@Autowired
private RabbitAdmin rabbitAdmin; @Test
public void test() {
rabbitAdmin.declareExchange(new DirectExchange("test.direct", false, false));
rabbitAdmin.declareExchange(new TopicExchange("test.topic", false, false));
rabbitAdmin.declareExchange(new FanoutExchange("test.fanout", false, false)); rabbitAdmin.declareQueue(new Queue("test.direct.queue", false));
rabbitAdmin.declareQueue(new Queue("test.topic.queue", false));
rabbitAdmin.declareQueue(new Queue("test.fanout.queue", false)); //先声明队列和交换机再绑定
rabbitAdmin.declareBinding(new Binding("test.direct.queue",
Binding.DestinationType.QUEUE,
"test.direct", "direct", new HashMap<>())); //绑定的时候再声明队列和交换机
rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.topic.queue", false))//直接创建队列
.to(new TopicExchange("test.topic", false, false))//直接创建交换机,建立关联关系
.with("user.#"));//指定路由key rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.fanout.queue", false))
.to(new FanoutExchange("test.fanout", false, false))); //清空队列数据
rabbitAdmin.purgeQueue("test.topic.queue", false);
}
}

使用SpringAMQP的@Bean方式去声明

/**
* 针对消费者的配置
* 1.设置交换机的类型
* 2.将队列绑定到交换机
* FanoutExchange:将消息分发到所有绑定的队列,无routingkey的概念
* TopicExchange:多关键字匹配
* HeadersExchange:通过添加属性key-value匹配
* DirectExchange:按照routingkey分发到指定队列
*/
@Bean
public TopicExchange exchange001() {
return new TopicExchange("topic001", true, false);
} @Bean
public Queue queue001() {
return new Queue("queue001", true);//队列持久化
} @Bean
public Binding binding001() {
return BindingBuilder.bind(queue001()).to(exchange001()).with("spring.*");
} @Bean
public TopicExchange exchange002() {
return new TopicExchange("topic002", true, false);
} @Bean
public Queue queue002() {
return new Queue("queue002", true);//队列持久化
} @Bean
public Binding binding002() {
return BindingBuilder.bind(queue002()).to(exchange002()).with("rabbit.*");
} @Bean
public TopicExchange exchange003() {
return new TopicExchange("topic003", true, false);
} @Bean
public Queue queue003() {
return new Queue("queue003", true);//队列持久化
} @Bean
public Binding binding003() {
return BindingBuilder.bind(queue003()).to(exchange003()).with("mq.*");
} @Bean
public Queue queue_image() {
return new Queue("image_queue", true);//队列持久化
} @Bean
public Queue queue_pdf() {
return new Queue("pdf_queue", true);//队列持久化
}

RabbitAdmin的更多相关文章

  1. Rabbitmq与spring整合之重要组件介绍——rabbitAdmin组件

    rabbitAdmin组件是一个管理组件,主要是用户通过该组件进行rabbitmq的队列交换器虚拟主机等等进行操作.这里面有些教程说不用声明可以直接绑定,但是本博主运行时,不生命情况下就会报错,可能是 ...

  2. CentOS6.7安装RabbitMQ3.6.5

    1.安装所有依赖包yum install -y gcc ncurses ncurses-base ncurses-devel ncurses-libs ncurses-static ncurses-t ...

  3. rabbitmq使用心得

    因为公司项目需要使用消息中间件,实现相关业务的异步处理,所有选用了rabbitmq.通过看文档,爬过一个一个坑,终于还是实现了相关功能. 直接上配置文件: <?xml version=" ...

  4. spring-amqp 动态创建queue、exchange、binding

    pom.xml <!-- mq 依赖 --> <dependency> <groupId>com.rabbitmq</groupId> <arti ...

  5. Centos6.5 安装 RabbitMQ3.6.1

    Centos6.5 安装 RabbitMQ3.6.1 个人安装RabbitMQ总结: 安装编译工具 yum -y install make gcc gcc-c++ kernel-devel m4 nc ...

  6. spring boot实战(第十二篇)整合RabbitMQ

    前言 最近几篇文章将围绕消息中间件RabbitMQ展开,对于RabbitMQ基本概念这里不阐述,主要讲解RabbitMQ的基本用法.Java客户端API介绍.spring Boot与RabbitMQ整 ...

  7. mq消息队列

    rabbitmq学习9:使用spring-amqp发送消息及同步接收消息 通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的.有兴趣的朋友 ...

  8. RabbitMQ安装和使用(和Spring集成)

    一.安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 E ...

  9. RabbitMQ-从基础到实战(6)— 与Spring集成

    0.目录 RabbitMQ-从基础到实战(1)- Hello RabbitMQ RabbitMQ-从基础到实战(2)- 防止消息丢失 RabbitMQ-从基础到实战(3)- 消息的交换(上) Rabb ...

随机推荐

  1. Neo4j WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual

    you can add a line in /etc/default/neo4j: NEO4J_ULIMIT_NOFILE=60000 to set the ulimit setting (60000 ...

  2. element-ui 中日期控件限制时间跨度

    支持点击今天及之前任意日期,前后跨度不超过31天,且不超过今天 <el-date-picker v-model="searchForm.dateRange" type=&qu ...

  3. Python之if-else语句

    if--else语句if username == 'admin' and password == '123456': print('身份验证成功!') else: print('身份验证失败!')if ...

  4. 彻底卸载mysql数据库~

    以下操作以win10操作系统为例 1 停止window的MySQL服务 打开此台电脑的管理 ---> 服务和应用程序 --->服务,找到mysql的服务并停止 2 卸载MySQL安装程序 ...

  5. Delphi TIdUDPServer组件

  6. deep_learning_Softmax()

    该节课中提到了一种叫作softmax的函数,因为之前对这个概念不了解,所以本篇就这个函数进行整理,如下: 维基给出的解释:softmax函数,也称指数归一化函数,它是一种logistic函数的归一化形 ...

  7. Python制作的射击游戏

    如果其他朋友也有不错的原创或译文,可以尝试推荐给伯乐在线.] 你有没有想过电脑游戏是怎样制作出来的?其实它没有你想象的那样复杂! 在这个教程里,你要学做一个叫<兔子和獾>的塔防游戏,兔子作 ...

  8. Linux根文件系统和目录结构及bash特性3

    bash的基础特性: 命令补全:        shell程序在接收到用户执行命令的请求,分析完成之后,最左侧的字符串会被当作命令        命令查找机制:            查找内部命令   ...

  9. 需求分析&系统设计

    这个作业属于哪个课程 课程链接 这个作业要求在哪里 作业要求 团队名称 朋友 代打了解一下 这个作业的目标 需求分析&系统设计 一.团队成员的姓名学号列表 学号 姓名 特长 061126 黄天 ...

  10. Sereja and Brackets CodeForces - 380C (树状数组+离线)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...