Spring 集成 RabbitMQ
pom.xml
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
spring-rabbitmq-parent.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!-- 连接服务配置 -->
<rabbit:connection-factory id="connectionFactory" host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}"/>
<rabbit:admin connection-factory="connectionFactory"/>
<!-- queue 队列声明 -->
<rabbit:queue id="queue" name="${rabbit.queue.name}"/>
<!-- exchange queue binging key 绑定 -->
<rabbit:direct-exchange id="directExchange" name="${rabbit.direct.exchange.name}">
<rabbit:bindings>
<rabbit:binding queue="queue" key="${rabbit.queue.key}"/>
</rabbit:bindings>
</rabbit:direct-exchange>
</beans>
spring-rabbitmq-producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!-- 导入生产者和消费者的公共配置 -->
<import resource="spring-rabbitmq-parent.xml"/>
<!-- spring template声明 -->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" exchange="${rabbit.direct.exchange.name}" queue="${rabbit.queue.name}" routing-key="${rabbit.queue.key}"/>
</beans>
spring-rabbitmq-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!-- 导入生产者和消费者的公共配置 -->
<import resource="spring-rabbitmq-parent.xml"/>
<!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener queues="queue" ref="${rabbit.queue.listener}"/>
</rabbit:listener-container>
</beans>
RabbitMQUtil.java
package com.app.core.util;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
@Log4j2
public class RabbitMQUtil {
/**
* RabbitMQ消息发送和接收模板
*/
private static RabbitTemplate template;
@Autowired
public void setTemplate(RabbitTemplate template) {
RabbitMQUtil.template = template;
}
/**
* 发送文本消息
*
* @param msg 消息内容
*/
public static void send(String msg) {
template.convertAndSend(msg);
if (log.isInfoEnabled())
log.info("RabbitMQ消息发送成功,消息内容:{}", msg);
}
}
QueueListener.java
package com.app.server.listener;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.CharEncoding;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
@Log4j2
@Component
public class QueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
// 将 byte 数组转换为字符串
String msgContent = new String(message.getBody(), CharEncoding.UTF_8);
if (log.isInfoEnabled())
log.info("RabbitMQ消息接收成功,消息内容:{}", msgContent);
} catch (UnsupportedEncodingException e) {
log.error("RabbitMQ编码类型不支持", e);
}
}
}
config.properties
rabbit.host=127.0.0.1
rabbit.port=8080
rabbit.username=
rabbit.password=
rabbit.direct.exchange.name=exchange.demo.name
rabbit.queue.key=queue.demo.key
rabbit.queue.name=queue.demo.name
rabbit.queue.listener=queueListener
Spring 集成 RabbitMQ的更多相关文章
- rabbitMQ第五篇:Spring集成RabbitMQ
前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...
- RabbitMQ第四篇:Spring集成RabbitMQ
前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...
- spring集成rabbitMq(非springboot)
首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...
- spring集成RabbitMQ配置文件详解(生产者和消费者)
1,首先引入配置文件org.springframework.amqp,如下: <dependency> <groupId>org.springframework.amqp< ...
- spring集成rabbitmq
https://www.cnblogs.com/nizuimeiabc1/p/9608763.html
- Spring集成RabbitMQ-使用RabbitMQ更方便
如果提到Spring,你脑海中对他的印象还停留在SSH三大框架之一,那或许你该好好重新认识这个家伙. 在IT技术日新月异的今天,他还能让你忘不了并与他朝夕相处,他,肯定有自己的绝活.如今他早已经不是孤 ...
- Spring Boot实战三:集成RabbitMQ,实现消息确认
Spring Boot集成RabbitMQ相比于Spring集成RabbitMQ简单很多,有兴趣了解Spring集成RabbitMQ的同学可以看我之前的<RabbitMQ学习笔记>系列的博 ...
- Spring集成RabbiMQ-Spring AMQP新特性
上一篇<Spring集成RabbitMQ-使用RabbitMQ更方便>中,我们只需要添加响应jar的依赖,就可以写一个Spring集成RabbitMQ下非常简单收发消息的程序. 我们使用的 ...
- Spring 集成rabbiatmq
pom 文件 <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artif ...
随机推荐
- 如何快速系统学会使用SPSS?
SPSS是一款数据统计与数据分析工具,操作简单属于数据分析的入门工具. 想要灵活使用SPSS,需要掌握两个方面内容:数据分析相关知识.SPSS操作 1 数据分析 在使用数据分析工具之前,首先要了解数据 ...
- Mysql锁【转】
一.概述 数据库锁定机制简单来说,就是数据库为了保证数据的一致性,而使各种共享资源在被并发访问变得有序所设计的一种规则.对于任何一种数据库来说都需要有相应的锁定机制,所以MySQL自然也不能例外. M ...
- Myeclipse maven 配置有问题 改之后重启还是不好用
在配置maven项目的时候我一大意选错了maven服务,然后回来改配置文件的时候发现改完之后重启并没有效果,重新清了好几次编译也不好用,最后发现最好是手动去更新一下maven服务的配置文件 位置如下: ...
- py_递归实例:汉诺塔问题
递归的两个特点 调用自身 结束条件 # _*_coding:utf-8 ''' 递归实例:汉诺塔问题 n----盘子总数 a----第一个柱子 b----第二个柱子 c----第三个柱子 n个盘子时: ...
- 位运算处理字符大小写转换 - 关联Leetcode 709. 转成小写字母
大写变小写.小写变大写 : 字符 ^= 32; 大写变小写.小写变小写 : 字符 |= 32; 小写变大写.大写变大写 : 字符 &= -33; 题目 实现函数 ToLowerCase(),该 ...
- 超简单集成华为HMS ML Kit文本识别SDK,一键实现账单号自动录入
前言 在之前的文章<超简单集成华为HMS Core MLKit通用卡证识别SDK,一键实现各种卡绑定>中我们给大家介绍了华为HMS ML Kit通用卡证识别技术是如何通过拍照自动识别卡 ...
- 要不是真的喜欢学技术,谁会来用Python爬小姐姐啊
养成习惯,先赞后看!!!不用于任何商业价值,只是自己娱乐.否则 爬虫爬的好,牢饭吃到饱.这是我们这次爬取的网址:https://www.vmgirls.com/ 很多人学习python,不知道从何学起 ...
- MySQL常用指令,java,php程序员,数据库工程师必备。程序员小冰常用资料整理
MySQL常用指令,java,php程序员,数据库工程师必备.程序员小冰常用资料整理 MySQL常用指令(备查) 最常用的显示命令: 1.显示数据库列表. show databases; 2.显示库中 ...
- 小程序开发-媒体组件image
image 图片组件,支持 JPG.PNG.SVG.WEBP.GIF 等格式,2.3.0 起支持云文件ID. 所有属性如下: Tips image组件默认宽度320px.高度240px image组件 ...
- 二.spring boot第一个web服务
通过<一.spring boot初始化项目>我们已经会初始化spring boot项目,那本篇文章就说明下初始化项目的具体内容,并编写第一个Hello页面. 项目结构 mvnw.mvnw. ...