Springboot怎么整合RabbitMQ
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇文章给大家分享的是有关Springboot怎么整合RabbitMQ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。准备工作15minI
千家信息网最后更新 2025年12月02日Springboot怎么整合RabbitMQ
本篇文章给大家分享的是有关Springboot怎么整合RabbitMQ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
准备工作
15min
IDEA
maven 3.0
在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/download.html ,如果你是用的Mac(程序员都应该用mac吧),你可以这样下载:
brew install rabbitmq
安装完成后开启服务器:
rabbitmq-server
开启服务器成功,你可以看到以下信息:
RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc.## ## Licensed under the MPL. See http://www.rabbitmq.com/## ############ Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log###### ## /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log########## Starting broker... completed with 6 plugins.
构建工程
构架一个SpringBoot工程,其pom文件依赖加上spring-boot-starter-amqp的起步依赖:
org.springframework.boot spring-boot-starter-amqp
创建消息接收者
在任何的消息队列程序中,你需要创建一个消息接收者,用于响应发送的消息。
@Componentpublic 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; }}消息接收者是一个简单的POJO类,它定义了一个方法去接收消息,当你注册它去接收消息,你可以给它取任何的名字。其中,它有CountDownLatch这样的一个类,它是用于告诉发送者消息已经收到了,你不需要在应用程序中具体实现它,只需要latch.countDown()就行了。
创建消息监听,并发送一条消息
在spring程序中,RabbitTemplate提供了发送消息和接收消息的所有方法。你只需简单的配置下就行了:
需要一个消息监听容器
声明一个quene,一个exchange,并且绑定它们
一个组件去发送消息
代码清单如下:
package com.forezp;import com.forezp.message.Receiver;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;@SpringBootApplicationpublic class SpringbootRabbitmqApplication { final static String queueName = "spring-boot"; @Bean Queue queue() { return new Queue(queueName, false); } @Bean TopicExchange exchange() { return new TopicExchange("spring-boot-exchange"); } @Bean Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(queueName); } @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(listenerAdapter); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } public static void main(String[] args) { SpringApplication.run(SpringbootRabbitmqApplication.class, args); }}创建一个测试方法:
@Componentpublic class Runner implements CommandLineRunner { private final RabbitTemplate rabbitTemplate; private final Receiver receiver; private final ConfigurableApplicationContext context; public Runner(Receiver receiver, RabbitTemplate rabbitTemplate, ConfigurableApplicationContext context) { this.receiver = receiver; this.rabbitTemplate = rabbitTemplate; this.context = context; } @Override public void run(String... args) throws Exception { System.out.println("Sending message..."); rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!"); receiver.getLatch().await(10000, TimeUnit.MILLISECONDS); context.close(); }}启动程序,你会发现控制台打印:
Sending message...Received
以上就是Springboot怎么整合RabbitMQ,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
消息
程序
接收者
方法
整合
工程
更多
服务器
知识
篇文章
工作
服务
监听
实用
成功
代码
信息
发送者
只需
名字
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
罗永浩视频软件开发
问道清理角色物品数据库
网络安全等保工具箱采购
mx解析需要服务器吗
ifix历史服务器停止工作
海南服务器代理服务器
华为澳洲网络安全
国家软件开发证书代号
十年伴奏软件开发
258ip站群服务器
适合网络技术课播放的纪录片
如何成为优秀的软件开发工程师呢
大修管理软件开发公司
数据库分片提高查询速度
国产公司实时数据库与国外比
网络安全hoc
雷石服务器拆解
办税大厅网络安全自查情况
2020一季度网络安全份额
学校专人负责网络安全工作
彩讯科技股份产业互联网专家
服务器什么时候升级
电子商务数据库课件
发动机管理系统软件开发流程
基础软件开发是指哪些
谷歌 网络技术
要玩娱乐网络技术有限公司
杭州.net软件开发薪资
重返帝国换服务器
浅析计算机网络技术答辩