RabbitMQ和SpringBoot的简单整合列子
一 思路总结
1 主要用spring-boot-starter-amqp来整合RabbitMQ和SpringBoot
2 使用spring-boot-starter-test来进行单元测试
3编写配置文件application.yml
spring:
application:
name: rabbitmq-hello
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
4 编写主类Application.class
5 编写Sender并自动扫面,以备自动注入
@Component
public class Sender {
@Autowired
private AmqpTemplate amqpTemplate; public void send() throws Exception {
String context = "hello" + new Date();
System.out.println("Sender:"+context);
this.amqpTemplate.convertAndSend("hello",context);
}
}
6 编写Reveiver并自动扫面同时监听队列hello,并用RabbitHandler来处理请求。
@Component
@RabbitListener(queues = "hello")
public class Receiver { @RabbitHandler
public void process(String hello){
System.out.println("Receiver:"+hello);
}
}
7 编写刚刚用到的hello队列的配置类
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}
8 编写单元测试类Test,调用Sender的方法发送message,这样Receiver就能自动监听并在主类哪里输出了
@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class Test { @Autowired
private Sender sender; @org.junit.Test
public void hello() throws Exception {
sender.send();
}
}
9 结果


RabbitMQ和SpringBoot的简单整合列子的更多相关文章
- RabbitMQ(三):RabbitMQ与Spring Boot简单整合
RabbitMQ是目前非常热门的一款消息中间件,不管是互联网大厂还是中小企业都在大量使用.Spring Boot的兴起,极大地简化了Spring的开发,本文将使用Spring Boot与RabbitM ...
- springboot vue简单整合
1.vue项目 (1)修改config/index.js (2)执行 npm run build 生成静态文件,在dist目录 2.springboot项目 (1)在src/main/resource ...
- 快速搭建springboot框架以及整合ssm+shiro+安装Rabbitmq和Erlang、Mysql下载与配置
1.快速搭建springboot框架(在idea中): file–>new project–>Spring Initializr–>next–>然后一直下一步. 然后复制一下代 ...
- Springboot与MyBatis简单整合
之前搭传统的ssm框架,配置文件很多,看了几天文档才把那些xml的逻辑关系搞得七七八八,搭起来也是很麻烦,那时我完全按网上那个demo的版本要求(jdk和tomcat),所以最后是各种问题没成功跑起来 ...
- SpringBoot学习之整合Druid的简单应用
一.Druid介绍 Druid简介 Druid是目前Java语言中最好的数据库连接池之一.结合了 C3P0.DBCP 等 DB 池的优点,同时加入了日志监控.Druid 是一个分布式的.支持实时多维 ...
- SpringBoot简单整合redis
Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...
- 【spring boot】SpringBoot初学(8)– 简单整合redis
前言 到目前为止,把项目中需要用到的:properties读取.数据源配置.整合mybatis/JdbcTemplate.AOP.WebService.redis.filter.interceptor ...
- 带着新人学springboot的应用08(springboot+jpa的整合)
这一节的内容比较简单,是springboot和jpa的简单整合,jpa默认使用hibernate,所以本质就是springboot和hibernate的整合. 说实话,听别人都说spring data ...
- RabbitMQ与Spring的框架整合之Spring Cloud Stream实战
1.RabbitMQ与Spring Cloud Stream整合实战.SpringCloud Stream整体结构核心概念图,如下所示: 图示解释:Outputs输出,即消息的发送端.Inputs输入 ...
随机推荐
- C#微信公众号/订阅号开发 接口源码
using System; using System.Web; using System.IO; using System.Text; using System.Web.Security; using ...
- 使用jquery.form.js文件进行文件上传
本想着文件上传是一件挺简单的事,不过是获取文件地址保存到服务器而已,然而事实并非如此. 我信心满满的写下input type="file",alert input 的value,打 ...
- vs2012中使用localdb实例还原一个sql server 2008r2版本的数据库
use localdb sometime is easy than sql server ,and always use visual studio make you stupid. vs2012中还 ...
- #tensorflow入门(1)
tensorflow入门(1) 关于 TensorFlow TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操 ...
- 解决由于VNC日志导致服务器磁盘100%
今天通过SSH连接服务器看到磁盘直接100%了.于是通过 sudo du -h --max-depth=1 发现某个用户下面占用了100个G.于是切换进去看了一下.发现VNC的log占满了整个磁盘.然 ...
- vim搭建笔记
在接触vim近一年后,自己的vimrc都是拼凑别人的,所以有很多插件和配置并不会使用 现在,我决定,花费一天时间,一步一步的搭建自己的vim配置! 去该网址下载安装vim http://www.vim ...
- struts2使用模型传值
用户bean package userBeans; public class User { private String username; public String getUsername() { ...
- Mongodb的mongostat命令
Mongodb的mongostat命令可实时(1秒钟刷新一次)显示Mongodb数据库的运行情况,可视为性能监视器. 1.启动命令:authenticationDatabase表示用户认证证书所在的数 ...
- bootstrap 鼠标悬停显示
1. <button type="button" rel="drevil" data-content="报名截止时间:'+time+'" ...
- MySql sql按时间分组
select DATE_FORMAT(f.upload_time,'%Y%u') weeks,count(*),sum(p.download_times),sum(p.collection_times ...