Java之RabbitMQ(二)多mq配置
场景:
springboot单项目,自身使用mq中间件处理一些业务需求,某些业务上又需要消费第三方mq消息,这时候需要我们单项目中配置多套mq,这时候,需要我们自定义多套mq相关连接工厂、模板、监听工厂、管理等流程,具体实现,参见如下:
实现:
1.配置文件:application.yml
spring:
rabbitmq:
first:
host: localhost
port: 5672
username: guest
password: guest
virtualHost: /channel-demo
second:
host: 100.100.100.184
port: 5672
username: guest
password: guest
virtualHost: /xxx-test
2.配置类:
FirstRabbitConfig配置类:
package com.xxx.channe.config; import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Configuration
@ConfigurationProperties("spring.rabbitmq.first")
public class FirstRabbitConfig extends AbstractRabbitConfig {
@Bean(name = "firstConnectionFactory")
@Primary
public ConnectionFactory firstConnectionFactory() {
return super.connectionFactory();
} @Bean(name = "firstRabbitTemplate")
@Primary
public RabbitTemplate rabbitTemplate(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
} @Bean(name = "firstFactory")
public SimpleRabbitListenerContainerFactory firstFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
} @Bean(value = "firstRabbitAdmin")
public RabbitAdmin firstRabbitAdmin(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
// rabbitAdmin.declareExchange(firstDirectExchange());
// rabbitAdmin.declareQueue(firstQueue());
// rabbitAdmin.declareBinding(firstBinding());
return rabbitAdmin;
} @Bean
public DirectExchange firstDirectExchange() {
return new DirectExchange("first-direct-exchange");
} @Bean
public Queue firstQueue() {
return new Queue("first-queue");
} @Bean
public Binding firstBinding() {
return BindingBuilder.bind(firstQueue()).to(firstDirectExchange()).with("first-routing-key");
}
}
SecondRabbitConfig配置类:
package com.xxx.channe.config; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Configuration
@ConfigurationProperties("spring.rabbitmq.second")
public class SecondRabbitConfig extends AbstractRabbitConfig { @Bean(name = "secondConnectionFactory")
public ConnectionFactory secondConnectionFactory() {
return super.connectionFactory();
} @Bean(name = "secondRabbitTemplate")
public RabbitTemplate secondRabbitTemplate(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
} @Bean(name = "secondFactory")
public SimpleRabbitListenerContainerFactory secondFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
} @Bean(value = "secondRabbitAdmin")
public RabbitAdmin secondRabbitAdmin(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
} // @Bean
// public DirectExchange secondDirectExchange() {
// return new DirectExchange("second-direct-exchange");
// }
//
// @Bean
// public Queue secondQueue() {
// return new Queue("second-queue");
// }
//
// @Bean
// public Binding secondBinding() {
// return BindingBuilder.bind(secondQueue()).to(secondDirectExchange()).with("second-routing-key");
// }
}
3.生产者:
package com.xxx.channe.service.impl; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Slf4j
@Component
public class MqTest { @Autowired
@Qualifier(value = "firstRabbitTemplate")
private RabbitTemplate firstRabbitTemplate;
public void send() {
firstRabbitTemplate.convertAndSend("first-direct-exchange", "first-routing-key", "我是 first。。。");
log.info("消息发送成功...");
} }
4.消费者:
package com.xxx.channe.service.impl; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Slf4j
@Component
public class MqTest {@RabbitListener(queues = {"first-queue"}, containerFactory = "firstFactory")
@RabbitHandler
public void firstTest() {
System.out.println("hello, first mq收到消息");
} }
未完待续...
Java之RabbitMQ(二)多mq配置的更多相关文章
- MQ配置安装
一,MQ安装 ./mqlicense.sh -accept rpm -ivh MQSeries*.rpm -- rpm -qa|grep MQSeries 二,MQ配置 环境变量配置(MQM)实际安 ...
- Java进阶专题(二十) 消息中间件架构体系(2)-- RabbitMQ研究
前言 接上文,这个继续介绍RabbitMQ,并理解其底层原理. 介绍 RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队列协议)协议实现的 ...
- RabbitMQ学习(二):Java使用RabbitMQ要点知识
转 https://blog.csdn.net/leixiaotao_java/article/details/78924863 1.maven依赖 <dependency> <g ...
- rabbit的简单搭建,java使用rabbitmq queue的简单例子和一些坑
一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...
- (一)RabbitMQ安装与基本配置
[博主使用的环境是阿里云ecs服务器,操作系统为centos] 安装erlang环境 RabbitMQ底层是Erlang语言,因此要先安装erlang环境,就像你要运行Java程序就必须先安装JRE/ ...
- Java SE 简介 & 环境变量的配置
Java SE 简介 & 环境变量的配置 一.Java 技术的三个方向 Java 技术分为三个方向 javaSE( Java Platform Standard Edition 标准版)用来开 ...
- Java 验证码、二维码
Java 验证码.二维码 资源 需要: jelly-core-1.7.0.GA.jar网站: http://lychie.github.io/products.html将下载下来的 jelly ...
- Java并发编程二三事
Java并发编程二三事 转自我的Github 近日重新翻了一下<Java Concurrency in Practice>故以此文记之. 我觉得Java的并发可以从下面三个点去理解: * ...
- JAVA开发环境 - 环境变量及配置
JDK是什么?JRE是什么? JRE(Java Runtime Environment):Java运行环境: JDK(Java Development Kit):Java开发工具包,里面已经包含JRE ...
随机推荐
- pymupdf 修改pdf文件
安装: sudo pip install pymupdf==1.16.0 引入使用: import fitz 可以插入文字.图片.... 帮助文档: PyMuPDF documentation ht ...
- Java checked异常 和 RuntimeException
RuntimeException RuntimeException是非常特殊的子类,你可以不用throw和throws. 哪怕你throw了,也没必要throws,即使你throws了,调用者也没必要 ...
- LOJ #6538. 烷基计数 加强版 加强版(生成函数,burnside引理,多项式牛顿迭代)
传送门. 不妨设\(A(x)\)表示答案. 对于一个点,考虑它的三个子节点,直接卷起来是\(A(x)^3\),但是这样肯定会计重,因为我们要的是无序的子节点. 那么用burnside引理,枚举一个排列 ...
- NOIp2018集训test-9-18
T1.Conjugate 只能选没选过的点,看成如果选了选过的堆的点就不管它继续选.如果第一次选到某一堆的点在第一次选到第一堆的点之前,这一堆对答案就会有1的贡献.那么a[i]有贡献的概率是a[i]和 ...
- Mac 精品软件
Snagit:Mac 平台下最优秀的屏幕截图软件,可以录制屏幕视频.截图以及对截图进行加工. Flux 4:强大易用的网页设计工具,不需要学习编程即可在一天内建成一个专业的网站 Jump Deskto ...
- [C#]记录一次异常排查,关于using语法、sqlserver数据库session、DBHelper类
最近在做一个基于asp.net和sqlserver的网站项目,发现网站运行一段时间之后,会报异常: 超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小 ...
- HDU6395-Sequence 矩阵快速幂+除法分块 矩阵快速幂模板
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
- centos 7 设置IP地址
先说下安装方式:我是采用的最小化安装 虚拟机软件:vmware 设置IP有两种情况,动态IP和静态IP,下面分别说明两种IP地址的设置方法 1.动态IP 条件:路由设置了动态分配IP地址(一般默认是动 ...
- 函数的属性和方法, apply和call的区别及bind的使用
==>我的新博客中 http://www.suanliutudousi.com/2017/08/27/%E5%87%BD%E6%95%B0%E7%9A%84%E5%B1%9E%E6%80%A7% ...
- 深夜Python - 第1夜 - for 迷 in 迷思
深夜Python - 第1夜 - for 迷 in 迷思 在一个月黑风高的夜晚,我悄悄打开编辑器,进入程序的世界.刚刚学会Python的我,由于一段时间的过度装B,被委托优化一段程序,我信心十足地接下 ...