Spring Boot文档阅读笔记-对Messaging with RabbitMQ解析
此篇教程以Rabbitmq作为消息队列服务端,使用Spring Boot产生和发布消息。
使用Spring AMQP的RabbitTemplate发布消息,使用MessageListenerAdapter订阅消息。
其中对应的Maven如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.it1995</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-amqp</artifactId><version>2.3.1</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>2.3.1</version><scope>compile</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
下面是创建接受者,获取发布者的发布的消息:
package cn.it1995.demo;import org.springframework.stereotype.Component;import java.util.concurrent.CountDownLatch;@Component
public class Receiver {private CountDownLatch latch = new CountDownLatch(1);public void receiveMessage(String message){System.out.println("Received <" + message + ">");latch.countDown();}public CountDownLatch getLatch(){return latch;}
}
这个Receiver是POJO类,这里的POJO是指没有带有业务处理的类,当注册后,就能在任意的地方进使用。
CountDownLatch,里面会有一个接收消息的信号。
注册监听者以及发送消息
使用Spring AMQP的RabbitTemplate提供的函数用于接收消息,逻辑如下:
1. 配置消息监听者容器;
2.声明队列,交换机对其都进行绑定;
3.配置发送者组建,使得监听者能接收到。
代码如下:
package cn.it1995.demo;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;@SpringBootApplication
public class DemoApplication {static final String topicExchangeName = "spring-boot-exchange";static final String queueName = "spring-boot";@BeanQueue queue(){return new Queue(queueName, false);}@BeanTopicExchange exchange(){return new TopicExchange(topicExchangeName);}@BeanBinding binding(Queue queue, TopicExchange exchange){return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");}@BeanSimpleMessageListenerContainer container(ConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter){SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.setQueueNames(queueName);container.setMessageListener(listenerAdapter);return container;}@BeanMessageListenerAdapter listenerAdapter(Receiver receiver){return new MessageListenerAdapter(receiver, "receiveMessage");}public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
listenerAdapter()方法将消息监听者注册到容器中(默认是在container())。
queue()方法创建了AMQP的队列。exchange()方法创建了topic交换机。
binding()方法将其进行绑定。
发送测试数据到Rabbitmq
package cn.it1995.demo;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Component
public class Runner implements CommandLineRunner {private final RabbitTemplate rabbitTemplate;private final Receiver receiver;public Runner(Receiver receiver, RabbitTemplate rabbitTemplate){this.receiver = receiver;this.rabbitTemplate = rabbitTemplate;}@Overridepublic void run(String... args) throws Exception {System.out.println("Sending message ...");rabbitTemplate.convertAndSend(DemoApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);}
}
项目的application.properties如下:
spring.rabbitmq.host=122.xxx.xxx.xxx
spring.rabbitmq.port=5672
spring.rabbitmq.username=xxxx
spring.rabbitmq.password=xxxxxxx
spring.rabbitmq.virtual-host=/xxxxx
程序运行截图如下:
源码打包下载地址:
https://github.com/fengfanchen/Java/tree/master/SpringBootRabbitmq
Spring Boot文档阅读笔记-对Messaging with RabbitMQ解析相关推荐
- Spring Boot文档阅读笔记-how-to-implement-2-way-ssl-using-spring-boot
two-way-ssl需要12次握手(除去TCP的三次握手),如下图: 双向认证过程: 1.客户端发送ClientHello消息,告诉服务端要使用SSL. 2.客户端发送ServerHello的响应, ...
- Spring Boot文档阅读笔记-EhCache的使用
这里要先注意2个概念: buffer和cache,很多人会讲这两个概念混用.但其实这是两个概念! buffer:一般是指存储临时数据的实体.只能读写一次,对于程序员来说buffer是可见的,比如TCB ...
- Spring Boot文档阅读笔记-Spring Boot @Bean解析
利用SpringBoot的@Bean创建一个简单的Bean. Spring的@Bean注解是放在方法上的,带上这个注解的方法会被Spring容器管理.并且这个方法要返回一个值(对象),这个值和对象会被 ...
- Spring Boot文档阅读笔记-对Securing a Web Application解析
首先创建一个非安全的Web应用 这个应用包含两个页面,一个是home页面,一个是"Hello,World"页面.home页面使用Thymeleaf,相关代码如下: <!DOC ...
- Spring Boot文档阅读笔记-构建Restful风格的WebService客户端
对应的maven如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring Boot文档阅读笔记-构建Restful风格的WebService
Maven代码如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring Boot文档阅读笔记-@SpringBootApplication官方解析与实例(1.5.19)
目录 官方解析 博主例子 官方解析 @SpringBootApplication有如下3个特点: 1. @EnableAutoConfiguration: 能够启动Spring Boot的自动配置机制 ...
- Spring Boot文档阅读笔记-DataSource configuration
DataSource:一个工厂可以连接任意厂家的数据库.通常使用URL以及一些认证去建立数据库连接. DataSource在代码中是一个对象,这个对象贯彻并落实了javax.sql.DataSourc ...
- Spring Boot文档阅读笔记=Caching Data with Spring
此篇博文展示了使用Spring去管理Bean开启缓存. maven如下: <?xml version="1.0" encoding="UTF-8"?> ...
最新文章
- springboot打印sql日志_Spring boot 工程,http打印日志太多
- C# indexof和indexofany区别(转)
- 【转】10.Qt编程涉及的术语和名词
- mysql的纵向扩展方案_SQL Server横向扩展方案-SODA
- 《MySQL必知必会》学习笔记——第三章(了解数据库和表)
- gsp计算机系统系统操作培训,gsp计算机系统操作
- wpf基于DevExpress实现折线图的两种方法
- 图像处理库Pillow的使用
- U3D Distortion
- Matlab程序——3d玫瑰
- 【图像分类】2021-Twins NeurIPS
- 电脑调分辨率黑屏了怎么办_调显示器分辨率黑屏怎么办
- 智利车厘子的尺寸说明,给大家扫盲
- Html5制作工具对比
- 解决nginx emerg bind to 80 failed 98 Address alrea
- mongdb权限问题
- android修改checkbox样式边框颜色
- 技术20期:结构化数据与非结构化数据:有什么区别?
- 使用MSF进行提权(windows提权、linux提权、wesng使用)
- 先行一步,7 大技术创新和突破,阿里云把 Serverless 领域的这些难题都给解了
热门文章
- 转载:JAVA日期处理
- FTP协议的分析和扩展
- 精品 IDEA 插件大汇总!值得收藏
- 程序员的搞笑日常,你们懂得!....
- C++ Primer中文版(第4版 特别版)
- 飞鸽传书的这一新的通信方式采用云技术
- 36岁程序员感慨:天天加班压力太大,有200万存款能转行了吗?
- 在linux下使用360随身wifi 2 | 李凡希的blog,在Linux下使用“360随身WiFi 2”
- SQLite | Group By 和 Order By 子句
- python图像处理大全