目前有整合RabbitMQ到项目中, 有个需求就是:需要根据不同的门店创建不同的队列, 而在启动项目初始化的时候, 是不知到有哪些门店在使用,所以创建了也就浪费了资源。所以就有了动态创建队列的需求。
然后在网上疯狂的搜罗了一波。现总结如下:
RabbitConfig.java 配置类

import java.util.HashMap;
import java.util.Map;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 配置rabbitMQ* @author Rayson517**/
@Configuration
public class RabbitConfig {@Beanpublic RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){return new RabbitAdmin(connectionFactory);}@Beanpublic RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){RabbitTemplate rabbitTemplate=new RabbitTemplate(connectionFactory);//数据转换为json存入消息队列rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());return rabbitTemplate;}
}

RabbitUtil.java工具类

import java.util.Date;
import java.util.UUID;import javax.annotation.Resource;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AbstractExchange;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.SerializationUtils;@Configuration
public class RabbitUtil {private static final Logger logger = LoggerFactory.getLogger(RabbitUtil.class);private final RabbitAdmin rabbitAdmin;private final RabbitTemplate rabbitTemplate;@Autowiredpublic RabbitUtil(RabbitAdmin rabbitAdmin, RabbitTemplate rabbitTemplate){this.rabbitAdmin = rabbitAdmin;this.rabbitTemplate = rabbitTemplate;}/*** 转换Message对象* @param messageType 返回消息类型 MessageProperties类中常量* @param msg* @return*/public Message getMessage(String messageType, Object msg){MessageProperties messageProperties = new MessageProperties();messageProperties.setContentType(messageType);Message message = new Message(msg.toString().getBytes(),messageProperties);return message;}/*** 有绑定Key的Exchange发送* @param routingKey* @param msg*/public void sendMessageToExchange(TopicExchange topicExchange, String routingKey, Object msg){Message message = getMessage(MessageProperties.CONTENT_TYPE_JSON,msg);rabbitTemplate.send(topicExchange.getName(), routingKey, message);}/*** 没有绑定KEY的Exchange发送* @param exchange* @param msg*/public void sendMessageToExchange(TopicExchange topicExchange, AbstractExchange exchange, String msg){addExchange(exchange);logger.info("RabbitMQ send "+exchange.getName()+"->"+msg);rabbitTemplate.convertAndSend(topicExchange.getName(),msg);}/*** 给queue发送消息* @param queueName* @param msg*/public void sendToQueue(String queueName, String msg){sendToQueue(DirectExchange.DEFAULT, queueName, msg);}/*** 给direct交换机指定queue发送消息* @param directExchange* @param queueName* @param msg*/public void sendToQueue(DirectExchange directExchange, String queueName, String msg){Queue queue = new Queue(queueName);addQueue(queue);Binding binding = BindingBuilder.bind(queue).to(directExchange).withQueueName();rabbitAdmin.declareBinding(binding);//设置消息内容的类型,默认是 application/octet-stream 会是 ASCII 码值rabbitTemplate.convertAndSend(directExchange.getName(), queueName, msg);}/*** 给queue发送消息* @param queueName* @param msg*/public String receiveFromQueue(String queueName){return receiveFromQueue(DirectExchange.DEFAULT, queueName);}/*** 给direct交换机指定queue发送消息* @param directExchange* @param queueName* @param msg*/public String receiveFromQueue(DirectExchange directExchange, String queueName){Queue queue = new Queue(queueName);addQueue(queue);Binding binding = BindingBuilder.bind(queue).to(directExchange).withQueueName();rabbitAdmin.declareBinding(binding);String messages = (String)rabbitTemplate.receiveAndConvert(queueName);System.out.println("Receive:"+messages);return messages;}/*** 创建Exchange* @param exchange*/public void addExchange(AbstractExchange exchange){rabbitAdmin.declareExchange(exchange);}/*** 删除一个Exchange* @param exchangeName*/public boolean deleteExchange(String exchangeName){return rabbitAdmin.deleteExchange(exchangeName);}/*** Declare a queue whose name is automatically named. It is created with exclusive = true, autoDelete=true, and* durable = false.* @return Queue*/public Queue addQueue(){return rabbitAdmin.declareQueue();}/*** 创建一个指定的Queue* @param queue* @return queueName*/public String addQueue(Queue queue){return rabbitAdmin.declareQueue(queue);}/*** Delete a queue.* @param queueName the name of the queue.* @param unused true if the queue should be deleted only if not in use.* @param empty true if the queue should be deleted only if empty.*/public void deleteQueue(String queueName, boolean unused, boolean empty){rabbitAdmin.deleteQueue(queueName,unused,empty);}/*** 删除一个queue* @return true if the queue existed and was deleted.* @param queueName*/public boolean deleteQueue(String queueName){return rabbitAdmin.deleteQueue(queueName);}/*** 绑定一个队列到一个匹配型交换器使用一个routingKey* @param queue* @param exchange* @param routingKey*/public void addBinding(Queue queue ,TopicExchange exchange,String routingKey){Binding binding = BindingBuilder.bind(queue).to(exchange).with(routingKey);rabbitAdmin.declareBinding(binding);}/*** 绑定一个Exchange到一个匹配型Exchange 使用一个routingKey* @param exchange* @param topicExchange* @param routingKey*/public void addBinding(Exchange exchange,TopicExchange topicExchange,String routingKey){Binding binding = BindingBuilder.bind(exchange).to(topicExchange).with(routingKey);rabbitAdmin.declareBinding(binding);}/*** 去掉一个binding* @param binding*/public void removeBinding(Binding binding){rabbitAdmin.removeBinding(binding);}
}

然后在需要的地方注入这个RabbitUtil工具类就可以使用了

RabbitMQ之业务场景(四):动态创建,删除队列工具类,拿来即用相关推荐

