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组件的更多相关文章

  1. Rabbitmq与spring整合之重要组件介绍——AMQP声明式配置&RabbitTemplate组件

    上一节是使用rabbitAdmin的管理组件进行声明队列,交换器,绑定等操作,本节则是采用AMQP声明式配置来声明这些东西.AMQP声明主要是通过@Bean注解进行的. 配置: package com ...

  2. Thymeleaf模板引擎+Spring整合使用方式的介绍

    尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...

  3. 消息队列 RabbitMQ 与 Spring 整合使用

    一.什么是 RabbitMQ RabbitMQ 是实现 AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消 ...

  4. Spring(四):Spring整合Hibernate,之后整合Struts2

    背景: 上一篇文章<Spring(三):Spring整合Hibernate>已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5 ...

  5. Netty快速入门(09)channel组件介绍

    书接上回,继续介绍组件. ChannelHandler组件介绍 ChannelHandler组件包含了业务处理核心逻辑,是由用户自定义的内容,开发人员百分之九十的代码都是ChannelHandler. ...

  6. Blazor 组件库 BootstrapBlazor 中Editor组件介绍

    组件介绍 Editor组件是对Summernote 组件的二次封装. 组件分为div模式和editor模式. 默认状态下editor模式的组件样子如下: 其代码如下: <Editor @bind ...

  7. Django框架11 /form组件、modelForm组件

    Django框架11 /form组件.modelForm组件 目录 Django框架11 /form组件.modelForm组件 1. form组件介绍 2. form常用字段与插件 3. form所 ...

  8. RabbitMQ的介绍与spring整合

    本文主要讲述的是个人参考官网及其他前辈博客,对RabbitMQ的一些理解与spring整个RabbitMQ. 一.RabbitMQ的介绍 1.1.什么是RabbitMQ RabbitMQ是一个由erl ...

  9. 关于RabbitMQ以及RabbitMQ和Spring的整合

    转自:https://www.cnblogs.com/s648667069/p/6401463.html 基本概念 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是 ...

随机推荐

  1. highcharts饼图

    效果: JSON加载数据: var chartseries2 = [ { name: '完成' + data.rate + '%', y: data.rate }, { name: '未完成' + d ...

  2. Spring Boot 项目本地运行无异常,部署到 Linux 服务器运行报错:java.lang.ClassNotFoundException

    一 背景 最近在用 Springboot 开发项目 A,引了小伙伴开发的模块 B,本地起服务,运行的好好的,等部署到服务器上,一运行就报错:Caused by: java.lang.ClassNotF ...

  3. C++赋值操作符不能继承

    在网上搜索相关资料时,看到有人说,赋值操作符可以被继承,并且给出了一个例子. 一时间,也没想出那个例子错在哪里. 例子代码如下: #include <iostream> using nam ...

  4. icos_snake_port-to-port_configuration

    Topo: # $language = "Python" # $interface = "1.0"# Author:Bing# Date:6/21/2017# ...

  5. day5-1继承

    继承: Constructor属性和prototype属性的关系: 创建了自定义的构造函数之后,其原型对象默认会取得constructor属性:当调用构造函数创建一个新实例后,该实例的内部将包含一个指 ...

  6. Linux centosVMware LAMP php-fpm的pool、php-fpm慢执行日志、open_basedir、php-fpm进程管理

    一.php-fpm的pool vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加 include = etc/php-fpm.d/*.conf mkd ...

  7. Dynamic Programming(动态规划)

    钢材分段问题 #include<iostream> #include<vector> using namespace std; class Solution { public: ...

  8. 引入CSS的方式、link和@import的区别

    引入CSS的方式有四种:内联方式.嵌入方式.链接方式.导入方式. 内联方式 内联方式指的是直接在 HTML 标签中的 style 属性中添加 CSS. <div style="back ...

  9. vue中配置sass(包含vue-cli 3)

    目录 vue vue cli 3 老版本的脚手架搭建的项目 版本 安装 不用修改任何配置 vue文件中使用 vue 更新时间: 2018-09-21 vue cli 3 选择 Manually sel ...

  10. redis5.0版本集群搭建

    模式简介 Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令,比如:mget, 因 ...