spring-boot rabbitMq 完整项目搭建,包括创建、发送、监听
写在开始
rabbitMq 代码按照三部分介绍
第一部分 交换机和队列的创建
第二部分 消息发送
第三部分 消息监听
第一部分
1 建立queue
2 建立exchange
3 exchange绑定queue
建立之前需要配置两样东西
一个是rabbitMq的连接工厂(ConnectionFactory)、另外一个是操作句柄(RabbitAdmin)。可以看到连接工厂是给操作句柄初始化时使用的。
后续创建队列等一系列操作都需要使用到操作句柄,如果没有使用的话操作被视为无效。
// 初始化连接
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1", 5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
} // 队列操作配置
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
开始建立队列
public final static String QUEUE_NAME = "tianmh-queue";
public final static String QUEUE_NAME2 = "tianmh-queue2"; @Autowired
private RabbitAdmin rabbitAdmin; // 创建队列1
@Bean(value = QUEUE_NAME)
public Queue queue() {
Queue queue = new Queue(QUEUE_NAME, true, true, true);
this.rabbitAdmin.declareQueue(queue);
return queue;
} // 创建队列2
@Bean(value = QUEUE_NAME2)
public Queue queue2() {
Queue queue = new Queue(QUEUE_NAME2, true, true, true);
this.rabbitAdmin.declareQueue(queue);
return queue;
}
建立交换机。下面例子建立的队列为广播类型队列
// 创建一个 Fanout 类型的交换器
@Bean(value = EXCHANGE_NAME)
public Exchange exchange() {
Exchange exchange = new FanoutExchange(EXCHANGE_NAME, true, true);
this.rabbitAdmin.declareExchange(exchange);
return exchange;
}
交换机绑定队列
// 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange)
@Bean
public Binding binding(@Qualifier(QUEUE_NAME) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
Binding binding = new Binding(queue.getName(), Binding.DestinationType.QUEUE, exchange.getName(), ROUTING_KEY, null);
this.rabbitAdmin.declareBinding(binding);
return binding;
} // 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange)
@Bean
public Binding binding2(@Qualifier(QUEUE_NAME2) Queue queue, @Qualifier(EXCHANGE_NAME) Exchange exchange) {
Binding binding = new Binding(queue.getName(), Binding.DestinationType.QUEUE, exchange.getName(), ROUTING_KEY, null);
this.rabbitAdmin.declareBinding(binding);
return binding;
}
第二部分 消息发送
消息发送需指定发送到的exchangeName及routeKey及内容
@Component
public class SenderDemo {
@Autowired
RabbitTemplate rabbitTemplate; @PostConstruct
public void testSender() {
TestCommand command = new TestCommand();
command.setKey("testContent");
byte[] content = JSONObject.toJSONBytes(command);
MessageProperties messageProperties = new MessageProperties();
messageProperties.setTimestamp(new Date());
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
Message message = new Message(content, messageProperties);
rabbitTemplate.send(RabbitMqConfig.EXCHANGE_NAME, "log", message);
}
}
第三部分 消息监听
接收消息是通过监听队列实现的
@RabbitListener(queues = RabbitMqConfig.QUEUE_NAME)
public void process(Message message) {
TestCommand command = JSON.parseObject(new String(message.getBody()), TestCommand.class);
logger.info("接收处理队列[{}]的消息[{}]", RabbitMqConfig.QUEUE_NAME, command.toString());
}
就此一个完整的RabbitMqDemo搭建完成
附带项目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">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>rabbitmq</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</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-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
spring-boot rabbitMq 完整项目搭建,包括创建、发送、监听的更多相关文章
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例
下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...
- 基于Spring boot的web项目搭建教程(一)
前言: 本教程参考了大量前辈的代码,在此不方便一一列举.本教程使用IDEA开发工具搭建项目,对于本人的IDEA已经集成了某些插件,比如Lombok,Thymeleaf,yml等插件,这些插件不在文中提 ...
- 一 、Spring Boot 学习之项目搭建
一.简介 spring 官方网站本身使用Spring 框架开发,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置文件以及复杂的Bean依赖关系. 随着Spring 3.0的发布,Spring ...
- spring boot rabbitmq 多MQ配置 自动 创建 队列 RPC
源码地址:https://github.com/hutuchong518/RabbitmqStudy 需求: spring boot 整合 rabbitmq rpc功能, 需要将 请求和响应 ...
- Spring Boot学习笔记:ApplicationEvent和ApplicationEventListener事件监听
采用事件监听的好处 以用户注册的业务逻辑为例,用户在填写完信息表单后,提交信息到后台,后台对用户信息进行处理,然后给用户返回处理结果的信息. 如上图所示,用户在注册时,后台需要处理一些系列流程,实际业 ...
- spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途
Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...
- spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包
下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...
- Spring Boot 多模块项目创建与配置 (一) (转)
Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...
- Spring Boot 多模块项目创建与配置 (一)
最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...
随机推荐
- 集合之Stack
在Java中Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构,它采用典型的先进后出的操作方式完成的.每一个栈都包含一个栈顶,每次出栈是将栈顶的数据取出,如下: Stack通过 ...
- beta冲刺后续讨论
目录 组员:胡绪佩 组员:何家伟 组员:黄鸿杰 组员: 翟丹丹 组员:周政演 组员:胡青元 组员:庄卉 组员:刘恺琳 组员:何宇恒 组员:刘一好 组员:葛家灿 组员:胡绪佩 总结 通过这次的Beta版 ...
- select * from * with ur
DB2中,共有四种隔离级:RS,RR,CS,UR,DB2提供了这4种不同的保护级别来隔离数据.隔离级是影响加锁策略的重要环节,它直接影响加锁的范围及锁的持续时间.两个应用程序即使执行的相同的操作,也可 ...
- CPP/类/成员函数访问权限
- C++学习第一天(helloword)
C++编译过程 #include <iostream> //iostream 提供了一个叫命名空间的东西,标准的命名空间是std 包含了有关输入输出语句的函数 // input&^ ...
- mfc this指针
知识点 this指针 this指针使用 一.this指针 this指针可以看成是实例化对象的地址.在类成员函数里访问成员变量,其实也隐含使用了this指针. 在 Tdate中this->相当于T ...
- 【搭建RAC报错】搭建RAC,第二个节点执行root.sh报错:CRS-2800、CRS-4000
Creating /etc/oratab file...Entries will be added to the /etc/oratab file as needed byDatabase Confi ...
- Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件
一. Spring Auto Scanning Components —— 自动扫描组件 1. Declares Components Manually——手动配置componen ...
- 使用efwplusScript开发Winform程序【像小程序那样开发PC软件】
一.前言 本人从事多年医疗管理软件的开发,在医院大多数的软件都还是CS程序,BS程序很少,对于使用者来说CS的操作体验确实比BS的要好. 1.CS的缺点就是升级麻烦,每次有新版本都需要所有客户端操作升 ...
- win10家庭版没有组策略怎么办?(win10管理员已阻止你运行此应用”解决方法)
把下面代码复制到TXT文本中,把文本再改成 .cmd 格式保存后以管理员身份运行 @echo off pushd "%~dp0" dir /b C:\Windows\serv ...