9、RabbitMQ-集成Spring
spring封装RabbitMQ看官网:https://spring.io/projects/spring-amqp#learn
开发时根据官网的介绍进行开发,具体的说明都有具体的声明
1、导入依赖
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1..RELEASE</version>
</dependency>
2、配置文件
config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 、定义RabbitMQ的连接工厂 -->
<rabbit:connection-factory id="connectionFactory" host="192.168.1.130"
username="user" password="user" virtual-host="/user" port=""/> <!-- 消息发送到交换机还是队列 -->
<!-- 、定义Rabbit模板指定连接的工厂以及定义的exchange -->
<!-- 可以指定消息发送到交换机还是队列 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="spring_exchange" ></rabbit:template> <!-- MQ的管理:包括队列、交换机等 -->
<!-- 如交换机、队列是否存在,是否需要进行创建 -->
<rabbit:admin connection-factory="connectionFactory"/> <!-- 定义队列 -->
<!-- auto-declare:自动声明,交换机不存在的时候进行创建,存在不声明 -->
<rabbit:queue name="myQueue" auto-declare="true"></rabbit:queue> <!-- 定义交换机 -->
<!-- auto-declare:自动声明,交换机不存在的时候进行创建,存在不声明 -->
<rabbit:fanout-exchange name="spring_exchange" auto-declare="true" >
<!-- 将队列绑定到交换机 -->
<rabbit:bindings>
<rabbit:binding queue="myQueue"></rabbit:binding>
</rabbit:bindings>
</rabbit:fanout-exchange> <!-- 定义监听容器,当收到消息的时候会执行内部的配置 -->
<rabbit:listener-container connection-factory="connectionFactory">
<!-- 定义那个方法用于用于处理到接收到的消息 -->
<rabbit:listener ref="consumer" method="listen" queue-names="myQueue"/>
</rabbit:listener-container> <!-- 消费者 -->
<bean id="consumer" class="com.rabbitmq.spring.Receive"></bean>
</beans>
3、消费者
package com.rabbitmq.spring; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Receive {
//接受消息
public void listen(String foo){
System.out.println("foo:" + foo);
}
}
4、测试类
package com.rabbitmq.spring;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class test {
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:/config.xml");
RabbitTemplate template = app.getBean(RabbitTemplate.class);
template.convertAndSend("hello foo");
app.close();
System.out.println("send...");
} }
结果如图:

详细具体可以参考:https://blog.csdn.net/leixiaotao_java/article/details/78952930
9、RabbitMQ-集成Spring的更多相关文章
- 消息中间件系列四:RabbitMQ与Spring集成
一.RabbitMQ与Spring集成 准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...
- RabbitMQ与spring集成,配置完整的生产者和消费者
RabbitMQ与AMQP协议详解可以看看这个 http://www.cnblogs.com/frankyou/p/5283539.html 下面是rabbitMQ和spring集成的配置,我配置了二 ...
- RabbitMQ入门教程(十六):RabbitMQ与Spring集成
原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...
- MyBatis6:MyBatis集成Spring事物管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...
- CXF集成Spring实现webservice的发布与请求
CXF集成Spring实现webservice的发布(服务端) 目录结构: 主要代码: package com.cxf.spring.pojo; public class User { int id ...
- Dubbo集成Spring与Zookeeper实例
>>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...
- Thymeleaf 集成spring
Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...
- 06_在web项目中集成Spring
在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...
- Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)
1.非集成Spring hibernate的检索方式,主要有以下五种. 1.导航对象图检索方式.(根据已经加载的对象,导航到其他对象.) 2.OID检索方式.(按照对象的OID来检索对象.) 3.HQ ...
- SpringMVC 3.1集成Spring Security 3.1
这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...
随机推荐
- Docker学习(四): 操作容器
特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! =============系列文章============= 1 ...
- HDU 2084(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=2084 状态转移方程: dp[i][j] = MAX(dp[i+1][j],dp[i+1][j+1])+tower ...
- Oracle数据库基本操作(二) —— 视图、序列、索引、同义词
一.视图(Views)与 同义词 1.视图:实际上是对查询结果集的封装,视图本身不存储任何数据,所有的数据都存放在原来的表中; 在逻辑上可以把视图看作是一张表 2.作用: 封装查询语句,简化复杂的查询 ...
- MySQL:PyMySQL&ORM
一.PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. Django中也可以使用PyMySQL连接MySQ ...
- sql 传入参数为逗号分隔的字符串处理方法
写了个存储过程,中间用到了类似这种写法 Select * From User Where ID In('1,2,3') 其中'1,2,3'是从外面传进来的参数,就这样执行报错:'1,2,3'转换为in ...
- Vue打包桌面程序
开源的地址:https://github.com/electron/electron-quick-start 一.运行 1. 安装依赖 cnpm install electron --save cnp ...
- Git 拉取Gitee仓库报错:“fatal: unable to access ''": Failed to connect to 127.0.0.1 port 1080: Connection refused”
1.报错信息: 2.本地查看是否Git使用了代理 git config --global http.proxy 3.取消代理 git config --global --unset http.prox ...
- XML与web开发-01- 在页面显示和 XML DOM 解析
前言: 关于 xml 特点和基础知识,可以菜鸟教程进行学习:http://www.runoob.com/xml/xml-tutorial.html 本系列笔记,主要介绍 xml 在 web 开发时需要 ...
- 8.Bootstrap CSS编码规范
Bootstrap CSS编码规范 本节的介绍内容为 Bootstrap 中的 CSS 编码规范. 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得一致展现的方法. 为选择 ...
- 用FileZilla Server开FTP
FileZilla(教程)是经典的开源FTP解决方案,包括FileZilla客户端和FileZilla Server.其中,FileZilla Server的功能比起商业软件FTP Serv-U毫不逊 ...