Spring boot Rabbitmq 示例


简介

    Spring boot RabbitMQ 简单程序示例

编写详情

RabbitMQ docker

    避免麻烦,直接使用docker启动一个RabbitMQ,命令如下:

docker run -dit --name rabbitmq -p 5672:5672 rabbitmq

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.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.spring.boot.rabbitmq</groupId><artifactId>rabbitmqexample</artifactId><version>0.0.1-SNAPSHOT</version><name>rabbitmqexample</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</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.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>

消费者编写

    编写一个消费类,接收消息后简单打印,代码大致如下:

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("Receive < " + message + " >");latch.countDown();}public CountDownLatch getLatch() {return latch;}
}

生产者编写

    发送消息,代码大致如下:

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(RabbitmqexampleApplication.topicExchangeName, "foo.bar.baz","Hello from RabbitMA!");receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);}
}

相关配置

    直接在主函数中进行配置即可,代码如下:

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 RabbitmqexampleApplication {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.#");}@BeanMessageListenerAdapter listenerAdapter(Receiver receiver) {return new MessageListenerAdapter(receiver, "receiveMessage");}@BeanSimpleMessageListenerContainer container(ConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.setQueueNames(queueName);container.setMessageListener(listenerAdapter);return container;}public static void main(String[] args) {SpringApplication.run(RabbitmqexampleApplication.class, args).close();}}

    运行后即可看到输出

Spring boot Rabbitmq 示例相关推荐

  1. Spring Boot AJAX 示例

    本文以spring boot框架.thymeleaf引擎为基础,利用jquery.ajax提交HTML表单请求到后台(spring rest api),后台返回一个JSON格式的数据为例进行说明. 开 ...

  2. Spring AMQP RabbitMQ示例

    Spring AMQP RabbitMQ示例 今天我们将研究Spring AMQP RabbitMQ示例应用程序.我们之前的帖子中已经讨论了一些"Spring AMQP基础知识理论" ...

  3. spring boot + rabbitMq整合之死信队列(DL)

    rabbit mq 死信队列 什么是死信队列? DL-Dead Letter 死信队列 死信,在官网中对应的单词为"Dead Letter",可以看出翻译确实非常的简单粗暴.那么死 ...

  4. Spring Boot完成示例

    这篇文章提供了一个使用Spring Boot开发松耦合REST服务的完整示例. 使用spring boot,我们可以开发可独立运行的生产就绪Java应用程序,它是独立的应用程序,具有最小的依赖性,并且 ...

  5. Spring Boot 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统...

    一.概述 经过HelloWorld示例(Spring Boot 快速入门(上)HelloWorld示例)( Spring Boot  快速入门 详解 HelloWorld示例详解)两篇的学习和练习,相 ...

  6. Spring Boot DTO 示例 - 实体到 DTO 的转换

    在本教程中,我们将学习如何在Spring Boot 应用程序中创建 DTO(数据传输对象)类,以及如何使用 ModelMapper 库将实体转换为 DTO,反之亦然. 数据传输对象设计模式是一种常用的 ...

  7. 【Spring Boot】Spring Boot Logging 示例 | 日志记录

    文章目录 logging.level | 设置日志级别 logging.file | 指定输出日志文件的路径和名称 logging.path | 指定输出日志文件的路径 logging.pattern ...

  8. Spring Boot——RabbitMQ

    转载自https://blog.csdn.net/lyhkmm/article/details/78772919 RabbitMq的介绍 RabbitMq的基本原理可以自行上网查阅,或者点击传送门:R ...

  9. Spring boot + RabbitMQ延迟队列实战

    一.背景 延时队列顾名思义,即放置在该队列里面的消息是不需要立即消费的,而是等待一段时间之后取出消费. 那么,为什么需要延迟消费呢?我们来看以下的场景: 订单业务: 在电商/点餐中,都有下单后 30 ...

最新文章

  1. LeetCode 52. N皇后 II
  2. Spark LogisticRegression 逻辑回归之建模
  3. 【 Markdown 】Markdown 编辑器语法常用格式 ( 整理中 ... )
  4. gethostbyname()函数说明
  5. Venkat 演讲翻译:你要清除代码中的异味
  6. 66Linux主机名和hosts67网络配置小结
  7. Kubernetes 架构(上)- 每天5分钟玩转 Docker 容器技术(120)
  8. Docker学习总结(55)——Google和Facebook为什么不用Docker?
  9. Python笔记:变量的作用域
  10. 磁盘工具无法修复磁盘怎么办
  11. VC++6.0环境下调试c语言代码的方法和步骤_附图
  12. 非线性光纤光学_片上光学频率梳:可产生光子微波,应用于卫星通信和5G网络!...
  13. 读书笔记——《图解TCP/IP》(1/4)
  14. codeforces949D Curfew
  15. Android vitamo 实现横竖屏的切换和页面内部的网络视频
  16. P1162 填图颜色 洛谷(BFS的简单应用)
  17. 烽火狼烟丨VMware Workspace ONE Access身份验证绕过、本地提权漏洞风险提示
  18. 有关三年级计算机课的日记,有趣的一节课日记 三年级日记
  19. 密码子偏好性分析~codonW,EMBOSS:CUSP(图文教程)
  20. 小说更新太慢怎么办_为什么现在的网络小说更新这么慢

热门文章

  1. haproxy +keepalived 原创
  2. iOS Crash文件的解析(一)
  3. web.py+xheditor+ ajaxfileupload+新浪sae图片上传
  4. 解决jquery之get缓存问题的最简单方法
  5. C#执行Sql 时,出现“算术运算导致溢出”问题,如何解决?
  6. RMI 异常 no security manager: RMI class loader disabled
  7. 【报告分享】2021年数字化浪潮在中国的发展和实践.pdf(附下载链接)
  8. 【报告分享】2020中国时尚跨境电商发展报告.pdf(附下载链接)
  9. 北深互联网之争:北京约饭,深圳建群
  10. github无法显示图片,其他一切正常的解决办法