在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=rabbitmqspring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=lg
spring.rabbitmq.password=lg

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

@Component
public class Sender {@Autowiredprivate 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 {@RabbitHandlerpublic 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
    @BeanQueue queue() {return new Queue(queueName, false);}//注入exchange
    @BeanExchange exchange() {//return new FanoutExchange("fanout-exchange");//return new DirectExchange("direct-exchange");//return new HeadersExchange("headers-exchange");return new TopicExchange("topic-exchange");}//绑定exchange和queue
    @BeanBinding 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 {@Autowiredprivate Sender sender;@Testpublic 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

转载于:https://www.cnblogs.com/luangeng/p/6143285.html

Spring Boot 集成RabbitMQ相关推荐

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

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

  2. Spring boot集成RabbitMQ

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

  3. Spring Boot 集成 RabbitMq 实战操作(二)

    本人学习新框架方法. 一.先学习框架基本知识,也就是看这本书的前三章,了解基本概念.比如这个Rabbitmq,我会先看一些概念,比如,交换机,路由器,队列,虚拟机. 二.然后写代码,写demo,有哪些 ...

  4. Spring Boot集成RabbitMQ发送接收JSON

    ObjectMapper 最简单发送JSON数据的方式是把对象使用ObjectMapper等JSON工具类把对象转换为JSON格式,然后发送.如下: @Autowired private Object ...

  5. SpringCloud学习笔记015---Spring Boot集成RabbitMQ发送接收JSON

    在Spring Boot 集成RabbitMQ一文中介绍了如何集成RabbitMQ.默认情况下发送的消息是转换为字节码,这里介绍一下如何发送JSON数据. ObjectMapper 最简单发送JSON ...

  6. spring boot 集成sleuth

    spring boot 集成sleuth 1. 理论 1.1 sleuth是什么 1.2 sleuth有哪些 1.3 链路追踪的一些基本概念 1.4 zipkin的组成 2. zipkin 实例 2. ...

  7. Spring Boot集成Swagger导入YApi@无界编程

    接口APi开发现状 现在开发接口都要在类似YApi上写文档,这样方便不同的团队之间协作,同步更新接口,提高效率. 但是如果接口很多,你一个个手工在YApi去录入无疑效率很低. 如果是使用Spring ...

  8. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例...

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  9. 6.3 Spring Boot集成mongodb开发

    6.3 Spring Boot集成mongodb开发 本章我们通过SpringBoot集成mongodb,Java,Kotlin开发一个极简社区文章博客系统. 0 mongodb简介 Mongo 的主 ...

  10. springboot(十八):使用Spring Boot集成FastDFS

    上篇文章介绍了如何使用Spring Boot上传文件,这篇文章我们介绍如何使用Spring Boot将文件上传到分布式文件系统FastDFS中. 这个项目会在上一个项目的基础上进行构建. 1.pom包 ...

最新文章

  1. Appcan关闭主窗口
  2. 继穿越火线后的又一传奇:Final Approach
  3. vue-video-player文档_vue的video插件vue-video-player
  4. IDEA 部署项目的时候出错:Jar not loaded错误
  5. IIS7 设置读取、脚本和可执行文件的执行权限的步骤
  6. python精要(69)-turtle(1)
  7. 关于模拟量转光纤的不同实现方法与区别
  8. 常考程序 —— 笔试篇
  9. 使用Kotlin的Android CoordinatorLayout
  10. mongodb update操作
  11. MATLAB显示图像变白问题
  12. 3dmax2020软件安装教程
  13. qq android 哪个版本好用吗,Android QQ轻聊版好用吗?
  14. 【大学物理·光学】光的衍射现象 惠更斯-菲涅耳原理
  15. Ubuntu下配置FreeRadius+L2TP+MySQL,实现限制用户流量和登录人数
  16. DRV_03_编写最简单的触摸屏驱动程序_基于QEMU
  17. maven创建eclipse wtp项目
  18. STM32F4应用-串口通信
  19. 利用nodemcu和mqtt协议让嵌入式设备接入互联网(一.layui前端框架)
  20. 三季度高歌猛进,广告主为何向微博平台迁移?

热门文章

  1. js系列之每天一练成长录之一
  2. LNMP架构数据迁移到NFS存储
  3. SOS Dynamic Programming
  4. day14:磁盘管理df/du/fdisk/parted命令
  5. xshell 输入w 命令后报错 66 column window is too narrow
  6. Python多线程学习资料1
  7. Spring的基于AspectJ的AOP开发——Spring AOP(五)
  8. java 的 sort()_Java中Array.sort()的几种用法
  9. How to disable cursor positioning and text selection in an EditText? (Android)
  10. 修改element ui的table的某一列的样式