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的更多相关文章

  1. 消息中间件系列四:RabbitMQ与Spring集成

    一.RabbitMQ与Spring集成  准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...

  2. RabbitMQ与spring集成,配置完整的生产者和消费者

    RabbitMQ与AMQP协议详解可以看看这个 http://www.cnblogs.com/frankyou/p/5283539.html 下面是rabbitMQ和spring集成的配置,我配置了二 ...

  3. RabbitMQ入门教程(十六):RabbitMQ与Spring集成

    原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  4. MyBatis6:MyBatis集成Spring事物管理(下篇)

    前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...

  5. CXF集成Spring实现webservice的发布与请求

    CXF集成Spring实现webservice的发布(服务端) 目录结构: 主要代码: package com.cxf.spring.pojo; public class User { int id ...

  6. Dubbo集成Spring与Zookeeper实例

    >>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...

  7. Thymeleaf 集成spring

    Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...

  8. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

  9. Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)

    1.非集成Spring hibernate的检索方式,主要有以下五种. 1.导航对象图检索方式.(根据已经加载的对象,导航到其他对象.) 2.OID检索方式.(按照对象的OID来检索对象.) 3.HQ ...

  10. SpringMVC 3.1集成Spring Security 3.1

    这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...

随机推荐

  1. 三、hdfs的JavaAPI操作

    下文展示Java的API如何操作hdfs,在这之前你需要先安装配置好hdfs https://www.cnblogs.com/lay2017/p/9919905.html 依赖 你需要引入依赖如下 & ...

  2. 撩课-Python-每天5道面试题-第2天

    一. 简述编程过程中, 注释的作用? (1) 方便开发人员自己理清楚代码思路 因为开发人员在拿到一个需求时, 首先应该思考的是如何将需求问题, 分解成具体的实施步骤; 第一步干啥, 第二步干啥, 第三 ...

  3. 高并发系列之——缓存中间件Redis

    1 概念和使用场景 下载路径 2 基本存储类型 String List Set SortedSet Hash 3 事务 单线程执行,即只能保证一个client发起的事务中的命令可以连续的执行,而中间不 ...

  4. MyBatis缓存通俗易懂

    1.1     mybatis缓存介绍 如下图,是mybatis一级缓存和二级缓存的区别图解: Mybatis一级缓存的作用域是同一个SqlSession,在同一个sqlSession中两次执行相同的 ...

  5. springcloud 实战 feign使用中遇到的相关问题

    springcloud 实战 feign使用中遇到的相关问题 1.使用feign客户端调用其他微服务时,session没有传递成功,sessionId不一样. /** * @author xbchen ...

  6. ActiveMQ的用途

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线. ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现. 消息队列的主要作用是为了 ...

  7. js 数据监听--对象的变化

    class Observer { constructor(data) { this.data = data; this.filterObj(data); } static isObject(obj) ...

  8. sql-(Cross||Outer)Apply

    Apply - 涉及以下两个步骤中的一步或两步(取决于Apply的类型): 1.A1:把右表表达式应用于左表的行 2.A2:添加外部行 Apply运算符把右表表达式应用于左输入的每一行.右表达式可以引 ...

  9. 我为什么用docker-compose来打包开发环境

    BUILD, SHIP, RUN Docker is the world's leading software containerization platform Docker的出现,让虚拟技术更上一 ...

  10. 使用WICleanup清理Windows Installer 冗余文件

    使用WICleanup清理Windows Installer 冗余文件 | 浏览:816 | 更新:2015-11-02 10:43 | 标签:Win7 Win10 1 2 3 4 5 6 7 分步阅 ...