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 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...
随机推荐
- nodejs简易代理服务器
直接代码: var http = require('http') var proxy = http.createServer(function (request, response) { var op ...
- 【8】memcached实例
一.memcached环境搭建 1.下载后解压到D:\memcached(下载地址:memcached-win64下载地址) 2.安装到windows服务,打开cmd命令行,进入memcached目录 ...
- 二、hdfs单节点安装
一.准备环境 在配置hdfs之前,我们需要先安装好hadoop的配置,本文主要讲述hdfs单节点的安装配置. hadoop的单节点安装配置请参考:https://www.cnblogs.com/lay ...
- 一、hadoop单节点安装测试
一.hadoop简介 相信你或多或少都听过hadoop这个名字,hadoop是一个开源的.分布式软件平台.它主要解决了分布式存储(hdfs)和分布式计算(mapReduce)两个大数据的痛点问题,在h ...
- Spring中三个重要概念 IOC AOP Bean
Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...
- 6 springboot Docker 部署
安装请参考其他的教程请参考http://www.runoob.com/docker/centos-docker-install.htm 拉取zookeeper镜像 docker pull zookee ...
- JavaScript根据国家二字码获取国家全称
function getCountryNameByCode(code) { var countryName = ''; switch(code) { case "US": coun ...
- 武汉邀请赛 Key Logger 双向链表
Key Logger Time Limit: 3000ms Case Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO form ...
- JavaWeb学习总结(六):HttpServletRespone对象(二)
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类, 生成随机图片范例: package gacl.r ...
- ORA-16014: log 3 sequence# 540 not archived, no available destinations
https://blog.csdn.net/zonelan/article/details/7329369