springboot12(rabbitmq)
RabbitAutoConfiguration
@Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {
//配置连接工厂
@Bean
public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties properties,
ObjectProvider<ConnectionNameStrategy> connectionNameStrategy) throws Exception {
PropertyMapper map = PropertyMapper.get();
CachingConnectionFactory factory = new CachingConnectionFactory(
getRabbitConnectionFactoryBean(properties).getObject());
map.from(properties::determineAddresses).to(factory::setAddresses);
map.from(properties::isPublisherConfirms).to(factory::setPublisherConfirms);
map.from(properties::isPublisherReturns).to(factory::setPublisherReturns);
RabbitProperties.Cache.Channel channel = properties.getCache().getChannel();
map.from(channel::getSize).whenNonNull().to(factory::setChannelCacheSize);
map.from(channel::getCheckoutTimeout).whenNonNull().as(Duration::toMillis)
.to(factory::setChannelCheckoutTimeout);
RabbitProperties.Cache.Connection connection = properties.getCache().getConnection();
map.from(connection::getMode).whenNonNull().to(factory::setCacheMode);
map.from(connection::getSize).whenNonNull().to(factory::setConnectionCacheSize);
map.from(connectionNameStrategy::getIfUnique).whenNonNull().to(factory::setConnectionNameStrategy);
return factory;
}
RabbitProperties.class // 封装了rabbitmq的所有配置
RabbitTemplate // 给rabbitmq发送和接收消息
AmqpAdmin //rabbitmq系统功能管理组件
消息转换器,在rabbitmqtemplate中默认使用SimpleMessageConverter
private MessageConverter messageConverter = new SimpleMessageConverter();
测试的代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitmqApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void contextLoads() {
//Object 默认当成消息体
//rabbitTemplate.convertAndSend(exchange,routeKey,Object);
Map<String, Object> map = new HashMap<>();
map.put("msg", "这是java发的第一个消息");
map.put("data", Arrays.asList("hello", "what is your name?"));
rabbitTemplate.convertAndSend("exchange.direct", "delay", map);
}
@Test
public void receive() {
Object delay = rabbitTemplate.receiveAndConvert("delay");
System.out.println(delay.getClass());
System.out.println(delay);
}
@Test
public void fanout(){
rabbitTemplate.convertAndSend("exchange.fanout","",new Book("你是谁?","199"));
}
}
启动类
@EnableRabbit
@SpringBootApplication
public class SpringbootRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
}
}
配置类
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
service
@Service
public class BookService {
@RabbitListener(queues = "delay")
public void receive(Book book){
System.out.println(book.toString());
}
@RabbitListener(queues = "delay.news")
public void receive2(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
springboot12(rabbitmq)的更多相关文章
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- RabbitMq应用二
在应用一中,基本的消息队列使用已经完成了,在实际项目中,一定会出现各种各样的需求和问题,rabbitmq内置的很多强大机制和功能会帮助我们解决很多的问题,下面就一个一个的一起学习一下. 消息响应机制 ...
- 如何优雅的使用RabbitMQ
RabbitMQ无疑是目前最流行的消息队列之一,对各种语言环境的支持也很丰富,作为一个.NET developer有必要学习和了解这一工具.消息队列的使用场景大概有3种: 1.系统集成,分布式系统的设 ...
- RabbitMq应用一的补充(RabbitMQ的应用场景)
直接进入正题. 一.异步处理 场景:发送手机验证码,邮件 传统古老处理方式如下图 这个流程,全部在主线程完成,注册->入库->发送邮件->发送短信,由于都在主线程,所以要等待每一步完 ...
- RabbitMq应用一
RabbitMq应用一 RabbitMQ的具体概念,百度百科一下,我这里说一下我的理解,如果有少或者不对的地方,欢迎纠正和补充. 一个项目架构,小的时候,一般都是传统的单一网站系统,或者项目,三层架构 ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
- windows下 安装 rabbitMQ 及操作常用命令
rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rab ...
- RabbitMQ + PHP (三)案例演示
今天用一个简单的案例来实现 RabbitMQ + PHP 这个消息队列的运行机制. 主要分为两个部分: 第一:发送者(publisher) 第二:消费者(consumer) (一)生产者 (创建一个r ...
随机推荐
- HDU1548 奇怪的电梯(bfs求最少)
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...
- 2019-08-20 纪中NOIP模拟A组
T1 [JZOJ6310] Global warming 题目描述 给定整数 n 和 x,以及一个大小为 n 的序列 a. 你可以选择一个区间 [l,r],然后令 a[i]+=d(l<=i< ...
- 通过nginx实现多个域名访问同一个服务器
一台服务器通过nginx配置多个域名(80端口) 参考:https://www.cnblogs.com/ruanjianlaowang/p/11182486.html 1. 问题描述 多个域名对应一 ...
- vjudge 最大公约数GCD 直接求最大共约束和最小公倍数的指令
原题链接https://vjudge.net/contest/331993#problem/C 输入2个正整数A,B,求A与B的最大公约数. Input2个数A,B,中间用空格隔开.(1<= A ...
- 【C语言】求s(n)=a+aa+aaa+...+aa...a的值
原理:比如a=2,s(1)=2,s(2)=2+2*10+2,s(3)=2+2*10+2+(2*10+2)*10+2 ..... 规律: item=item*10+a sum=sum+item 代码 ...
- print不是函数
对文章阅读有感!!! 文章地址:http://www.laruence.com/2019/03/01/4904.html print是一个语法结构(language constructs), 他并不是 ...
- IntelliJ IDEA 2019.1.3 最新破解教程【最强 可用至2099年】
本文包括最新[2019.1.2]安装 和[2018.3.2](推荐)安装 ①IntelliJ IDEA 2018.3.2安装永久安装[最强] 一. 在官网下载IDEA安装包 链接:https:// ...
- RN开发-组件View,Text
1.外联js文件(RN自定义组件) module.exports=Header; 导出模块 const Header=require('./header'); 导入外部j ...
- Harvard's CS50
最近看了哈佛大学David主讲的计算机科学入门,给我一个最大的感受是计算机编程语言真的不重要,重要的是编程者的思想. 1.同一个问题可以就Scratch解决,也可以用C,用Python,用JavaSc ...
- AntDesign(React)学习-4 登录页面提交数据简单实现
github代码:https://github.com/zhaogaojian/jgdemo 全国肺炎,过节期间没地方去在家学习antd. 一.感觉antd pro项目太庞大了,可以学习下结构和代码风 ...