依赖包:

        <!--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. matlab 整局-部视知觉实验(读取excel点阵设计图替换数据)

    -------给我那编程盲的女盆友,我怎么感觉是我选了一门课???做了这么多次作业,还是整理出来吧,要知道双鸭山大学心理系单身妹子还是很多啊. 整体-局部范式是心理学的经典实验之一.请尝试利用 MAT ...

  2. hyperledger中文文档学习-2-简介

    参考https://hyperledgercn.github.io/hyperledgerDocs/blockchain_zh/ Hyperledger区块链框架(https://blog.csdn. ...

  3. ASM problem : ORA-15001: diskgroup "DGROUP1" does not exist or is not mounted ORA-15040: diskgroup is incomplete

    ============================================================= mos中的详细解释: ODA: After Apply ODA 12.2.1 ...

  4. ORA-20011 ORA-29913 KUP-11024 GATHER_TABLE_STATS

    --alter 日志Sat Mar 30 22:01:08 2019DBMS_STATS: GATHER_STATS_JOB encountered errors. Check the trace f ...

  5. Spring AOP无法拦截Controller中的方法

    想使用AOP Annotation配置Spring MVC的Controller进行拦截, 发现无法拦截Controller的方法, 却可以拦截Service层的方法. 一开始: Spring的配置文 ...

  6. [MicroPython]TPYBoardv102自动浇花系统

    1.系统功能 监测土壤湿度.环境温度.光照强度 根据当前环境自动浇水,寒冷天气自动加热土壤 2.所需元器件 TPYBoard板子1块 光敏模块1块 DS18B20模块1块 土壤湿度检测模块1块 杜邦线 ...

  7. Git中删除指定文件

    之前的博客Git基础使用教程介绍了Git这个开源分布式管理系统的一些基础操作,这篇博客,介绍下如何利用Git删除远程仓库的文件... 1.拉取远程仓库的文件到本地 git clone xxx 如果还未 ...

  8. Unity编辑器:自定义编辑器样式——GUIStyle

    通过GUIStyle,可以自定义Unity编辑器的样式. GUIStyle可以new一个全新的实例,这样,需要自己处理所有自己需要的效果. GUIStyle还可以基于已经存在的实例new一个新的实例, ...

  9. Autofac踩坑经历

    背景 接口框架使用反射,动态生成Controller,使用Autofac进行依赖注入,并替换默认DependencyResolver及IControllerFactory,Controller实例化代 ...

  10. 朱晔和你聊Spring系列S1E5:Spring WebFlux小探

    阅读PDF版本 本文会来做一些应用对比Spring MVC和Spring WebFlux,观察线程模型的区别,然后做一下简单的压力测试. 创建一个传统的Spring MVC应用 先来创建一个新的web ...