  1. 安卓创建第一个工具类

    安卓创建工具类 1.运用IOC框架简化代码 注解 (Annotation):@Class 给代码看的处理代码逻辑 2. 创建自己的注解 @Target() ElementType.FIELD 放在属性 ...

  2. Mybatis的动态创建删除表

    Mybatis中可以使用JSTL标签 动态删除表 Mapper void deleteTable(@Param("tableName") String tableName); Ma ...

  3. Pygame飞机大战(四)——创建己方飞机的类,并添加子弹吧

    我们接下来要基于pygame.sprite.Sprite创建自己的类,首先必须是主角的,创建一个己方飞机的类,并且给飞机加上子弹哈! 首先我们还是先分析一下,己方飞机需要有哪些部分构成: 自身的图片加 ...

  4. JUC(3)List、Set、Map集合线程安全Callable创建线程三大工具类:CountDownLatch减法计数器、CyclicBarrier加法计数器、Semaphore计数信号量

    1. List集合线程安全 CopyOnWriteArrayList是线程安全的集合: ArrayList是线程不安全的集合: Vector是线程安全的集合(不推荐使用) 1.1 解决ArrarLis ...

  5. JUC系列(四) callable与 常用的工具类

  6. 一个字稳,云原生产品家族支撑冬奥会九大业务场景,打造云上奥运新体验

    北京冬奥会已经成为收视最高的一届冬奥会,在转播时长.技术.内容制作方式等多方面都书写了新记录.云技术的应用,是本届北京冬奥会赛事转播的一大特色. 而云原生作为云计算的新界面,如何稳定支撑北京冬奥会多个 ...

  7. php业务的适用场景,根据业务场景寻找合适解决方案(PHP)?

    系统环境: Linux PHP 7.3 ThinkPHP 6 先说一下业务场景: 订单创建成功之后,每一笔订单都需要进行统计及其他业务处理. 如何及时发现处理失败的订单,然后进行补单处理. 订单所产生 ...

  8. throwable四参构造_深入分析Java反射(四)-动态代理

    动态代理的简介 Java动态代理机制的出现,使得Java开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象,便能动态地获得代理类.代理类会负责将所有的方法调用分派到委托对象上反射执行,在分 ...

  9. Drools动态创建规则文件并动态调用

    目录 1.编写规则内容,如下图 2.创建KieSession 3.根据规则名称货主匹配规则进行执行 3.1.根据规则名称完全匹配方式执行 3.2.根据规则名称前缀匹配方式执行 1.编写规则内容,如下图 ...

  10. Android动态加载黑科技 动态创建Activity模式

    基本信息 Author:kaedea GitHub:android-dynamical-loading 代理Activity模式的限制 还记得我们在代理Activity模式里谈到启动插件APK里的Ac ...

最新文章

  1. 【Android基础】RecyclerView的设计艺术
  2. 【译】Engineering Security Through Coordination Problems
  3. java库net2.0下载_.NET Framework各版本独立下载.NET Framework 3.5下载.NET Framework 2.0下载...
  4. JavaScript三种创建构造函数的方式
  5. windows下面怎么github ssh 公钥,然后克隆项目
  6. windows截图c语言,window 截取屏幕,并实现jpeg压缩
  7. Dell PowerEdge R740xd可以做什么?
  8. 真牛!打开mysql
  9. JavaScript之对象学习
  10. 【numpy】argmax参数辨析(axis=0,axis=1,axis=-1)
  11. 【OpenCV4】fatal error: opencv2/core.hpp: No such file or directory 解决方法
  12. qart 图形二维码 html2canvas下载二维码
  13. Package inputenc Error: Invalid UTF-8 byte “A1;Improper alphabetic constant. <to be read again>
  14. 安卓adb push图片到相册后刷新相册(Mac版)
  15. cisco 三层交换机与二层交换机级联 vlan trunk
  16. 原型制作与图解——墨刀工具
  17. 《廊桥遗梦》这样确切的爱,一生只有一次
  18. 我是如何零基础开始能写爬虫的
  19. 【电脑办公软件有哪些】万彩办公大师教程丨重复音频文件探测工具
  20. 武汉第一职业教育中心计算机技能高考,武汉市第一职业教育中心2019年招生简章...

热门文章

  1. VC++实现禁止上网
  2. 计算机学win7画图,Windows7系统画图工具怎么打开?
  3. OSPF多区域配置【eNSP实现】
  4. Anaconda重装后【系统找不到指定的路径】
  5. 源码解析少儿编程微课程9:机械手臂模拟制作
  6. 如果让你来制作一个访问量很高的大型网站,你会如何来管理所有CSS文件、JS与图片?
  7. 美团外卖移动端性能监测体系实现
  8. android USB OTG功能如何打开及实现
  9. Latex VS Code 编辑中文Latex乱码——详细解决方案操作流程
  10. JEECG集成ACTIVITI