Spring Boot 集成RabbitMQ
在Spring Boot中整合RabbitMQ是非常容易的,通过在Spring Boot应用中整合RabbitMQ,实现一个简单的发送、接收消息的例子。
首先需要启动RabbitMQ服务,并且add一个账户lg或使用guest账户
1. 创建一个Spring Boot项目,pom如下
<?xml version="1.0" encoding="UTF-8"?>
<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"> <groupId>com.luangeng</groupId>
<artifactId>boot</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- TEST -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2.配置文件
spring.application.name=rabbitmq spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=lg
spring.rabbitmq.password=lg
3.新增类Sender, 发送方,使用RabbitTemplate发送消息
@Component
public class Sender { @Autowired
private AmqpTemplate rabbitTemplate; private static AtomicInteger count = new AtomicInteger(1); public void send() {
String msg = "msg" + count.getAndIncrement() + " at " + new Date();
System.out.println("Sender : " + msg);
this.rabbitTemplate.convertAndSend(RabbitConfig.queueName, msg);
}
}
4.新增类Receive, 消息接受方,使用@RabbitListener包装一个Queue
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(queues = RabbitConfig.queueName)
public class Receiver { @RabbitHandler
public void process(String msg) {
System.out.println("Receiver : " + msg);
} }
5.Rabbit配置类,作用是初始化Queue和Exchange等
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class RabbitConfig { public final static String queueName = "myQueue"; //注入queue
@Bean
Queue queue() {
return new Queue(queueName, false);
} //注入exchange
@Bean
Exchange exchange() {
//return new FanoutExchange("fanout-exchange");
//return new DirectExchange("direct-exchange");
//return new HeadersExchange("headers-exchange");
return new TopicExchange("topic-exchange");
} //绑定exchange和queue
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
} }
6.主类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
7.测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest { @Autowired
private Sender sender; @Test
public void hello() throws Exception {
while(true) {
sender.send();
Thread.sleep(1000);
}
} }
---
运行测试类,控制台输出如下:
Receiver : msg514 at Sun Aug 06 11:21:03 CST 2017
Sender : msg515 at Sun Aug 06 11:21:04 CST 2017
Receiver : msg515 at Sun Aug 06 11:21:04 CST 2017
Sender : msg516 at Sun Aug 06 11:21:05 CST 2017
Receiver : msg516 at Sun Aug 06 11:21:05 CST 2017
Sender : msg517 at Sun Aug 06 11:21:06 CST 2017
Receiver : msg517 at Sun Aug 06 11:21:06 CST 2017
Sender : msg518 at Sun Aug 06 11:21:07 CST 2017
Receiver : msg518 at Sun Aug 06 11:21:07 CST 2017
此时Rabbit管理页面显示:


说明消息通过RabbitMQ发送接收成功。
end
Spring Boot 集成RabbitMQ的更多相关文章
- Spring Boot 集成 RabbitMQ 实战
Spring Boot 集成 RabbitMQ 实战 特别说明: 本文主要参考了程序员 DD 的博客文章<Spring Boot中使用RabbitMQ>,在此向原作者表示感谢. Mac 上 ...
- Spring boot集成RabbitMQ(山东数漫江湖)
RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...
- 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】
这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...
- RabbitMQ(3) Spring boot集成RabbitMQ
springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持. 资源代码:练习用的代码. ...
- spring boot集成RabbitMQ
原文:https://www.jianshu.com/p/e1258c004314 RabbitMQ作为AMQP的代表性产品,在项目中大量使用.结合现在主流的spring boot,极大简化了开发过程 ...
- spring boot 集成 rabbitmq 指南
先决条件 rabbitmq server 安装参考 一个添加了 web 依赖的 spring boot 项目 我的版本是 2.5.2 添加 maven 依赖 <dependency> &l ...
- spring boot 集成 rabbitmq
1.使用默认的AmqpTemplate生产消费pojo时,pojo需要implement Serializable,否则会抛出org.springframework.amqp.AmqpExceptio ...
- spring boot 集成RabbitMQ的异常
com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.clos ...
- Spring boot集成Rabbit MQ使用初体验
Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...
随机推荐
- html文件转换成pdf和word
1.html文件转成pdf 采用jar包有itext-asian.jar.itextpdf-5.5.5.jar.itext-pdfa-5.5.5.jar.itext-xtra-5.5.5.jar,为了 ...
- 使用Shell脚本查找程序对应的进程ID,并杀死进程
#!/bin/sh NAME='shell.php' echo $NAME ID=`ps -ef | grep "$NAME" | grep -v "$0" | ...
- P2610 【[ZJOI2012]旅游】(dfs+树的直径)
楼下那篇题解说实话就是什么都没说,所以我再发一篇正常一点的. 楼下思路大体是正确的,但是之所以是说什么都没说,是因为他有两个比较致命的遗漏.首先是点,这里的点不是平时我们认为的点,如果多少接触过对偶图 ...
- json数据的拼接与解析
json数据格式 [{ "firstName": "Brett", "lastName":"McLaughlin", & ...
- SpringCloud-分布式配置中心(config)
简介 在分布式文件系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- QT 使用QTcpServer QTcpSocket 建立TCP服务器端 和 客户端
1. 如图客户端连接server后,server发送"hello tcp" 给客户端 2. 实例代码 ----------------------------------- s ...
- Flume-NG启动过程源码分析(三)(原创)
上一篇文章分析了Flume如何加载配置文件的,动态加载也只是重复运行getConfiguration(). 本篇分析加载配置文件后各个组件是如何运行的? 加载完配置文件订阅者Application类会 ...
- ios 获取导航栏和状态栏高度,针对iPhoneX
因为iPhoneX的产生,原本的导航栏再也不是44了,那么会出现什么问题呢?在隐藏导航栏的页面,原本的高度要向上调整一个导航栏+状态栏的高度.那么iPhoneX的导航栏高度不是44了,我们需要手动获取 ...
- 配置JSP模板
- SSAS——基础--cube
SSAS——基础 一.Analysis Services Analysis Services是用于决策支持和BI解决方案的数据引擎.它提供报表和客户端中使用的分析数据. 它可在多用途数据模型中创建 ...