依赖包:

        <!--RabbitMQ集成spring-->
<!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>

消息者Spring配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> <!-- 连接服务配置 -->
<rabbit:connection-factory id="connectionFactory"
host="10.15.1.26" username="admin" password="admin" port="5672"
virtual-host="/test_host" channel-cache-size="5"/> <!--MQ的管理,包括队列、交换器的声明等-->
<rabbit:admin connection-factory="connectionFactory"/> <!-- queue 队列声明 -->
<rabbit:queue durable="true"
auto-delete="false" exclusive="false" name="test.spring.queue"/> <!-- exchange queue binging key 绑定 -->
<rabbit:direct-exchange name="spring.exchange"
durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="test.spring.queue" key="spring.queue.key"/>
</rabbit:bindings>
</rabbit:direct-exchange> <!-- spring template声明 -->
<rabbit:template id="amqpTemplate" exchange="spring.exchange" routing-key="spring.queue.key"
connection-factory="connectionFactory"/> <!-- 监听生产者发送的消息开始 --> <!-- 声明消息转换器为SimpleMessageConverter -->
<bean id="messageConverter"
class="org.springframework.amqp.support.converter.SimpleMessageConverter">
</bean> <!-- 用于接收消息的处理类 -->
<bean id="myListener" class="org.study.model.MyListener"/> <!-- 用于消息的监听的代理类MessageListenerAdapter -->
<bean id="receiveListenerAdapter"
class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="myListener" />
<property name="defaultListenerMethod" value="onMessage"></property>
</bean> <!-- 用于消息的监听的容器类SimpleMessageListenerContainer,对于queueName的值一定要与定义的Queue的值相同 -->
<bean id="listenerContainer"
class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queueNames" value="test.spring.queue"></property>
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageListener" ref="receiveListenerAdapter"></property>
</bean> </beans>

消费者消息处理代码:

 package org.study.model;

 /**
* RabbitMQ与Spring整合
* 消费者消息处理类
*/
public class MyListener {
public void onMessage(String message) {
System.out.println(" [RECV] : " + message);
} }

运行代码:

 package org.study.spring5;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* RabbitMQ与Spring整合
* 消费者
*/
public class SpringConsumer { public static void main(String args[]) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/consumer-spring-config.xml"); }
}

Java使用RabbitMQ之整合Spring(消费者)的更多相关文章

  1. Java使用RabbitMQ之整合Spring(生产者)

    依赖包 <!--RabbitMQ集成spring--> <!-- https://mvnrepository.com/artifact/org.springframework.amq ...

  2. RabbitMQ学习笔记三:Java实现RabbitMQ之与Spring集成

    搭建好maven项目环境,加入RabbitMQ依赖包 <dependency> <groupId>org.springframework.amqp</groupId> ...

  3. Maven整合Spring与Solr

    首先,在maven的pom.xml文件中配置对spring和solrj客户端的依赖: <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  4. 【深度分析】:阿里,腾讯面试题 SpringBoot整合Spring MVC

    Java学习总结 SpringBoot整合Spring MVC 1.SpringMVC概述 MVC(Model–view–controller)是软件工程中的一种软件架构模式,基于此模式把软件系统分为 ...

  5. RabbitMQ入门到进阶(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)

    1.MQ简介 MQ 全称为 Message Queue,是在消息的传输过程中保存消息的容器.多用于分布式系统 之间进行通信. 2.为什么要用 MQ 1.流量消峰 没使用MQ 使用了MQ 2.应用解耦 ...

  6. JMS(java消息服务)整合Spring项目案例

    转载自云栖社区 摘要: Sprng-jms消息服务小项目 所需的包: spring的基础包 spring-jms-xx包 spring-message–xx包 commons-collection-x ...

  7. 【Java EE 学习 81】【CXF框架】【CXF整合Spring】

    一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...

  8. RabbitMQ整合spring

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. Java WebService 教程系列之 Spring 整合 CXF

    Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...

随机推荐

  1. [LeetCode]最大系列(最大正方形221,最大加号标志764)

    221. 最大正方形 题目描述: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 ...

  2. 【Flask】abort和errorhandler、app_errorhandler进行请求中断及自定义异常处理

    在view函数中,如果需要中断request,可以使用abort(500)或者直接raise exception.当然我们还需要返回一个出错信息给前端,所以需要定制一下ErrorHandler.一般只 ...

  3. Spring Security(十六):5.7 Multiple HttpSecurity

    We can configure multiple HttpSecurity instances just as we can have multiple <http> blocks. T ...

  4. Edusoho之LAMP环境搭建

    主要参考官方文档Ubuntu16.04+Apache+PHP+MySQL+EduSoho 安装教程LAMP环境按照如下搭建是没有问题的,本地虚拟机试验是完全没有问题的. 1.更新 sudo apt-g ...

  5. 论文笔记(一)---翻译 Rich feature hierarchies for accurate object detection and semantic segmentation

    论文网址: https://arxiv.org/abs/1311.2524 RCNN利用深度学习进行目标检测. 摘要 可以将ImageNet上的进全图像分类而训练好的大型卷积神经网络用到PASCAL的 ...

  6. redis学习(九)——数据持久化

    一.概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存储在内存中的数据将会丢失,在很多情况下是无法容忍这样的事情的.所以,我们需要将内存中的数据持久 ...

  7. 通过buildroot+qemu搭建ARM-Linux虚拟开发环境

    1. 配置工作环境 sudo apt install gcc build-essential bison flex gettext tcl sharutils libncurses-dev zlib1 ...

  8. Spring Boot 之日志记录

    Spring Boot 之日志记录 Spring Boot 支持集成 Java 世界主流的日志库. 如果对于 Java 日志库不熟悉,可以参考:细说 Java 主流日志工具库 关键词: log4j, ...

  9. RPC与Zookeeper注册中心的简单实现

    连接上文:https://www.cnblogs.com/wuzhenzhao/p/9962250.html RPC框架的简单实现,基于这个小程序,在我学习完Zookeeper之后如何将注册中心与RP ...

  10. sklearn 数据预处理1: StandardScaler

    作用:去均值和方差归一化.且是针对每一个特征维度来做的,而不是针对样本. [注:] 并不是所有的标准化都能给estimator带来好处. “Standardization of a dataset i ...