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. 小程序插入html代码

    转自:https://blog.csdn.net/BetterGG/article/details/81027291 一.首先,html 分 2 种情况: 1.我在做详情页开发的时候发现获取到的数据是 ...

  2. 01 使用Git基本方法

    什么是Git? Git是目前世界上最先进的分布式版本控制系统(没有之一). 你得先有一个Git仓库,才能进行操作.创库就是Git存放你要保存的快照的数据的地方. 拥有一个Git仓库,有两种方法. 创建 ...

  3. VPS虚拟化架构OpenVZ、KVM、Xen、Hyper-V的区别

    1.OpenVZ OpenVZ(简称OVZ)采用SWsoft的Virutozzo虚拟化服务器软件产品的内核,是基于Linux平台的操作系统级服务器虚拟化架构.这个架构直接调用宿主机(俗称:母机)中的内 ...

  4. golang 记录函数执行耗时的一个简单方法。

    先写一个公共函数, 比如在 common 包下有这么一个方法: // 写超时警告日志 通用方法 func TimeoutWarning(tag, detailed string, start time ...

  5. PHP-redis英文文档

    作为程序员,看英文文档是必备技能,所以尽量还是多看英文版的^^ PhpRedis The phpredis extension provides an API for communicating wi ...

  6. 20个实用便捷的CSS3工具、库及实例

    编者按:坊间传闻,有本CSS的高手炼成秘籍在江湖失传已久,书中所载,多为最新的惊人技术与实例示范,是为集大成者,一旦学成,代码效率猛增,功力提升数倍,今日偶获,不敢怠慢,赶紧发到优设,望人人受益.说人 ...

  7. 公共cdn的js和css库

    使用cdn的js和css公用库 为什么要使用cdn,用cdn资源有什么好处了,可以看看yahoo性能建议,中间有一条的http://developer.yahoo.com/performance/ru ...

  8. Maven学习总结(八):Myecplise中配置maven

    第一步:下载maven安装包,配置环境变量M2_HOME;变量值为maven的解压目录. 第二步:在eclipse4.0之前的版本需要安装maven插件,方法即:将maven插件包复制到eclipse ...

  9. mac上调整phpstorm和webstorm的使用内存(默认是128m-750m) 避免卡顿

    For Mac Only WebStorm/phpstrom用起来一卡一卡,如今才发现是它的默认内存配置太弱.修改之: 修改phpstrom vi /Applications/PhpStorm.app ...

  10. 使用nodeJs安装Vue-cli (win10 使用管理员身份)

    TIP:win10下安装,使用管理员身份进行,否则会有权限限制. 1,安装完成node,node有自带的npm,可以直接在cmd中,找到nodeJs安装的路径下,进行命令行全局安装vue-cli.(n ...