在Spring Boot中整合RabbitMQ是非常容易的,通过在Spring Boot应用中整合RabbitMQ,实现一个简单的发送、接收消息的例子。

首先需要启动RabbitMQ服务,并且add一个账户lg或使用guest账户

1. 创建一个Spring Boot项目,pom如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
  6. <groupId>com.luangeng</groupId>
  7. <artifactId>boot</artifactId>
  8. <packaging>pom</packaging>
  9. <version>1.0-SNAPSHOT</version>
  10.  
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.2.RELEASE</version>
  15. </parent>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. </properties>
  21.  
  22. <dependencies>
  23.  
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-amqp</artifactId>
  27. </dependency>
  28.  
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33.  
  34. <!-- TEST -->
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. </dependencies>
  41.  
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50.  
  51. </project>

2.配置文件

  1. spring.application.name=rabbitmq
  2.  
  3. spring.rabbitmq.host=localhost
  4. spring.rabbitmq.port=5672
  5. spring.rabbitmq.username=lg
  6. spring.rabbitmq.password=lg

3.新增类Sender, 发送方,使用RabbitTemplate发送消息

  1. @Component
  2. public class Sender {
  3.  
  4. @Autowired
  5. private AmqpTemplate rabbitTemplate;
  6.  
  7. private static AtomicInteger count = new AtomicInteger(1);
  8.  
  9. public void send() {
  10. String msg = "msg" + count.getAndIncrement() + " at " + new Date();
  11. System.out.println("Sender : " + msg);
  12. this.rabbitTemplate.convertAndSend(RabbitConfig.queueName, msg);
  13. }
  14. }

4.新增类Receive, 消息接受方,使用@RabbitListener包装一个Queue

  1. import org.springframework.amqp.rabbit.annotation.RabbitHandler;
  2. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  3. import org.springframework.stereotype.Component;
  4.  
  5. @Component
  6. @RabbitListener(queues = RabbitConfig.queueName)
  7. public class Receiver {
  8.  
  9. @RabbitHandler
  10. public void process(String msg) {
  11. System.out.println("Receiver : " + msg);
  12. }
  13.  
  14. }

5.Rabbit配置类,作用是初始化Queue和Exchange等

  1. import org.springframework.amqp.core.*;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4.  
  5. @Configuration
  6. public class RabbitConfig {
  7.  
  8. public final static String queueName = "myQueue";
  9.  
  10. //注入queue
  11. @Bean
  12. Queue queue() {
  13. return new Queue(queueName, false);
  14. }
  15.  
  16. //注入exchange
  17. @Bean
  18. Exchange exchange() {
  19. //return new FanoutExchange("fanout-exchange");
  20. //return new DirectExchange("direct-exchange");
  21. //return new HeadersExchange("headers-exchange");
  22. return new TopicExchange("topic-exchange");
  23. }
  24.  
  25. //绑定exchange和queue
  26. @Bean
  27. Binding binding(Queue queue, TopicExchange exchange) {
  28. return BindingBuilder.bind(queue).to(exchange).with(queueName);
  29. }
  30.  
  31. }

6.主类

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class, args);
  5. }
  6. }

7.测试类

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = Application.class)
  3. public class ApplicationTest {
  4.  
  5. @Autowired
  6. private Sender sender;
  7.  
  8. @Test
  9. public void hello() throws Exception {
  10. while(true) {
  11. sender.send();
  12. Thread.sleep(1000);
  13. }
  14. }
  15.  
  16. }

---

运行测试类,控制台输出如下:

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

  1. Spring Boot 集成 RabbitMQ 实战

    Spring Boot 集成 RabbitMQ 实战 特别说明: 本文主要参考了程序员 DD 的博客文章<Spring Boot中使用RabbitMQ>,在此向原作者表示感谢. Mac 上 ...

  2. Spring boot集成RabbitMQ(山东数漫江湖)

    RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...

  3. 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】

    这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用Rabbit ...

  4. RabbitMQ(3) Spring boot集成RabbitMQ

    springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持. 资源代码:练习用的代码. ...

  5. spring boot集成RabbitMQ

    原文:https://www.jianshu.com/p/e1258c004314 RabbitMQ作为AMQP的代表性产品,在项目中大量使用.结合现在主流的spring boot,极大简化了开发过程 ...

  6. spring boot 集成 rabbitmq 指南

    先决条件 rabbitmq server 安装参考 一个添加了 web 依赖的 spring boot 项目 我的版本是 2.5.2 添加 maven 依赖 <dependency> &l ...

  7. spring boot 集成 rabbitmq

    1.使用默认的AmqpTemplate生产消费pojo时,pojo需要implement Serializable,否则会抛出org.springframework.amqp.AmqpExceptio ...

  8. spring boot 集成RabbitMQ的异常

    com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.clos ...

  9. Spring boot集成Rabbit MQ使用初体验

    Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...

随机推荐

  1. 基于Visual c++ 2012的php扩展开发 - HelloWord!

    1.cmd进入命令行模式,并进入php-5.6.20-src/ext源代码的ext目录下输入命令php ext_skel_win32.php --extname=HelloWord,执行结果如下图: ...

  2. tcp底层连接过程(c语言)

    在用了多种上位机开发环境,包括mfc.Qt.C#之后,发现它们的API都是对底层协议的(可以说是C语言)的封装,所以了解了底层协议,任意换上位机开发环境都是没问题的. 1.服务器创建套接字socket ...

  3. Yii技巧大全(摘录)

    Yii技巧大全(摘录) db组件 'schemaCachingDuration'=>3600, 为什么不起做用? 需要开缓存 如何在页面下边显示sql的查询时间 在log组件的routes中加入 ...

  4. codeforces 439C 模拟

    http://codeforces.com/problemset/problem/439/C 题意:给你n个数,分成k个非空集合,其中有p个集合的元素和为偶数,其余k-p个集合的元素和为奇数. 思路: ...

  5. input ajax自动补全

    页面 <div class="manage-Car-add-info-sn"> <p style="width:25%; height:70%;floa ...

  6. java深入探究09-Filter,Listener,国际化

    1.Filter过滤器 1)为是么有过滤器 开发项目中经常遇到直接登录主页面要判断用户是否合法,这类代码比较重复,可以通过过滤器来解决 2)过滤器原理生命周期 服务器创建过滤器对象->一个执行i ...

  7. C# 反射通过GetCustomAttributes方法,获得自定义特性

    http://blog.csdn.net/litao2/article/details/17633107 使用反射访问: 自定义属性的信息和对其进行操作的方法. 一.实例1 1.代码: 如:Syste ...

  8. bind的原生代码实现

    <script> function foo(p1,p2) { this.val = p1 + p2; } var bar = foo.bind(this, "p1"); ...

  9. Codeforces Round #283 (Div. 2) E. Distributing Parts 贪心+set二分

    E. Distributing Parts time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  10. RocketMQ原理讲解系列文章

    [RocketMQ原理解析][http://blog.csdn.net/quhongwei_zhanqiu/article/category/2548637] [消息的可靠性.顺序和重复][https ...