Spring 集成 RabbitMQ
2024-09-07 21:17:05
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
最新文章
- poi excel导出,下载
- nopcommerce 初学2
- C#后台程序重启IIS,发邮件通知
- 边工作边刷题:70天一遍leetcode: day 88
- 51nod1476 括号序列的最小代价
- Android 多进程编程 15问15答!
- Android SDK安装时碰到的问题之解决办法
- Windows环境下安装导入tornado
- Android开发中小知识
- 使用Eclipse来操作HDFS的文件
- zabbix3.2监控mysql
- python数据结构-如何在列表、字典、集合中根据条件筛选数据
- JSON数据展示神器:react-json-view(常用于后台网站)
- Java中的comparable接口和Comparator接口的区别
- github如何添加新的分支
- Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)
- gtk+学习笔记(七)
- C# Directory和DirectoryInfo类(文件目录操作)
- The database cluster was initialized with RELSEG_SIZE 1048576, but the server was compiled with RELSEG_SIZE 8388608
- C++ Primer 第二章 学习笔记