依赖包:

        <!--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. Python 在 Windows 下安装第三方包,报 Python 未注册的问题解决

    保存一些代码为 reg.py,运行之即可. #/usr/bin/env python # -*- coding: utf-8 -*- import sys from _winreg import * ...

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

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

  3. Linux每天一个命令:iostat

    iostat用于输出CPU和磁盘I/O相关的统计信息 安装Sysstat工具包 centos: yum install sysstat ubuntu: sudo apt-get install sys ...

  4. DOM(二)

    文档信息 document对象还有一些标准的Document对象所没有的属性: title属性:包含着<title>元素中的文本——显示在浏览器窗口的标题栏或标签页上,通过整个属性可以取得 ...

  5. 单点登录SSO:图示和讲解

    目录 概述 示例运行效果动画 跨域Web SSO时序图 代码截图 几个基本概念 涉及的站点和页面 重点理解:单点登录的核心步骤 敢说最准确的单点登录图示,因为: 我严格对照所画时序图的每个步骤,开发了 ...

  6. JVM参数配置 java内存区域

    java内存区域 一些基本概念 http://www.importnew.com/18694.html https://www.cnblogs.com/wangyayun/p/6557851.html ...

  7. Redis中单机数据库的实现

    1. 内存操作层 zmalloc 系接口 redis为了优化内存操作, 封装了一层内存操作接口. 默认情况下, 其底层实现就是最简朴的libc中的malloc系列接口. 如果有定制化需求, 可以通过配 ...

  8. k8s

    https://www.cnblogs.com/sheng-jie/p/10591794.html

  9. SQL 显示表名显示列名

    显示表名:show 表名: 显示列(Field)名:show columns from 表名:

  10. Python全栈开发之路 【第十六篇】:jQuey的动画效果、属性操作、文档操作、input的value

    01-动画效果 show 显示 概念:显示隐藏的匹配元素 语法:show(speed,callback) 参数: speed:三种预定速度之一的字符串('slow','normal','fast')或 ...