85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】
这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用RabbitMQ,那么本节分如下几个步骤:
(1) 新建Maven Java Project;
(2) 在pom.xml添加相关依赖;
(3) 编程+测试
(4) 配置信息
接下来看看每个步骤是怎么操作的。
(1) 新建Maven Java Project;
新建一个Maven Java Project项目,取名为spring-boot-rabbitmq
(2) 在pom.xml添加相关依赖;
这里需要加入基本的依赖以及rabbitmq相关的依赖,具体如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kfit</groupId>
<artifactId>spring-boot-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-rabbitmq</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk版本号,Angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->
<java.version>1.8</java.version>
</properties>
<!--
spring boot 父节点依赖,
引入这个之后相关的引入就不需要添加version配置,
spring boot会自动选择最合适的版本进行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
(3) 编程+测试
在这里我们为了方便直接在启动类App.java进行编码,先提供代码:
package com.kfit;
import java.util.Date;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年8月23日
*/
@SpringBootApplication
@EnableScheduling//启用任务调度.
@RabbitListener(queues="foo")//启用Rabbit队列监听foo key.
public class App {
//rabbit操作类;
@Autowired
private RabbitTemplate rabbitTemplate;
@Scheduled(fixedDelay=3000)//3s执行1次此方法;
public void send(){
rabbitTemplate.convertAndSend("foo","zhang");
}
@Bean
public Queue fooQueue(){
returnnew Queue("foo");
}
//接收到消息处理.
@RabbitHandler
public void onMessage(@Payload String foo){
System.out.println(" >>> "+new Date() + ": " + foo);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
好了,到这里就可以运行测试了,观察控制台的打印信息:
>>> Tue Aug 23 15:06:54 CST 2016: zhang
>>> Tue Aug 23 15:06:57 CST 2016: zhang
>>> Tue Aug 23 15:07:00 CST 2016: zhang
看到如上信息,恭喜你简单的例子编写成功了。好了,上面的源代码还是有必要进行简单的讲解下。
首先我们需要清除RabbitMQ的话有这么几个角色,消息生产者或者说消息提供者(sender);其次就是消息队列提供存放消息的对方(queue);消息消费者或者说消息接收者(receiver)。知道这些概念之后在看代码就好理解很多了。
我们先看消息提供者:send()方法,这里使用Spring 提供的RabbitTemplate 进行操作消息,调用convertAndSend方法将消息发布到对应的消息频道上,这里也就是使用Queue进行存储;然后我们注意到这个方法不是我们人为执行的方法而是采用定时执行的方式进行3s一次进行发布消息,所以你会看到send方法上有一个注解@Scheduled,那么使用该注解的话,相应的类上上面就需要@EnableScheduling启用注解。
在上面的分析中我们说消息被发布到队列中,所以我们需要创建一个Queue进行存放,这里需要注意Queue的包路径是org.springframework.amqp.core.Queue。
这里我们注入了fooQueue 方法指定队列的名称是foo,那么此队列只会处理key为foo的消息,其它的key不会处理。
最后就是消息的接收者了:这里我们使用了@RabbitHandler定义了一个消息接收者onMessage,其中这个方法名可以随意取名,再者我们需要启用Rabbit监听,所以需要在类上添加@RabbitListener,这样编写完就能按照我们的想法执行了。那么如果我们编写了两个消息接收者的话,可以正常运行嘛,两个接收者都能接收到消息嘛,答案是肯定的,因为我们以上的编码是发布(Pub)/订阅(Sub)消息模型。
很有意思的是,这个方法还可以在简化下,将类上的@RabbitListener直接转移到方法上,去掉@RabbitHandler注解,直接为如下:
//接收到消息处理.
@RabbitListener(queues = "foo")
public void onMessage(@Payload String foo){
System.out.println(" >new>> "+new Date() + ": " + foo);
}
(4) 配置信息
在以上的代码你会发现我们根本没有添加什么配置信息,所以spring boot提供了默认的配置,那么我们应该怎么修改配置呢,只需要在application.properties进行配置即可:
# RABBIT (RabbitProperties)
spring.rabbitmq.host= # connection host
spring.rabbitmq.port=# connection port
spring.rabbitmq.addresses= # connection addresses (e.g. myhost:9999,otherhost:1111)
spring.rabbitmq.username= # login user
spring.rabbitmq.password= # login password
spring.rabbitmq.virtualhost=
spring.rabbitmq.dynamic=
这里可以配置主机地址,端口号,用户名密码(默认使用guest/guest)。
85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】的更多相关文章
- 63.JPA/Hibernate/Spring Data概念【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 事情的起源,无意当中在一个群里看到这么一句描述:"有人么?默默的问一句,现在开发用mybatis还是hibernate还是jpa&quo ...
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
(提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...
- 20. Spring Boot Servlet【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52069482 Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可 ...
- (20)Spring Boot Servlet【从零开始学Spring Boot】
Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用Spring-Boot时,嵌 ...
- 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】
[原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...
- (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...
- 57. Spring 自定义properties升级篇【从零开始学Spring Boot】
之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...
- 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...
- 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
随机推荐
- js js弹出框、对话框、提示框、弹窗总结
js弹出框.对话框.提示框.弹窗总结 一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹 ...
- 一个简易的Http请求转发器
这两天一直再看微信开发,临时在我的电脑搭了个IIS服务器做微信开发,外网也能访问了,关键是,调试太麻烦了!! 我写完代码,要将代码发布到IIS才能接收微信消息,可是在这个过程中,我不知道微信发过来的是 ...
- div 绝对定位
div绝对居下 .Phone2title{ width:%; height:30px; line-height:30px; /*text-align:left;*/ /*background-colo ...
- linux安装redis官方教程
官方链接:http://redis.io/download Download, extract and compile Redis with: $ wget http://download.redis ...
- ios 自定义加载动画效果
在开发过程中,可能会遇到各种不同的场景需要等待加载成功后才能显示数据.以下是自定义的一个动画加载view效果. 在UIViewController的中加载等到效果,如下 - (void)vi ...
- Gitlab User Guide
Installation Configuration Set user name and email Add SSH keys Repository Create New Repository Clo ...
- 洛谷 P2617 Dynamic Ranking
题目描述 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤ ...
- codevs 1606 台阶
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 话说某牛家门外有一台阶,这台阶可能会很高(总层数<=1000000). 这 ...
- CAD控件界面显示与隐藏(网页版)
控件界面工具栏的显示或隐藏,js代码实现如下: 1 2 3 4 5 6 7 8 9 //隐藏/显示工具栏 mxOcx.ShowToolBar("常用工具",isShow ...
- 获取url请求的参数值
function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '( ...