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. lua加载DLL

    .cpp //若没有在项目属性--库文件.依赖文件.包含添加.则添加一下路径 #pragma  comment (lib,"lua5.1.lib") #include " ...

  2. bash 中的 :=、=、:-、-、=?、?、:+、+

    bash 中的 :=.=.:-.-.=?.?.:+.+ 来源 https://www.cnblogs.com/fhefh/archive/2011/04/22/2024750.html 变量替换和变量 ...

  3. std::list保存大量数据时,类型即是无析构函数,该list析构时会占用大量CPU

    std::list保存大量数据时,类型即是无析构函数,该list析构时会占用大量CPU

  4. Advanced Installer 不弹出预安装的软件的窗口

    需求:当他电脑上没有sql server client 的时候,或没有localdb的时候,那么安装包会弹出窗口,让他选择 一个组件 一个组件的安装 太麻烦. 有没有办法,打开安装包就安装 安装的过程 ...

  5. Hadoop入门到实战全套大数据Hadoop学习视频

    资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章或者后台回复[Hadoop]获取,即可获取资源下载链接 更多资源获取,请关注公总号RaoRao1994

  6. 原创博客>>>解决粘包问题的方法

    目录 原创博客>>>解决粘包问题的方法 原创博客>>>解决粘包问题的方法 服务端: import socket import struct service=sock ...

  7. C# 图像基本处理

    使用第三方:AForge实现视频采集(实现视频采集.暂停) 实现图片的常用处理功能:旋转.反色.灰度.放大.缩小.模糊.拉伸.增强.锐化.裁剪...... 实现对图片进行文字编辑......

  8. laravel的下载与安装

    下载代码 https://github.com/laravel/laravel 在开发环境中配置好之后将根目录的 .env.example 文件改成 .env ,此文件是laravel的配置文件,将 ...

  9. C#微信公众平台账号开发真正给初学者的文章

    微信越来越受到大众人群的喜爱,但是对于开发人员来说刚接触肯能还是一头雾水的,比如像我,看了三四天文档感觉要吐,但是程序还是要写知识还是要学.发现了一个比较适合初学者的文章送给大家,废话到此:(转贴吧) ...

  10. Beta冲刺——星期四

    这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 飞猪们 这个作业的目标 剩余任务预估,分配任务(开发,测试等).按要求提交当天冲刺报告. ...