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

  1. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

  2. RabbitMq应用二

    在应用一中,基本的消息队列使用已经完成了,在实际项目中,一定会出现各种各样的需求和问题,rabbitmq内置的很多强大机制和功能会帮助我们解决很多的问题,下面就一个一个的一起学习一下. 消息响应机制 ...

  3. 如何优雅的使用RabbitMQ

    RabbitMQ无疑是目前最流行的消息队列之一,对各种语言环境的支持也很丰富,作为一个.NET developer有必要学习和了解这一工具.消息队列的使用场景大概有3种: 1.系统集成,分布式系统的设 ...

  4. RabbitMq应用一的补充(RabbitMQ的应用场景)

    直接进入正题. 一.异步处理 场景:发送手机验证码,邮件 传统古老处理方式如下图 这个流程,全部在主线程完成,注册->入库->发送邮件->发送短信,由于都在主线程,所以要等待每一步完 ...

  5. RabbitMq应用一

    RabbitMq应用一 RabbitMQ的具体概念,百度百科一下,我这里说一下我的理解,如果有少或者不对的地方,欢迎纠正和补充. 一个项目架构,小的时候,一般都是传统的单一网站系统,或者项目,三层架构 ...

  6. 缓存、队列(Memcached、redis、RabbitMQ)

    本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...

  7. 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)

    Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...

  8. windows下 安装 rabbitMQ 及操作常用命令

    rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rab ...

  9. RabbitMQ + PHP (三)案例演示

    今天用一个简单的案例来实现 RabbitMQ + PHP 这个消息队列的运行机制. 主要分为两个部分: 第一:发送者(publisher) 第二:消费者(consumer) (一)生产者 (创建一个r ...

随机推荐

  1. jQuery---内容复习

    内容复习 1. 操作样式 (5) 1.1 css操作 设置单个样式 设置多个样式 获取样式 css(name, value) :设置单个样式 css(obj):设置多个样式 css(name):获取样 ...

  2. contos7 用户管理相关操作命令

    # 查看用户列表 cut -d : -f 1 /etc/passwd # 查看可以登录系统的用户 cat /etc/passwd | grep -v /sbin/nologin | cut -d : ...

  3. SpringBoot之Configuration

      在SpringBoot中可以通过@Configuration对某个类注解将该类申明为配置类,以此在代替先前spring版本中配置xml中的功能,并且增加了可读性与维护性.并且在注解类中的类方法中可 ...

  4. 性能优化-css,js的加载与执行

    前端性能优化 css,js的加载与执行 javascript是单线程的 一个网站在浏览器是如何进行渲染的呢? html页面加载渲染的过程 html渲染过程的一些特点 顺序执行,并发加载 词法分析 并发 ...

  5. upload-labs打关详解

    1-19关 00x01 JS检查 方法一.修改javascript代码,将.php添加到允许上传的类型中 3.上传成功 方法二:绕过前端,通过burpsuit抓包,上传一张info.jpg图片,然后抓 ...

  6. jeecgboot数据字典使用

    jeecgboot数据字典使用 input页面下拉框使用 效果展示 实现 定义数据字典 引用并调用JDictSelectTag组件 import JDictSelectTag from '@/comp ...

  7. Myeclipse连接Mysql数据库时报错:Error while performing database login with the pro driver:unable

    driver template: Mysql connector/j(下拉框进行选择) driver name: 任意填,最好是数据库名称,方便查找 connection URL: jdbc:mysq ...

  8. git本地仓库远程仓库地址更改

    git remote rm origingit remote add origin git@52.82.8.87:iot3.0-service/test.gitgit push -u origin - ...

  9. 洛谷 P3796 【模板】AC自动机(加强版)(AC自动机)

    题目链接:https://www.luogu.com.cn/problem/P3796 AC自动机:复杂度$O( (N+M)\times L )$,N为模式串个数,L为平均长度,M为文章长度. ins ...

  10. TCL 包

    包用于创建代码的可重用单元. 程序包提供特定功能的文件集合. 1.创建代码 2.创建包index 打开tclsh,切换到HelloWorld目录,并使用pkg_mkindex 命令创建索引文件. %c ...