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 ...
随机推荐
- java中的特殊有用类
1.MessageDigest:类似与md5加密算法应用的功能类
- nginx流量全copy记录
参考:http://tyrion.iteye.com/blog/2311987 准备两台服务器: 0.0.0.1 0.0.0.2 在 0.0.0.1上 . 下载 wget https://github ...
- volist/foreach下,点击循环中的一个进行操作
第一种方法,是给点击元素绑定事件,用ajax将值传到控制器中,其中传的值,用jquery选择器选择值. 1.在html中 <foreach name="save" item= ...
- PowerDesigner(一)-PowerDesigner概述(系统分析与建模)
PowerDesigner概述 PowerDesigner是Sybase公司推出的一个集成了企业架构,UML(统一建模语言)和数据库的CASE(计算机辅助软件工程)工具.它不仅可以用于系统设计和开发 ...
- 自动化收集SQLSERVER诊断信息
自动化收集SQLSERVER诊断信息 相信很多人都遇到过当SQLSERVER出现问题的时候,而你又解决不了需要DBA或者微软售后支持工程师 去帮忙解决问题,那么他们一般需要你收集一些系统信息和SQ ...
- SARG
SARG (Searchable Arguments)操作,因为它通常是指一个特定的匹配,一个值得范围内的匹配或者两个以上条件的AND连接. 中文名 SARG 全 称 Searchable ...
- SSIS之序列容器
序列容器是一种十分简单,但使用非常广泛的容器,它可以对控制流的任务流组件进行结构化处理,把一些业务相关的任务组件,放到一个序列容器中,使整个包看起来更加整洁.美观,就如同我们家里的书柜.衣柜似的,把不 ...
- UML用例建模解析(一)----------用例概述
UML(统一建模语言): 1. 绘制用例图 用例图是UML中比较简单的一种图形,它包含两个主要组成元素,分别是执行者(Actor)和用例(Use Case).执行者又称为参与者或角色,用例又称为用况或 ...
- c++中对齐方式
使用cout<<setiosflags(ios::right)以后, 再调用 cout<<setiosflags(ios::left);无效的, 你可以在调用之前加上一句 co ...
- tableau 常识积累
没怎么在业务系统中使用过,所以需要好好积累.看起来很简单的东西都需要慢慢来用.下了一份它的官方文档10.3版本的.公司网络限制,不能去它官网学习.只有下班时间了. 先说一个,有时候度量值它和HANA类 ...