(二)RabbitMQ使用笔记
1.RabbitMQ简介
RabbitMQ是流行的开源消息队列系统,用erlang语言开发。RabbitMQ是AMQP(高级消息队列协议)的标准实现。
官网:http://www.rabbitmq.com/
2.Spring集成RabbitMQ
2.1pom.xml文件:
<!--rabbitmq依赖 -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!-- 如果没有这段,上面也会将其拉进来 -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!-- 这个必要的 -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.6</version>
</dependency>
2.2 spring-mq.properties
rmq.host = 192.168.XX.XX //rabbitmq服务器ip地址
rmq.port = 5672 //端口
rmq.producer.num = 20 //发消息生产者的最大数,没有这个需求可以不写
rmq.user = admin //用户名
rmq.password = admin //密码
2.3 spring-mq.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd"> <rabbit:connection-factory id="connectionFactory"
host="${rmq.host}" username="${rmq.user}" password="${rmq.password}"
port="${rmq.port}" /> <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
<rabbit:admin connection-factory="connectionFactory" /> <!--队列 -->
<!-- 说明: durable:是否持久化 exclusive: 仅创建者可以使用的私有队列,断开后自动删除 auto_delete: 当所有消费客户端连接断开后,是否自动删除队列 -->
<rabbit:queue name="my_first_queue" auto-declare="true" durable="true" /> <!-- 任务下发交换机 -->
<!-- 说明: rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。
rabbit:binding:设置消息queue匹配的key -->
<rabbit:direct-exchange name="mq-exchange" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding key="my_first_queue" queue="my_first_queue" />
</rabbit:bindings>
</rabbit:direct-exchange> <!-- 消息转换器声明 消息对象json转换类 -->
<bean id="jsonMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> <!-- 消费者 -->
<bean id="myCustomer" class="com.ln.mq.Customer">
<!-- 消费者方法要有相应的set方法 -->
<property name="converter" ref="jsonMessageConverter" />
</bean> <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
<rabbit:listener-container
connection-factory="connectionFactory" acknowledge="manual">
<rabbit:listener queues="my_first_queue" ref="myCustomer" />
</rabbit:listener-container> <!-- spring template声明 -->
<rabbit:template id="amqpTemplate" exchange="mq-exchange" connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
</beans>
说明:
<rabbit:queue id="test_queue_key" name="test_queue_key" durable="true" auto-delete="false" exclusive="false" />
durable:是否持久化
exclusive: 仅创建者可以使用的私有队列,断开后自动删除
auto_delete: 当所有消费客户端连接断开后,是否自动删除队列
<rabbit:direct-exchange name="test-mq-exchange" durable="true" auto-delete="false" id="test-mq-exchange">
<rabbit:bindings>
<rabbit:binding queue="test_queue_key" key="test_queue_key"/>
</rabbit:bindings>
</rabbit:direct-exchange>
rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。
rabbit:binding:设置消息queue匹配的key
2.4 web.xml
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml
classpath*:spring-mq.xml
</param-value>
</context-param>
3.测试
3.1 生产者测试类
package com.ln.mq; import javax.annotation.Resource; import org.junit.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired; import com.ln.web.controller.TestBase; /**
* 生产者
*
*/
public class Producer extends TestBase{ private static final String MY_FITST_QUEUE="my_first_queue"; @Resource
private AmqpTemplate amqpTemplate; @Test
public void sendMessage(){
System.out.println("*******生产者********");
String message="hello my first queue";
amqpTemplate.convertAndSend(MY_FITST_QUEUE, message);
} }
3.2 消费者测试类
package com.ln.mq; import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.support.converter.MessageConverter; import com.rabbitmq.client.Channel; /**
* 消费者
*
*/
public class Customer implements ChannelAwareMessageListener{
protected MessageConverter converter; public void setConverter(MessageConverter converter) {
this.converter = converter;
}
@Override
public void onMessage(Message message, Channel channel) throws Exception {
Object fromMessage = converter.fromMessage(message);
System.out.println("***********消费者********");
System.out.println("***********接收到的Message:"+fromMessage.toString());
}
}
3.3 运行
运行生产者Producer测试junit
*******生产者********
*******消费者********
*******接收到的Message:hello my first queue
3.4 rabbitmq视图
可以查看队列消息情况
地址:http://localhost:15672
我的是本地的服务,如果要链接远程服务,localhost换成服务器ip。
(二)RabbitMQ使用笔记的更多相关文章
- 简述C#中IO的应用 RabbitMQ安装笔记 一次线上问题引发的对于C#中相等判断的思考 ef和mysql使用(一) ASP.NET/MVC/Core的HTTP请求流程
简述C#中IO的应用 在.NET Framework 中. System.IO 命名空间主要包含基于文件(和基于内存)的输入输出(I/O)服务的相关基础类库.和其他命名空间一样. System.I ...
- 官网英文版学习——RabbitMQ学习笔记(十)RabbitMQ集群
在第二节我们进行了RabbitMQ的安装,现在我们就RabbitMQ进行集群的搭建进行学习,参考官网地址是:http://www.rabbitmq.com/clustering.html 首先我们来看 ...
- 官网英文版学习——RabbitMQ学习笔记(一)认识RabbitMQ
鉴于目前中文的RabbitMQ教程很缺,本博主虽然买了一本rabbitMQ的书,遗憾的是该书的代码用的不是java语言,看起来也有些不爽,且网友们不同人学习所写不同,本博主看的有些地方不太理想,为此本 ...
- 毕业设计 之 二 PHP学习笔记(一)
毕业设计 之 二 PHP学习笔记(一) 作者:20135216 平台:windows10 软件:XAMPP,DreamWeaver 一.环境搭建 1.XAMPP下载安装 XAMPP是PHP.MySQL ...
- 【基于spark IM 的二次开发笔记】第一天 各种配置
[基于spark IM 的二次开发笔记]第一天 各种配置 http://juforg.iteye.com/blog/1870487 http://www.igniterealtime.org/down ...
- RabbitMQ学习笔记(五) Topic
更多的问题 Direct Exchange帮助我们解决了分类发布与订阅消息的问题,但是Direct Exchange的问题是,它所使用的routingKey是一个简单字符串,这决定了它只能按照一个条件 ...
- 《Linux内核设计与实现》 第一二章学习笔记
<Linux内核设计与实现> 第一二章学习笔记 第一章 Linux内核简介 1.1 Unix的历史 Unix的特点 Unix很简洁,所提供的系统调用都有很明确的设计目的. Unix中一切皆 ...
- 《Linux内核设计与实现》第一、二章学习笔记
<Linux内核设计与实现>第一.二章学习笔记 姓名:王玮怡 学号:20135116 第一章 Linux内核简介 一.关于Unix ——一个支持抢占式多任务.多线程.虚拟内存.换页.动态 ...
- RabbitMQ学习笔记1-hello world
安装过程略过,一搜一大把. rabbitmq管理控制台:http://localhost:15672/ 默认账户:guest/guest RabbitMQ默认监听端口:5672 JAVA API地 ...
随机推荐
- ATM:模拟实现一个ATM + 购物商城程序
额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 提供还款接口 ATM记录操作日志 提供管理接 ...
- mysql索引原理与慢查询优化1
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- 字符串,字符数组(C/C++)
这个地方困惑我好久了,废话不多说 char c1[]="12345"; char *c2="12345"; string c3="12345" ...
- python's eighth day for me
f : 变量,f_obj, file, f_handler,...文件句柄. open : windows 的系统功能. windows 默认编码方式:gbk. Linux 默认编码方式:utf ...
- HALCON 算子函数(四) File
HALCON 算子函数——Chapter 4 : File 4.1 Images 1. read_image 功能:读取有不同文件格式的图像. 2. read_sequence 功能:读取图像. 3. ...
- Mac mysql-忘记数据库密码
第一步: 关闭mysql服务:苹果->系统偏好设置最下边点mysql 在弹出页面中关闭mysql服务(点击stop mysql server) 第二步: 进入终端输入:cd /usr/local ...
- 常用的软件设计模式的Java实现——让编程从野生到飞起
常用的软件设计模式的Java实现——让编程从野生到飞起_野生技术协会_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av7596511/
- array_splice()函数 ,删除数组中的某个值
array_splice() 这个函数是真的皮,有好多种方法,但是最后还是在PHP官方的文档找到了合理的解释的用法 花了大概半个小时 $arr = array('a','b','c','d'); ar ...
- Content 控件
转自:http://www.cnblogs.com/superfang/archive/2008/06/29/1232158.html 创建一个服务器控件,该控件包含呈现到母版页中的 ContentP ...
- jquery中的cookie使用
一.检测cookie是否启用 通过navigator.cookieEnabled这个属性实现,若该值为true,则当前cookie是启用的,反之是禁用的,注意:此属性不是一个标准的属性 二.cooki ...