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

最新文章

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

热门文章

  1. CSS 选择器及优先级
  2. 兄弟们,我打算抠100个网站JS加密代码召唤,一个也跑不掉,这次轮到小虎牙
  3. 前端测试框架 Jest
  4. Kubernetes实战总结 - 阿里云ECS自建K8S集群
  5. 2020年1月31日 安装Python的BeautifulSoap库记录
  6. Spring框架学习笔记(1)
  7. 双向绑定数据的实现(new Proxy 版本)
  8. adb安装apk包提示protocol failure问题
  9. pycharm之ctrl+鼠标滚轮调整字体大小
  10. 大揭秘| 我司项目组Gitlab Flow && DevOps流程