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是 ...
随机推荐
- 报错PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"
今天在调用第三方HTTPS接口的时候,一直显示这个报错,然后百度很久,有2种解决方法,一个是说自己手动去导入,第二种用代码忽略证书验证.我用二种方式, 复制即用, public void test2( ...
- Microsoft Cortana移动版除美国市场外不再可用
导读 先前已经透露,Microsoft Cortana的移动版本已不复存在.目前,Microsoft Cortana在移动设备上的多个国家和地区中支持多种语言.微软的Cortana移动版本不再支持的市 ...
- DuiLib中FlashDemo的例子经验杂粹1
转载:https://www.jianshu.com/p/3e958ae9e5ab 最近用duilib做个东西,经常卡壳 ,而且以前学的现在又忘.现在觉得应该好好做笔记,以前老是觉得博客是很郑重的东西 ...
- Spark教程——(8)本地执行spark-sql程序
在程序中设定Spark SQL的运行模式: //.setMaster("local")设置本地运行模式 val conf = new SparkConf().setAppName( ...
- 建小程序 - 报Error: EPERM : operation not permitted, scandir mac下改变一个目录的访问权限
问题:用微信开发者工具,建一个小程序,报错(见图1): 建小程序 - 报Error: EPERM : operation not permitted, scandir 解决: 1.打开终端 2.cd ...
- HashMap的应用
HashMap <String,String> hsMap=new HashMap<String,String> hsMap.put("1","v ...
- ASP.NET Core的身份认证框架IdentityServer4--入门
ASP.NET Core的身份认证框架IdentityServer4--入门 2018年08月11日 10:09:00 qq_42606051 阅读数 4002 https://blog.csdn ...
- js中各种类型转换为Boolean类型
数据类型 转换为true的值 转换为false的值 Boolean true false String 任何非空字符串 空字符串 Number 任何非零数字值(包括无穷大) 0和null ...
- Centos7 安装redis及简单使用
一.redis的介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset( ...
- ffmpeg使用C语言sdk实现抽取视频中的视频数据
主要使用函数 特征码:Start code 解码的一些视频参数,分辨率和帧率:SPS/PPS ffmpeg获取SPS/PPS:codec->extradata 实例 #include <s ...