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. Vue 多路由文件的合并

    Vue 多路由文件的合并 1.使用的是ES6 数组的合并方法 let routes = new Set([...routes1, ...homerouters]);2.两个路由文件,导出的实际上就是一 ...

  2. C#Winform实时更新数据库信息Demo(使用Scoket)

    最近在贴吧上看到有个提问就是关于怎么在Winform上实时的更新数据 提问者提到的是利用Timer去轮询,但最后经过网上查了下资料,感觉Socket也是可行的, 于是就写了这个Demo 这个Demo的 ...

  3. 06-码蚁JavaWeb之Servlet生命周期与基本配置

    学习地址:[撩课-JavaWeb系列1之基础语法-前端基础][撩课-JavaWeb系列2之XML][撩课-JavaWeb系列3之MySQL][撩课-JavaWeb系列4之JDBC][撩课-JavaWe ...

  4. Mybatis之简单注解

    Mybatis使用注解实现主键自增长: oracle: @SelectKey(statement="select my_seq.nextval from dual",resultT ...

  5. mybatis之typehandles

    mybatis之typehandles 无论是Mybatis在预处理语句PreparedStatement中设置一个参数时,还是从结果集中取出一个值时,都会用类型处理器将获取的值以合适的方式转换成ja ...

  6. 鸟哥linux私房菜学习笔记 第二章知识点

    2.1 linux一切皆文件 2.2 磁盘分区 磁盘即文件 2.2.1 磁盘连接的方式与设备文件名的关系 模糊 1.正常的实体机器大概使用的都是 /dev/sd[a-] 的磁盘文件名,至于虚拟机环境下 ...

  7. DLL文件

    Dll文件的全称是Dynamic Link Library,中文意思为动态链接库,DLL文件是不可执行文件,其是一个包含由多个程序同时使用的代码和数据的库,动态链接提供了一种方法,使进程可以调用不属于 ...

  8. C# 简单的loading提示控件

    自己画一个转圈圈的控件 using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...

  9. Linux / mysql: is it safe to copy mysql db files with cp command from one db to another?

    Copying is very simple for MyISAM and completely 100% risky (near suicidal) with InnoDB. From your q ...

  10. mac ASP.NET5

    不写1行代码,在Mac上体验ASP.NET 5的最简单方法   昨天微软发布了ASP.NET 5 beta2(详见ASP.NET 5 Beta2 发布),对ASP.NET 5的好奇心又被激发了. 今天 ...