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 100days学习笔记008-2:模块
目录 Day008_02:模块 1.import语句 1.1from - import 语句 1.2 from - import * 语句 2.深入模块 2.1 __name__属性 2.2 dir( ...
- kafka producer interceptor拦截器(五)
producer在发送数据时,会经过拦截器和序列化,最后到达相应的分区.在经过拦截器时,我们可以对发送的数据做进步的处理. 要正确的使用拦截器需要以下步骤: 1.实现拦截器ProducerInterc ...
- 初识RedisCluster集群
为什么需要Redis集群 需要提高更大的并发量 Redis官方提出拥有10万QPS的请求量 如果业务需要Redis拥有100万的QPS 可以通过集群来提升并发量. 需要存储更大的数据量 一般服务器的机 ...
- 【AtCoder】AGC033(A-F)
AGC033 A - Darker and Darker 直接BFS #include <bits/stdc++.h> #define fi first #define se second ...
- 数据结构 -- 栈(Stack)
一.栈的简介 定义 栈(英语:stack)又称为堆栈或堆叠,栈作为一种数据结构,它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据 ...
- 第十二章 ZYNQ-MIZ701 PL中断请求
本篇文章主要介绍外设(PL)产生的中断请求,在PS端进行处理. 在PL端通过按键产生中断,PS接受到之后点亮相应的LED. 本文所使用的开发板是Miz701 PC 开发环境版本:Vivado 20 ...
- 怎样修改一个已存在的Cookie
Cookie的修改也需要借助 Response-Header 的 Set-Cookie 字段, 不过需要注意的是: 待修改cookie的 key / domain / path / secure 必须 ...
- dev gridview 单元格值拖拽替换
public class GridViewDropCell { //dvginfo根据鼠标点击的x.y坐标获取该点的相关信息 private GridHitInfo downHitInfo; priv ...
- react用高阶组件实现路由守卫
react-router不像vue-router一样有很多钩子函数,可以做路由守卫.想实现路由守卫,可以用高阶组件来实现. @connect(state => ({ isLogin: state ...
- FFmpeg里面的时间单位
pts单位:1/90 ms(每个单位代表1/90 ms) RTP包头有个STAMP 对于视频 STAMP/90 就是 PTS (毫秒) 对于音频 STAMP/samplerate * 1000 才是 ...