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. Shell编程—数据展示

    1.标准文件描述符 Linux用文件描述符(file descriptor)来标识每个文件对象.文件描述符是一个非负整数,可以唯一标识会话中打开的文件.每个进程一次 多可以有九个文件描述符.出于特殊目 ...

  2. Python全局变量的简单使用

    对Pyhon实现静态变量全局变量的方法详解 python不能像C++一样直接定义一个static变量或者通过extern来导入别的库的变量而实现数据共享,但是python的思想是通过模块化来解决这个问 ...

  3. 重温Java Web的技术细节

    目录 一.背景 二.请求与响应 2.1.Http请求 2.2.Http响应 三.ServletConfig 3.1 测试ServletConfig参数 四.ServletContext 4.1 测试S ...

  4. 无法登陆网站,nginx漏配置

    location / {         try_files $uri $uri/ /index.php?$query_string; }   这条主要是将index.php入口文件重写掉,所以平常我 ...

  5. 我用 Java 8 写了一段逻辑,同事直呼看不懂,你试试看。。

    业务背景 首先,业务需求是这样的,从第三方电商平台拉取所有订单,然后保存到公司自己的数据库,需要判断是否有物流信息,如果有物流信息,还需要再进行上传. 而第三方接口返回的数据是 JSON 格式的,其中 ...

  6. Android开发之dp转像素,像素转换为dp工具类,详细代码,带有源文件下载地址。

    import android.content.Context; /** * @author 官网:http://blog.csdn.net/qq_21376985 * * David编写: 微博:ht ...

  7. 【Unity C#编程】自定义数据

    译林军 灰魅|2014-03-04 10:52|10589次浏览|Unity(315)移动应用(31)技术开发(16)0 在这篇Unity C#的文章中,你将会创建一个简单的数据结构,然后写下它的属性 ...

  8. CSS中的包含块

    1.初始包含块,浏览器viewport大小 2.非根元素,position:relative/static,包含块为最近的块级框,表格单元或行内祖先框的内容区 3.非根元素,position:abso ...

  9. 01vue.config.js

      const path = require('path'); module.exports = { // 基本路径 publicPath: process.env.NODE_ENV === 'pro ...

  10. C#知识点:操作XML

    XML是什么就不用说了文本标记语言. 主要纪录如何对XML文件进行增删改查. Xml的操作类都存在System.xml命名空间下面. 应用型的直接上代码 using System; using Sys ...