1.引入相关jar包
|
//RabbitMQ
compile group: 'org.springframework.amqp', name: 'spring-rabbit', version: '1.6.6.RELEASE' compile group: 'org.springframework.integration', name: 'spring-integration-amqp', version: '4.3.5.RELEAS
|
生产者配置
2.实现一个消息处理器,继承自org.springframework.amqp.core.MessageListener
public class AmqpMsgListener implements MessageListener {
@Override public void onMessage(Message message) { System.out.println(message.toString()); } }
|
3.rabbit-producer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:int="http://www.springframework.org/schema/integration" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd"> <!--连接工厂--> <rabbit:connection-factory id="connectionFactory" host="{ip地址}" port="{端口}" username="{用户名}" password="{密码}" publisher-confirms="true"/> <!--创建队列--> <rabbit:queue name="queue.test" durable="true" exclusive="false" auto-delete="false" /> <!--创建分发交换器--> <rabbit:direct-exchange name="exchange.directTest" durable="true"> <rabbit:bindings> <rabbit:binding key="foo.bar" queue="queue.test"></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="exchange.directTest" routing-key="foo.bar" message-converter="jsonMessageConverter" confirm-callback=""/> <rabbit:admin connection-factory="connectionFactory" id="adminId"/> <!-- 配置exchange,不同的exchange会影响消息分发策略 --> <!-- 消息对象json转换类 --> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
</beans>
|
消费者配置
4.rabbit-customer.xml
<?xml version="1.0" encoding="UTF-8"?> <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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!-- rabbitmq连接配置 --> <rabbit:connection-factory id="connectionFactory1" host="{ip地址}" port="{端口}" username="{用户名}" password="{密码}" publisher-confirms="true"/>
<rabbit:admin connection-factory="connectionFactory1"/>
<!--按项目需求配置 --> <rabbit:listener-container connection-factory="connectionFactory1" acknowledge="auto"> <rabbit:listener ref="amqpMsgListener" queues="queue.test" /> </rabbit:listener-container> <bean id="amqpMsgListener" class="com.nxin.farm.test.AmqpMsgListener"/> </beans>
|
5.创建测试类
public class MqTest extends BaseJunit { @Autowired private RabbitTemplate amqpTemplate;
@Test public void testSend() { try { for(int i=0;i<100;i++){ amqpTemplate.convertAndSend("nx.farm.exchange.directTest", "foo.bar", "Hello, world! send by xxxxx"); } Thread.sleep(1000*1000); } catch (AmqpException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
|
- RabbitMQ与spring集成,配置完整的生产者和消费者
RabbitMQ与AMQP协议详解可以看看这个 http://www.cnblogs.com/frankyou/p/5283539.html 下面是rabbitMQ和spring集成的配置,我配置了二 ...
- 消息中间件系列四:RabbitMQ与Spring集成
一.RabbitMQ与Spring集成 准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...
- RabbitMQ入门教程(十六):RabbitMQ与Spring集成
原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...
- 消息队列RabbitMQ与Spring集成
1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...
- rabbitmq 和Spring 集成 实现(一)
1.增加pom.xml依赖 <!--rabbitmq消息队列依赖架包--> <dependency> <groupId>org.springframework.am ...
- RabbitMQ与Spring集成
RabbitMQ服务端安装: https://blog.csdn.net/hzw19920329/article/details/53156015 与Spring集成 https://www.cnbl ...
- Activiti配置实例以及Spring集成配置
public class TestDB { public static void main(String[] args) { //1. 创建Activiti配置对象的实例 ProcessEngineC ...
- RabbitMQ ——与Spring集成及exchange的direct、topic方式实现和简单队列实现
程序整体结构 Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...
- ActiveMQ Spring 集成配置
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms&l ...
随机推荐
- Python openCV基础操作
1.图片加载.显示和保存 import cv2 # 读取图片 img = cv2.imread("img1.jpg") # 生成灰色图片 imgGrey = cv2.imread( ...
- 《MIT 6.828 Homework 2: Shell》解题报告
Homework 2的网站链接:MIT 6.828 Homework 2: shell 题目 下载sh.c文件,在文件中添加相应代码,以支持以下关于shell的功能: 实现简单shell命令,比如ca ...
- loj 10117 简单题
#include<iostream> #include<cstdio> #include<cctype> using namespace std; inline i ...
- python time模块认识
time 模块 -- 时间获取和转换 time 模块提供各种时间相关的功能 在python中, 与时间处理有关的模块包括: time, datatime 以及 calendar 必要说明!: 虽然这个 ...
- 【AC自动机】洛谷三道模板题
[题目链接] https://www.luogu.org/problem/P3808 [题意] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. [题解] 不再介绍基础知识了,就是裸的模 ...
- MySQL SELECT语法(四)UNION语法详解
源自MySQL 5.7 官方手册:13.2.9.3 UNION Syntax 一.UNION语法 UNION用于将多个SELECT语句的结果合并到一个结果集中. SELECT ... UNION [A ...
- 怎样理解ECMAScript 和 JavaScript的关系
JavaScript可以分为三大部分: 1. 核心语法 2. DOM 3. BOM 而核心语法实际上就是指的ECMAScript, 而JS又是不断在发展的, 而这个发展实际上最主要的就是ECMAScr ...
- WP8的新功能-通过一个程序来启动另一个程序
Wp8对原来的WP7做了大量的优化...其中一个就包括Protocol Association,也就是通过uri来打开另外一个程序,这也就是说,我们可以做一个程序来启动另外一个程序了,如微信,QQ之类 ...
- [NOIP10.6模拟赛]1.merchant题解--思维+二分
题目链接: while(1)gugu(while(1)) 闲扯 考场上怕T2正解写挂其他两题没管只打了暴力,晚上发现这题思维挺妙的 同时想吐槽出题人似乎热衷卡常...我的巨大常数现在显露无疑QAQ 分 ...
- canvas-绘制矩形-读书笔记
使用<canvas>元素,必须先设置其width和height属性,指定可以绘图的区域大小. 要在画布上绘图,需要取得绘图上下文,也就是要调用getContext()方法并传入上下文的名字 ...