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

  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. BYTE、HANDEL、DWORD等的定义

    有时候引入第三方库之后,可能会存在标题的这些变量没有定义,原来这些变量都定义在windows.h里面,包含进去就行了(Qt的MSVC编译器)

  2. etcd 安装部署

    etcd 是coreos团队开发的分布式服务发现键值存储仓库. github地址: https://github.com/coreos/etcd 安装: 1.下载etcd最新版本 https://gi ...

  3. 服务器windows 2003 安装SQL 2000+SP4

    (在windows 2003安装SQL 2000,对于03系统是没有32位和64位之分的) (32位系统需要重启机器,64不需要) 在站点下载数据库 解压后有两个文件夹 首先安装SQL2000 第1步 ...

  4. nodejs mysql 操作数据库方法二

    node.js 开发指南 – Node.js 连接 MySQL 并进行数据库操作 通常在NodeJS开发中我们经常涉及到操作数据库,尤其是 MySQL ,作为应用最为广泛的开源数据库则成为我们的首选, ...

  5. 【整理】C#文件操作大全(SamWang)

    [整理]C#文件操作大全(SamWang) 文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msd ...

  6. UOJ222 【NOI2016】区间

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. libvirt cpu mode

    libvirt中 cpu mode可以有以下3种: custom : 该模式下cpu element用来描述guest可见的CPU,该模式也是mode的default模式,它会使得persistent ...

  8. JVM内存管理基础概念

    .内存的不同形态 物理内存 虚拟内存 .内存的使用形式 内核空间 用户空间 .java虚拟机运行时数据划分 PC寄存器:保存当前程序运行时的内存地址. Java栈:总是和线程关联,每个线程拥有一个ja ...

  9. NSRegularExpression iOS自带的正则表达式

    以前做验证邮箱,电话号码的时候通常用第三方的正则表达式或者NSPredicate(点这里查看以前的文章),在后期,苹果推出了自己的正则表达式来提供给开发者调用,很方便,功能也强大. 具体可以查看官方文 ...

  10. Tensorflow搭建神经网络及使用Tensorboard进行可视化

    创建神经网络模型 1.构建神经网络结构,并进行模型训练 import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt ...