RabbitMQ-AmqpAdmin简单封装以快速创建Queue和Exchange并绑定
2020-06-28 05:08
1476 查看
用以快速创建消息队列(Queue)和交换器(Exchange)并绑定
import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class RabbitMQCustomizer { @Autowired AmqpAdmin amqpAdmin; private CustomExchange customExchange; private Queue queue; private Binding binding; public void registerExchangeAndQueueAndBinding(String queueName, String routingKey, String exchangeName, String exchangeType) { this.setCustomExchange(new CustomExchange(exchangeName,exchangeType,true,true)); this.setQueue(new Queue(queueName,true)); this.setBinding( new Binding( this.getQueue().getName(), Binding.DestinationType.QUEUE, this.getCustomExchange().getName(), routingKey, null ) ); amqpAdmin.declareExchange(this.getCustomExchange()); amqpAdmin.declareQueue(this.getQueue()); amqpAdmin.declareBinding(this.getBinding()); } private CustomExchange getCustomExchange() { return customExchange; } private void setCustomExchange(CustomExchange customExchange) { this.customExchange = customExchange; } private Queue getQueue() { return queue; } private void setQueue(Queue queue) { this.queue = queue; } private Binding getBinding() { return binding; } private void setBinding(Binding binding) { this.binding = binding; } }
通过调用registerExchangeAndQueueAndBinding方法
传入 (Queue)消息队列名,(routingKey)路由键名,(Exchange)交换器名,(direct、fanout、headers…)交换器类型
示例:
import com.live.config.RabbitMQCustomizer; import org.junit.jupiter.api.Test; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class RabbimqApplicationTests { @Autowired RabbitMQCustomizer rabbitMQCustomizer; @Test void setRabbitMQCustomizer() { rabbitMQCustomizer .registerExchangeAndQueueAndBinding( //消息队列 "direct.queue", //路由键 "exchange.direct.queue", //交换器 "exchange.direct", //交换器类型 ExchangeTypes.DIRECT); } }
结果
- Exchange
- Queue
- Binding
相关文章推荐
- 创建rabbitmq:virtual host、exchange、queue
- 创建rabbitmq:virtual host、exchange、queue
- 创建rabbitmq:virtual host、exchange、queue
- rabbitmq--通过rabbitmqctl创建exchange和queue
- JEPLUS之简单流程创建——JEPLUS软件快速开发平台
- CodeIgniter 入门:快速简单地创建 MVC 应用程序
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
- Jmeter 快速入门教程(二)--创建简单web测试
- RabbitMQ中 exchange、route、queue的关系
- 利用Maven快速创建一个简单的spring boot 实例
- 简单快速搭建Exchange 2013 服务器
- android 简单实用的Log打印类封装,助你快速定位问题
- C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(一)
- CodeIgniter 入门:快速简单地创建 MVC 应用程序
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- android 简单实用的Log打印类封装,助你快速定位问题
- Retrofit 最简单的快速入门及自己封装
- Spring Boot 系列 | 第一篇:快速创建一个简单的应用
- electron从安装到打包成exe---7个步骤快速完成简单封装
- Sassaparilla – 简单、快速的创建响应式网站