RabbitMQ学习之spring-amqp的重要类的认识
对于大多数应用来说都做了与spring整合,对于rabbitmq来说。也有与spring的整合。可能通过spring的官网找到spring-amqp项目下载。spring-amqp项目包括三个子项目:spring-amqp、spring-erlang、spring-rabbit.
下面来认识一下spring-amqp中的几个重要类;以spring-amqp-1.0.0.M3版本为例
1、Message : Spring AMQP定义的Message类是AMQP域模型中代表之一。Message类封装了body(消息BODY)和properties(消息属性) 。使得这个API看起来很简单。Message类定义如下:
- public class Message {
- private final MessageProperties messageProperties;
- private final byte[] body;
- public Message(byte[] body, MessageProperties messageProperties) {
- this.body = body;
- this.messageProperties = messageProperties;
- }
- public byte[] getBody() {
- return this.body;
- }
- public MessageProperties getMessageProperties() {
- return this.messageProperties;
- }
- }
其中MessageProperties类中定义了例如messageId、timestamp、contentType等属性。这此属性可以扩展到用户通过setHeader(String key, Object value)方法来自定义“headers”。
2、Exchange
Exchange接口代表一个AMQP的Exchange,决定消息生产者发送消息。每个Exchange都包括一个特定的唯一名字的虚拟主机的代理和一些其他属性。
- public interface Exchange {
- String getName();
- String getType();
- boolean isDurable();
- boolean isAutoDelete();
- Map<String, Object> getArguments();
- }
其中 AbstractExchange类实现了Exchange类。而DirectExchange、TopicExchange、FanoutExchang、HeadersExchange四个类继承AbstractExchange。并重写了getType()类。根据各自相对应的Exchange类型。DirectExchange、TopicExchange、FanoutExchang、HeadersExchange分别对应的类型为direct,topic,fanout,headers.
3、Queue
Queue类是消息消费者接收消息中重要的一个组成部分。通过与Exchange判定来肯定消费者所接收的消息。伪代码如下:
- public class Queue {
- private final String name;
- private volatile boolean durable;
- private volatile boolean exclusive;
- private volatile boolean autoDelete;
- private volatile Map<String, Object> arguments;
- public Queue(String name) {
- this.name = name;
- }
其中name表示队列的名称、durable表示持久性。true表示是。exclusive表示独占性。由于在AmqpTemplate中提供一个方法来得到唯一的队列。这个队列可能是一个”reply-to“地址或者其他信息,因此一般exclusive和autoDelete一般设定为true.
4、Binding
Bingding类通过多种构造参数来判定Exchange,Queue,routingkey;例如
- Binding(Queue queue, FanoutExchange exchange)
- Binding(Queue queue, HeadersExchange exchange, Map<String, Object> arguments)
- Binding(Queue queue, DirectExchange exchange)
- Binding(Queue queue, DirectExchange exchange, String routingKey)
- Binding(Queue queue, TopicExchange exchange, String routingKey)
5、AmqpTemplate
AmqpTemplate是用来发送消息的模板类
- /**
- * Specifies a basic set of AMQP operations.
- *
- * Provides synchronous send and receive methods. The {@link #convertAndSend(Object)} and {@link #receiveAndConvert()}
- * methods allow let you send and receive POJO objects. Implementations are expected to delegate to an instance of
- * {@link MessageConverter} to perform conversion to and from AMQP byte[] payload type.
- *
- * @author Mark Pollack
- * @author Mark Fisher
- */
- public interface AmqpTemplate {
- // send methods for messages
- /**
- * Send a message to a default exchange with a default routing key.
- *
- * @param message a message to send
- * @throws AmqpException if there is a problem
- */
- void send(Message message) throws AmqpException;
- /**
- * Send a message to a default exchange with a specific routing key.
- *
- * @param routingKey the routing key
- * @param message a message to send
- * @throws AmqpException if there is a problem
- */
- void send(String routingKey, Message message) throws AmqpException;
- /**
- * Send a message to a specific exchange with a specific routing key.
- *
- * @param exchange the name of the exchange
- * @param routingKey the routing key
- * @param message a message to send
- * @throws AmqpException if there is a problem
- */
- void send(String exchange, String routingKey, Message message) throws AmqpException;
- // send methods with conversion
- /**
- * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a default routing key.
- *
- * @param message a message to send
- * @throws AmqpException if there is a problem
- */
- void convertAndSend(Object message) throws AmqpException;
- /**
- * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.
- *
- * @param routingKey the routing key
- * @param message a message to send
- * @throws AmqpException if there is a problem
- */
- void convertAndSend(String routingKey, Object message) throws AmqpException;
- /**
- * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange with a specific routing key.
- *
- * @param exchange the name of the exchange
- * @param routingKey the routing key
- * @param message a message to send
- * @throws AmqpException if there is a problem
- */
- void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
- /**
- * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a default routing key.
- *
- * @param message a message to send
- * @param messagePostProcessor a processor to apply to the message before it is sent
- * @throws AmqpException if there is a problem
- */
- void convertAndSend(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
- /**
- * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.
- *
- * @param routingKey the routing key
- * @param message a message to send
- * @param messagePostProcessor a processor to apply to the message before it is sent
- * @throws AmqpException if there is a problem
- */
- void convertAndSend(String routingKey, Object message, MessagePostProcessor messagePostProcessor)
- throws AmqpException;
- /**
- * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange with a specific routing key.
- *
- * @param exchange the name of the exchange
- * @param routingKey the routing key
- * @param message a message to send
- * @param messagePostProcessor a processor to apply to the message before it is sent
- * @throws AmqpException if there is a problem
- */
- void convertAndSend(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor)
- throws AmqpException;
- // receive methods for messages
- /**
- * Receive a message if there is one from a default queue. Returns immediately, possibly with a null value.
- *
- * @return a message or null if there is none waiting
- * @throws AmqpException if there is a problem
- */
- Message receive() throws AmqpException;
- /**
- * Receive a message if there is one from a specific queue. Returns immediately, possibly with a null value.
- *
- * @param queueName the name of the queue to poll
- * @return a message or null if there is none waiting
- * @throws AmqpException if there is a problem
- */
- Message receive(String queueName) throws AmqpException;
- // receive methods with conversion
- /**
- * Receive a message if there is one from a default queue and convert it to a Java object. Returns immediately,
- * possibly with a null value.
- *
- * @return a message or null if there is none waiting
- * @throws AmqpException if there is a problem
- */
- Object receiveAndConvert() throws AmqpException;
- /**
- * Receive a message if there is one from a specific queue and convert it to a Java object. Returns immediately,
- * possibly with a null value.
- *
- * @param queueName the name of the queue to poll
- * @return a message or null if there is none waiting
- * @throws AmqpException if there is a problem
- */
- Object receiveAndConvert(String queueName) throws AmqpException;
- // send and receive methods for messages
- /**
- * Basic RPC pattern. Send a message to a default exchange with a default routing key and attempt to receive a
- * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time
- * limited by a timeout.
- *
- * @param message a message to send
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Message sendAndReceive(Message message) throws AmqpException;
- /**
- * Basic RPC pattern. Send a message to a default exchange with a specific routing key and attempt to receive a
- * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time
- * limited by a timeout.
- *
- * @param routingKey the routing key
- * @param message a message to send
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Message sendAndReceive(String routingKey, Message message) throws AmqpException;
- /**
- * Basic RPC pattern. Send a message to a specific exchange with a specific routing key and attempt to receive a
- * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time
- * limited by a timeout.
- *
- * @param exchange the name of the exchange
- * @param routingKey the routing key
- * @param message a message to send
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Message sendAndReceive(String exchange, String routingKey, Message message) throws AmqpException;
- // send and receive methods with conversion
- /**
- * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default
- * routing key and attempt to receive a response, converting that to a Java object. Implementations will normally
- * set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
- *
- * @param message a message to send
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Object convertSendAndReceive(Object message) throws AmqpException;
- /**
- * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a
- * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
- * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
- *
- * @param routingKey the routing key
- * @param message a message to send
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Object convertSendAndReceive(String routingKey, Object message) throws AmqpException;
- /**
- * Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a
- * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
- * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
- *
- * @param exchange the name of the exchange
- * @param routingKey the routing key
- * @param message a message to send
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Object convertSendAndReceive(String exchange, String routingKey, Object message) throws AmqpException;
- /**
- * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default
- * routing key and attempt to receive a response, converting that to a Java object. Implementations will normally
- * set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
- *
- * @param message a message to send
- * @param messagePostProcessor a processor to apply to the message before it is sent
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Object convertSendAndReceive(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
- /**
- * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a
- * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
- * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
- *
- * @param routingKey the routing key
- * @param message a message to send
- * @param messagePostProcessor a processor to apply to the message before it is sent
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Object convertSendAndReceive(String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
- /**
- * Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a
- * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
- * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
- *
- * @param exchange the name of the exchange
- * @param routingKey the routing key
- * @param message a message to send
- * @param messagePostProcessor a processor to apply to the message before it is sent
- * @return the response if there is one
- * @throws AmqpException if there is a problem
- */
- Object convertSendAndReceive(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
- }
6、AmqpAdmin和RabbitAdmin
用户配置Queue、Exchange、Binding的代理类。代理类会自动声明或创建这些配置信息。
下面这个类用于异步接收消息的处理类
7、MessageConverter 消息转换器类
8、SimpleMessageListenerContainer 监听消息容器类
spring-amqp:
http://projects.spring.io/spring-amqp/
https://github.com/spring-projects/spring-amqp-samples
转载:http://wubin850219.iteye.com/blog/1050251
RabbitMQ学习之spring-amqp的重要类的认识的更多相关文章
- RabbitMQ学习之spring配置文件rabbit标签的使用
下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...
- (转)RabbitMQ学习之spring整合发送同步消息
http://blog.csdn.net/zhu_tianwei/article/details/40890543 以下实现使用Exchange类型为DirectExchange. routingke ...
- (转)RabbitMQ学习之spring整合发送异步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...
- (转) RabbitMQ学习之spring整合发送异步消息
http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...
- (转) RabbitMQ学习之spring整合发送同步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...
- spring amqp初步了解
Rabbitmq简介 生产者会把消息发送给RabbitMQ的交换中心(Exchange),Exchange的一侧是生产者,另一侧则是一个或多个队列,由Exchange决定一条消息的生命周期--发送给某 ...
- Spring AMQP 源码分析 04 - MessageListener
### 准备 ## 目标 了解 Spring AMQP 如何实现异步消息投递(推模式) ## 前置知识 <RabbitMQ入门_05_多线程消费同一队列> ## 相关资源 Quick To ...
- RabbitMQ学习笔记(5)----RabbitMQ整合Spring
在Spring AMQP项目中Spring也提供了对RabbitMQ的支持,这里在之前学习SpringBoot的时候也整合过,但是今天这里使用的Spring的xml配置来整个rabbit. Sprin ...
- 消息中间件——RabbitMQ(九)RabbitMQ整合Spring AMQP实战!(全)
前言 1. AMQP 核心组件 RabbitAdmin SpringAMQP声明 RabbitTemplate SimpleMessageListenerContainer MessageListen ...
- 译: 1. RabbitMQ Spring AMQP 之 Hello World
本文是译文,原文请访问:http://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html RabbitMQ 是一个Brocker (消息队 ...
随机推荐
- js:多种方法实现数组去重
面试的时候数组去重要多种方法实现, 只想到一种判断重复删除的方法,而且还没写对.后来大概看了一下网上的方法. 下午想到一个网上没见过的filter方法,于是整理了一下,基于以前看到的思想,然后用了一些 ...
- BZOJ 3514 GERALD07加强版 (LCT+主席树)
题目大意:给定n个点m条边无向图,每次询问求当图中有编号为[L,R]的边时,整个图的联通块个数,强制在线 神题!(发现好久以前的题解没有写完诶) 我们要求图中联通块的个数,似乎不可搞啊. 联通块个数= ...
- Hexo系列(一) 搭建博客网站
写在前面的话:本系列文章主要参考 Hexo官方说明文档,同时结合自己在使用过程中的一些心得体会,撷取下来,和大家分享分享.好,下面闲话不多说,马上开始我们的 Hexo 之旅吧 温馨提醒:博主使用的操作 ...
- Python实现8中常用排序算法
L = [2,6,4,7,9,1,3,5,8] # 1.插入排序 def insert_sort(List): n = len(List) for i in range(1,n): # 得到索引 j ...
- Windows 2003 IIS 不支持ASP的问题
Windows 2003 IIS 不支持ASP的问题 问题: HTTP 错误 404 - 文件或目录未找到. Internet 信息服务 (IIS) 第一步,启用Asp,进入:控制面板 -> 管 ...
- sqlserver日志文件太大解决方法
SQL Server 的事务日志意外增大或充满的处理方法 事务日志文件Transaction Log File是用来记录数据库更新情况的文件,扩展名为ldf. 在 SQL Server 7.0 和 S ...
- C++开发人脸性别识别教程(19)——界面美化
在这篇博文中将完毕<C++开发人脸性别识别>的收尾工作.主要内容分为两部分:加入视频暂定功能.界面规范化. 一 视频暂停功能 严格来说这个视频暂定功能算是视频人脸性别识别的一个遗留问题,本 ...
- ios swift学习日记4-字符串和字符
近期ios的swift语言好像火了起来,本人没有objectc的基础,但之前是有c跟java的基础的. 从这几天開始学习ios的swift语言,后期以博客形式公布.这里提供一本翻译的英文版的swif书 ...
- luogu3366 【模板】 最小生成树 Prim
题目大意 给出一个无向图,求出最小生成树,如果该图不连通,则输出orz. 概念 对于一个无向图,要求选出一些边,使得图上的每一个节点互相连通,且边权和最小.选出的边与节点形成的子图必然是颗树,这棵树叫 ...
- 软件-集成开发环境:IDE
ylbtech-软件-集成开发环境:IDE 集成开发环境(IDE,Integrated Development Environment )是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器. ...