spring-rabbit的使用
rabbitMQ安装教程略
新建一个Maven项目,整体项目结构如下
1、所导入的jar
<dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>1.4.0.RELEASE</version></dependency>
2、RabbitMQ配置,RabbitMQ连接密码使用你自己的
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"xsi:schemaLocation="http://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsd"><!-- 定义RabbitMQ的连接工厂 --><rabbit:connection-factory id="connectionFactory"host="127.0.0.1" port="5672" username="admin" password="admin"virtual-host="testhost" /><!-- 定义Rabbit模板,指定连接工厂以及定义exchange --><rabbit:template id="template" connection-factory="connectionFactory" exchange="fanoutExchange" /><!-- RabbitAdmin主要用于在Java代码中对理队和队列进行管理,用于创建、绑定、删除队列与交换机,发送消息等 --><rabbit:admin connection-factory="connectionFactory" /><!-- 定义队列,自动声明 --><rabbit:queue name="messageQueue" auto-declare="true"/><!-- 定义交换器,把Queue绑定到交换机,自动声明 --><rabbit:fanout-exchange name="fanoutExchange" auto-declare="true"><rabbit:bindings><rabbit:binding queue="messageQueue"/></rabbit:bindings></rabbit:fanout-exchange><!-- 队列监听 --><rabbit:listener-container connection-factory="connectionFactory"><rabbit:listener ref="customer" method="listen" queue-names="messageQueue" /></rabbit:listener-container><!-- 消费者bean --><bean id="customer" class="ztest.rcc.Customer" /></beans>
3、定义一个消息生产者、一个消费者
生产者
package ztest.rcc;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Description 生产者* @author gj* @time 2022年9月15日上午11:06:11*/
public class Produce {public static void main(String[] argv) throws Exception {AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:conf/spring/applicationContext-rabbit.xml");//RabbitMQ模板RabbitTemplate template = ctx.getBean(RabbitTemplate.class);String message = "Hello, Ignore!";template.convertAndSend(message);System.out.println("发送消息, message:" + message);Thread.sleep(3000);ctx.destroy();}
}
消费者
package ztest.rcc;/*** @Description 消费者* @author gj* @time 2022年9月15日上午11:14:55*/
public class Customer {public void listen(String message) {System.out.println("\n消费者: " + message + "\n");}
}
运行结果
PS:可能会报错什么身份验证失败,把RabbitMQ的账号密码改一下就行了,记得配置文件同步修改
spring-rabbit的使用相关推荐
- 1.Spring Boot --hello world
2019独角兽企业重金招聘Python工程师标准>>> 参考文章及其源代码:http://www.bysocket.com/?p=1124 https://github.com/vi ...
- Spring cloud集成Rabbitmq
1.配置pom <dependency><groupId>org.springframework.boot</groupId><artifactId>s ...
- Spring Boot application.properties 常用配置
SPRING CONFIG (ConfigFileApplicationListener) spring.config.name 配置文件名称,默认为application spring.config ...
- Spring AMQP 教程
Spring AMQP 我们今天将研究Spring AMQP. 目录[ 隐藏 ] 1 Spring AMQP 1.1什么是AMQP? 1.2为什么我们需要AMQP? 1.3 JMS和AMQP之间的区别 ...
- Spring Boot常见应用属性默认值
2019独角兽企业重金招聘Python工程师标准>>> # ============================================================= ...
- Spring Boot 菜鸟教程 application.properties 常用配置
SPRING CONFIG (ConfigFileApplicationListener) spring.config.name 配置文件名称,默认为application spring.config ...
- Spring Boot 3.0.0-M1 Reference Documentation(Spring Boot中文参考文档) 9-16
9. 数据 Spring Boot与多个数据技术集成,包括SQL和NoSQL. 9.1. SQL数据库 Spring Framework提供扩展支持用于与SQL数据工作,从使用JdbcTemplate ...
- Spring AMQP参考手册
一.快速入门 Maven依赖声明 <dependency> <groupId>org.springframework.amqp</groupId> ...
- spring AMQP 中文文档翻译
标题spring AMQP 中文文档翻译 spring AMQP 实现: spring rabbit 官方中文文档翻译 最初是由于 找不到 ConfirmType中SIMPLE和 CORRELATED ...
- 微服务Spring Cloud
Java服务器项目分类 现在市面上常见的java开发的项目可以分为两大类 1.企业级应用 一般指一个企业或机构内部使用的网站或服务器应用程序 包括的领域很多,包括并不限于:商业,企事业单位,医疗,军事 ...
最新文章
- python svm超参数_grid search 超参数寻优
- 利用Java存储过程简化数据库操作
- vue判断浏览器是否id_QQ被曝读取浏览器历史记录!回应称系用于判断是否恶意登录...
- LR学习笔记三 之 界面分析
- 电机串电阻会有什么影响?
- 对具有外部依赖的Angular服务类(service class)进行单元测试的几种方式
- Spring项目的按层打包已过时
- 前端学习(94):css重置样式
- 使用TextRange获取输入框中光标的位置
- Codeblock汉化教程
- springboot整合腾讯云短信服务
- 计算机三级网络技术考过指南 【历年考点汇总】
- python 图片文字转换成word_如何基于python把文字图片写入word文档
- 做好加密手机 任重而道远
- NOIP模拟赛 队爷的讲学计划
- wifi设备名称android,Android设置WiFi设备名解析
- 每步科技动态域名怎么用
- proteus教程——操作内存并行扩展8255
- 安搭Share:如果说格局决定人生,那么什么决定格局
- 为VsCode配置C++环境
热门文章
- 冷战背景下的计算机,冷战最疯狂项目:直接把大脑拆下来给战斗机器人机器人当CPU...
- 基于QEMU-aarch64学习UEFI(EDK2)-1环境搭建
- 【个人感悟】如何学习计算机知识
- office2021 版本2203现已适配win11界面风格
- 计算机网络体系结构详解(图文)
- 咖啡 santos and java,咖啡种类的分析 煮咖啡水配比
- 2022年舞台造雪机市场前景分析及研究报告
- 计算机网络基础概念与重要定义汇总
- Nutch 2.0 终于来了
- 解决SSD+HDD双硬盘下电脑卡顿问题,CPU和内存使用率低,电脑依旧卡顿