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的更多相关文章

  1. rabbitMQ第五篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  2. RabbitMQ第四篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  3. spring集成rabbitMq(非springboot)

    首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...

  4. spring集成RabbitMQ配置文件详解(生产者和消费者)

    1,首先引入配置文件org.springframework.amqp,如下: <dependency> <groupId>org.springframework.amqp< ...

  5. spring集成rabbitmq

    https://www.cnblogs.com/nizuimeiabc1/p/9608763.html

  6. Spring集成RabbitMQ-使用RabbitMQ更方便

    如果提到Spring,你脑海中对他的印象还停留在SSH三大框架之一,那或许你该好好重新认识这个家伙. 在IT技术日新月异的今天,他还能让你忘不了并与他朝夕相处,他,肯定有自己的绝活.如今他早已经不是孤 ...

  7. Spring Boot实战三:集成RabbitMQ,实现消息确认

    Spring Boot集成RabbitMQ相比于Spring集成RabbitMQ简单很多,有兴趣了解Spring集成RabbitMQ的同学可以看我之前的<RabbitMQ学习笔记>系列的博 ...

  8. Spring集成RabbiMQ-Spring AMQP新特性

    上一篇<Spring集成RabbitMQ-使用RabbitMQ更方便>中,我们只需要添加响应jar的依赖,就可以写一个Spring集成RabbitMQ下非常简单收发消息的程序. 我们使用的 ...

  9. Spring 集成rabbiatmq

    pom 文件 <dependencies> <dependency> <groupId>com.rabbitmq</groupId> <artif ...

随机推荐

  1. 小白式DPDK搭建方法(附相关错误得处理方法)

    搭建环境:VM VM搭建参考其他博客 步骤一 点击虚拟机设置,添加两个网卡(我这里都是使用的都是桥接模式) 步骤二.从网上下载dpdk的压缩包: 命令:wget http://fast.dpdk.or ...

  2. 每日JS逆向练习之斗鱼登录密码加密,今天你练了吗?

    一切的基本功都是为后期调试滑块验证码准备的. 有兴趣的关注一下知识图谱与大数据公众号,当然不关注也无所谓.今天来看看斗鱼登录密码加密,正所谓熟能生巧,这种简单一点的基本3-5分钟就要能抠出来,有兴趣得 ...

  3. C++ Templates (2.2 使用Stack类模板 Use of Class Template Stack )

    返回完整目录 目录 2.2 使用Stack类模板 Use of Class Template Stack 2.2 使用Stack类模板 Use of Class Template Stack 在C++ ...

  4. 【Spring注解驱动开发】AOP核心类解析,这是最全的一篇了!!

    写在前面 昨天二狗子让我给他讲@EnableAspectJAutoProxy注解,讲到AnnotationAwareAspectJAutoProxyCreator类的源码时,二狗子消化不了了.这不,今 ...

  5. Mysql表,列,库的增删查改

    下面是我总结的一些基础的sql知识,主要是为了以后更好的查阅和帮助其他初学的人,同时记录自己的成长,还写了一点稍有难度的sql面试题级别的题目,好了废话不多说,见真题... #创建数据库 CREATE ...

  6. VMware安装Centos7 -九五小庞

    VMware安装Centos7超详细过程(图文) https://blog.csdn.net/babyxue/article/details/80970526 安装centos7的时候 启动会提示Pl ...

  7. Q200510-02: 重复的DNA序列 程序解法

    问题:  重复的DNA序列 所有 DNA 都由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:“ACGAATTCCG”.在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编 ...

  8. 20190923-02Linux文件目录类 000 010

    pwd 显示当前工作目录的绝对路径 pwd:print working directory 打印工作目录 1.基本语法 pwd (功能描述:显示当前工作目录的绝对路径) 2.案例实操 (1)显示当前工 ...

  9. SpringMVC-08-SpringMVC层编写

    SpringMVC层编写 web.xml DispatcherServlet <!--DispatcherServlet--> <servlet> <servlet-na ...

  10. [04] C# Alloc Free编程之实践

    C# Alloc Free编程之实践 上一篇说了Alloc Free编程的基本理论. 这篇文章就说怎么具体做实践. 常识 之所以说是常识, 那是因为我们在学任何一门语言的时候, 都能在各种书上看到各种 ...