在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. VMware 连接不上XSHELL

    本人创建虚拟机时把网络连接模式选成了桥接,后来在VMware虚拟网络编辑器中查看了连接模式:NAT. 重新回到VMware中更改了连接模式:NAT模式 成功连接XSHELL 步骤1 打开VMware的 ...

  2. centOS安装apache服务器

    # yum install httpd 启动 systemctl start httpd 重启 systemctl restart httpd 停止 systemctl stop httpd

  3. MongoDB快速入门(四)- 插入文档

    插入文档 将数据插入到MongoDB集合,需要使用MongoDB 的 insert() 方法. 语法 insert()命令的基本语法如下: >db.COLLECTION_NAME.insert( ...

  4. 加和求不同的组合方式数目(dp)

    描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: 那么可能的组合有5=1+4和5=2+3和5=5三种组合方式. 输入 输入的第 ...

  5. ajax01简介

    (Asynchronous JavaScript and XML)Ajax :异步 JavaScript 和 XML,一种允许浏览器和服务器通信进行少量数据交换而无需重新加载整个网页,以实现更新部分网 ...

  6. 深入分析理解Tomcat体系结构

    Tomcat整体结构 由上图可知Tomcat的顶层容器是Server,而且一个Tomcat对应一个Server,一个server有多个service提供服务.service包含两个重要组件:Conne ...

  7. 安全的 ActiveMQ

    本章知识点 ActiveMQ 鉴权 ActiveMQ 授权 怎么创建一个自定义安全插件 使用基于证书的安全保证 简介 安全地访问消息代理以及它的 destinations 是公众关注的焦点.因此,Ac ...

  8. linux(centos)安装Nexus

    1.解压nexus压缩包 tar xvzf ./nexus-2.13.0-01-bundle.tar.gz 2.修改配置文件: 修改jetty配置 [root@localhost nexus]# vi ...

  9. python MySQLdb连接mysql失败(转载)

    最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...

  10. windows7安装PyQt5(通过pip install 安装)

    开始接触PyQt5 ,总结了一下安装的方法 默认各位已经安装好了Python环境 首先,确定一下之前没有安装过pyqt5,如果安装了,可以先卸载,避免出现意外, 之前装了几次没成功就是这种情况,卸载命 ...