Rabbitmq与spring整合之重要组件介绍——rabbitAdmin组件
rabbitAdmin组件是一个管理组件,主要是用户通过该组件进行rabbitmq的队列交换器虚拟主机等等进行操作。这里面有些教程说不用声明可以直接绑定,但是本博主运行时,不生命情况下就会报错,可能是跟所采用的版本有关。
不通过属性文件配置
配置类
package com.zxy.demo.config; 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(basePackages= {"com.zxy.demo.*"})
public class RabbitmqCofing {
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connection = new CachingConnectionFactory();
connection.setAddresses("192.168.10.110:5672");
connection.setUsername("guest");
connection.setPassword("guest");
connection.setVirtualHost("/");
return connection;
}
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
//用RabbitAdmin一定要配置这个,spring加载的是后就会加载这个类================特别重要
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
}
测试类:
package com.zxy.demo; 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 ForwardApplicationTests { @Test
public void contextLoads() {
}
@Autowired
private RabbitAdmin rabbitAdmin;
@Test
public void testAdmin() {
// 切记命名不能重复复
rabbitAdmin.declareQueue(new Queue("test.direct.queue"));
rabbitAdmin.declareExchange(new DirectExchange("test.direct"));
rabbitAdmin.declareBinding(new Binding("test.direct.queue", Binding.DestinationType.QUEUE, "test.direct", "mq.direct", null)); rabbitAdmin.declareQueue(new Queue("test.topic.queue", true,false, false));
rabbitAdmin.declareExchange(new TopicExchange("test.topic", true,false));
// 如果注释掉上面两句实现声明,直接进行下面的绑定竟然不行,该版本amqp-client采用的是5.1.2,将上面两行代码放开,则运行成功
rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.topic.queue", true,false, false))
.to(new TopicExchange("test.topic", true,false)).with("mq.topic"));
// 经过实验确实是需要先声明,才可以运行通过
rabbitAdmin.declareQueue(new Queue("test.fanout.queue",true,false,false,null));
rabbitAdmin.declareExchange(new FanoutExchange("test.fanout", true, false, null));
rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.fanout.queue", true, false,false))
.to(new FanoutExchange("test.fanout", true, false)));
rabbitAdmin.purgeQueue("test.direct.queue", false);//清空队列消息
}
}
Rabbitmq与spring整合之重要组件介绍——rabbitAdmin组件的更多相关文章
- Rabbitmq与spring整合之重要组件介绍——AMQP声明式配置&RabbitTemplate组件
上一节是使用rabbitAdmin的管理组件进行声明队列,交换器,绑定等操作,本节则是采用AMQP声明式配置来声明这些东西.AMQP声明主要是通过@Bean注解进行的. 配置: package com ...
- Thymeleaf模板引擎+Spring整合使用方式的介绍
尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...
- 消息队列 RabbitMQ 与 Spring 整合使用
一.什么是 RabbitMQ RabbitMQ 是实现 AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消 ...
- Spring(四):Spring整合Hibernate,之后整合Struts2
背景: 上一篇文章<Spring(三):Spring整合Hibernate>已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5 ...
- Netty快速入门(09)channel组件介绍
书接上回,继续介绍组件. ChannelHandler组件介绍 ChannelHandler组件包含了业务处理核心逻辑,是由用户自定义的内容,开发人员百分之九十的代码都是ChannelHandler. ...
- Blazor 组件库 BootstrapBlazor 中Editor组件介绍
组件介绍 Editor组件是对Summernote 组件的二次封装. 组件分为div模式和editor模式. 默认状态下editor模式的组件样子如下: 其代码如下: <Editor @bind ...
- Django框架11 /form组件、modelForm组件
Django框架11 /form组件.modelForm组件 目录 Django框架11 /form组件.modelForm组件 1. form组件介绍 2. form常用字段与插件 3. form所 ...
- RabbitMQ的介绍与spring整合
本文主要讲述的是个人参考官网及其他前辈博客,对RabbitMQ的一些理解与spring整个RabbitMQ. 一.RabbitMQ的介绍 1.1.什么是RabbitMQ RabbitMQ是一个由erl ...
- 关于RabbitMQ以及RabbitMQ和Spring的整合
转自:https://www.cnblogs.com/s648667069/p/6401463.html 基本概念 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是 ...
随机推荐
- 大数据篇:MapReduce
MapReduce MapReduce是什么? MapReduce源自于Google发表于2004年12月的MapReduce论文,是面向大数据并行处理的计算模型.框架和平台,而Hadoop MapR ...
- python去除字符串中的特殊字符(爬虫存储数据时会遇到不能作为文件名的字符串)
问题描述 今天在写爬虫爬取影评时,本来的思路把影评的标题作为文件名,将每个影评的详情内容写入到"标题.txt"文件中,直到我遇到了这个问题: 这时我突然意识到,文件名中有些字符是不 ...
- maven设置镜像地址
方法一:在maven文件夹下的settings.xml中添加(对所有的项目都有效) <mirror> <id>alimaven</id> <name>a ...
- 最全Pycharm教程(39)——Pycharm版本控制之本地Git用法
1.主题 介绍如果通过Pycharm使用本地Git集. 2.准备工作 (1)PyCharm版本为2.7或更高 (2)已经创建一个工程 (3)Git插件可用,对应可执行文件在 Git page页面正确配 ...
- UDP通讯代码
UDP客户端代码: import socket # 创建套接字 socket.AF_INET:IPV4 socket.SOCK_DGRAM:UDP协议 udp_client=socket.socket ...
- 2.9 logistic回归中的梯度下降法(非常重要,一定要重点理解)
怎么样计算偏导数来实现logistic回归的梯度下降法 它的核心关键点是其中的几个重要公式用来实现logistic回归的梯度下降法 接下来开始学习logistic回归的梯度下降法 logistic回归 ...
- Python学习笔记002
字符编码:把二进制字符翻译成字符 ASCII码表 256 一个字节,8个比特 支持中文: GB2312 GBK1.0 GB18030 BIG5(台湾) unicode UTF-8 开头定义 ...
- Python学习第十四课——面向对象基本思想part1
面向对象的基本思想 # 写法1 person1 = { 'name': 'hanhan', ', 'sex': '男' } def xue_xi(person): print('%s在学习' % pe ...
- C++ 判断是文件还是文件夹
转载:https://www.csdn.net/gather_23/NtDaIg1sMDYtYmxvZwO0O0OO0O0O.html Windows平台代码如下: #include <wind ...
- Python 基础之循环结构 while
一.while循环介绍 while 循环 可以提高代码的效率,减少代码的冗余 while 条件表达式: code1 code2如果条件表达式成立,返回Ture,就执行其中的代码块 1.基本 ...