对于大多数应用来说都做了与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类定义如下:

  1. public class Message {
  2. private final MessageProperties messageProperties;
  3. private final byte[] body;
  4. public Message(byte[] body, MessageProperties messageProperties) {
  5. this.body = body;
  6. this.messageProperties = messageProperties;
  7. }
  8. public byte[] getBody() {
  9. return this.body;
  10. }
  11. public MessageProperties getMessageProperties() {
  12. return this.messageProperties;
  13. }
  14. }

其中MessageProperties类中定义了例如messageId、timestamp、contentType等属性。这此属性可以扩展到用户通过setHeader(String key, Object value)方法来自定义“headers”。

 2、Exchange
      Exchange接口代表一个AMQP的Exchange,决定消息生产者发送消息。每个Exchange都包括一个特定的唯一名字的虚拟主机的代理和一些其他属性。

  1. public interface Exchange {
  2. String getName();
  3. String getType();
  4. boolean isDurable();
  5. boolean isAutoDelete();
  6. Map<String, Object> getArguments();
  7. }

其中 AbstractExchange类实现了Exchange类。而DirectExchange、TopicExchange、FanoutExchang、HeadersExchange四个类继承AbstractExchange。并重写了getType()类。根据各自相对应的Exchange类型。DirectExchange、TopicExchange、FanoutExchang、HeadersExchange分别对应的类型为direct,topic,fanout,headers.

 3、Queue
 Queue类是消息消费者接收消息中重要的一个组成部分。通过与Exchange判定来肯定消费者所接收的消息。伪代码如下:

  1. public class Queue {
  2. private final String name;
  3. private volatile boolean durable;
  4. private volatile boolean exclusive;
  5. private volatile boolean autoDelete;
  6. private volatile Map<String, Object> arguments;
  7. public Queue(String name) {
  8. this.name = name;
  9. }

其中name表示队列的名称、durable表示持久性。true表示是。exclusive表示独占性。由于在AmqpTemplate中提供一个方法来得到唯一的队列。这个队列可能是一个”reply-to“地址或者其他信息,因此一般exclusive和autoDelete一般设定为true.

4、Binding
     Bingding类通过多种构造参数来判定Exchange,Queue,routingkey;例如

  1. Binding(Queue queue, FanoutExchange exchange)
  2. Binding(Queue queue, HeadersExchange exchange, Map<String, Object> arguments)
  3. Binding(Queue queue, DirectExchange exchange)
  4. Binding(Queue queue, DirectExchange exchange, String routingKey)
  5. Binding(Queue queue, TopicExchange exchange, String routingKey)

5、AmqpTemplate
AmqpTemplate是用来发送消息的模板类

  1. /**
  2. * Specifies a basic set of AMQP operations.
  3. *
  4. * Provides synchronous send and receive methods. The {@link #convertAndSend(Object)} and {@link #receiveAndConvert()}
  5. * methods allow let you send and receive POJO objects. Implementations are expected to delegate to an instance of
  6. * {@link MessageConverter} to perform conversion to and from AMQP byte[] payload type.
  7. *
  8. * @author Mark Pollack
  9. * @author Mark Fisher
  10. */
  11. public interface AmqpTemplate {
  12. // send methods for messages
  13. /**
  14. * Send a message to a default exchange with a default routing key.
  15. *
  16. * @param message a message to send
  17. * @throws AmqpException if there is a problem
  18. */
  19. void send(Message message) throws AmqpException;
  20. /**
  21. * Send a message to a default exchange with a specific routing key.
  22. *
  23. * @param routingKey the routing key
  24. * @param message a message to send
  25. * @throws AmqpException if there is a problem
  26. */
  27. void send(String routingKey, Message message) throws AmqpException;
  28. /**
  29. * Send a message to a specific exchange with a specific routing key.
  30. *
  31. * @param exchange the name of the exchange
  32. * @param routingKey the routing key
  33. * @param message a message to send
  34. * @throws AmqpException if there is a problem
  35. */
  36. void send(String exchange, String routingKey, Message message) throws AmqpException;
  37. // send methods with conversion
  38. /**
  39. * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a default routing key.
  40. *
  41. * @param message a message to send
  42. * @throws AmqpException if there is a problem
  43. */
  44. void convertAndSend(Object message) throws AmqpException;
  45. /**
  46. * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.
  47. *
  48. * @param routingKey the routing key
  49. * @param message a message to send
  50. * @throws AmqpException if there is a problem
  51. */
  52. void convertAndSend(String routingKey, Object message) throws AmqpException;
  53. /**
  54. * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange with a specific routing key.
  55. *
  56. * @param exchange the name of the exchange
  57. * @param routingKey the routing key
  58. * @param message a message to send
  59. * @throws AmqpException if there is a problem
  60. */
  61. void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
  62. /**
  63. * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a default routing key.
  64. *
  65. * @param message a message to send
  66. * @param messagePostProcessor a processor to apply to the message before it is sent
  67. * @throws AmqpException if there is a problem
  68. */
  69. void convertAndSend(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
  70. /**
  71. * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key.
  72. *
  73. * @param routingKey the routing key
  74. * @param message a message to send
  75. * @param messagePostProcessor a processor to apply to the message before it is sent
  76. * @throws AmqpException if there is a problem
  77. */
  78. void convertAndSend(String routingKey, Object message, MessagePostProcessor messagePostProcessor)
  79. throws AmqpException;
  80. /**
  81. * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange with a specific routing key.
  82. *
  83. * @param exchange the name of the exchange
  84. * @param routingKey the routing key
  85. * @param message a message to send
  86. * @param messagePostProcessor a processor to apply to the message before it is sent
  87. * @throws AmqpException if there is a problem
  88. */
  89. void convertAndSend(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor)
  90. throws AmqpException;
  91. // receive methods for messages
  92. /**
  93. * Receive a message if there is one from a default queue. Returns immediately, possibly with a null value.
  94. *
  95. * @return a message or null if there is none waiting
  96. * @throws AmqpException if there is a problem
  97. */
  98. Message receive() throws AmqpException;
  99. /**
  100. * Receive a message if there is one from a specific queue. Returns immediately, possibly with a null value.
  101. *
  102. * @param queueName the name of the queue to poll
  103. * @return a message or null if there is none waiting
  104. * @throws AmqpException if there is a problem
  105. */
  106. Message receive(String queueName) throws AmqpException;
  107. // receive methods with conversion
  108. /**
  109. * Receive a message if there is one from a default queue and convert it to a Java object. Returns immediately,
  110. * possibly with a null value.
  111. *
  112. * @return a message or null if there is none waiting
  113. * @throws AmqpException if there is a problem
  114. */
  115. Object receiveAndConvert() throws AmqpException;
  116. /**
  117. * Receive a message if there is one from a specific queue and convert it to a Java object. Returns immediately,
  118. * possibly with a null value.
  119. *
  120. * @param queueName the name of the queue to poll
  121. * @return a message or null if there is none waiting
  122. * @throws AmqpException if there is a problem
  123. */
  124. Object receiveAndConvert(String queueName) throws AmqpException;
  125. // send and receive methods for messages
  126. /**
  127. * Basic RPC pattern. Send a message to a default exchange with a default routing key and attempt to receive a
  128. * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time
  129. * limited by a timeout.
  130. *
  131. * @param message a message to send
  132. * @return the response if there is one
  133. * @throws AmqpException if there is a problem
  134. */
  135. Message sendAndReceive(Message message) throws AmqpException;
  136. /**
  137. * Basic RPC pattern. Send a message to a default exchange with a specific routing key and attempt to receive a
  138. * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time
  139. * limited by a timeout.
  140. *
  141. * @param routingKey the routing key
  142. * @param message a message to send
  143. * @return the response if there is one
  144. * @throws AmqpException if there is a problem
  145. */
  146. Message sendAndReceive(String routingKey, Message message) throws AmqpException;
  147. /**
  148. * Basic RPC pattern. Send a message to a specific exchange with a specific routing key and attempt to receive a
  149. * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time
  150. * limited by a timeout.
  151. *
  152. * @param exchange the name of the exchange
  153. * @param routingKey the routing key
  154. * @param message a message to send
  155. * @return the response if there is one
  156. * @throws AmqpException if there is a problem
  157. */
  158. Message sendAndReceive(String exchange, String routingKey, Message message) throws AmqpException;
  159. // send and receive methods with conversion
  160. /**
  161. * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default
  162. * routing key and attempt to receive a response, converting that to a Java object. Implementations will normally
  163. * set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
  164. *
  165. * @param message a message to send
  166. * @return the response if there is one
  167. * @throws AmqpException if there is a problem
  168. */
  169. Object convertSendAndReceive(Object message) throws AmqpException;
  170. /**
  171. * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a
  172. * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
  173. * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
  174. *
  175. * @param routingKey the routing key
  176. * @param message a message to send
  177. * @return the response if there is one
  178. * @throws AmqpException if there is a problem
  179. */
  180. Object convertSendAndReceive(String routingKey, Object message) throws AmqpException;
  181. /**
  182. * Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a
  183. * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
  184. * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
  185. *
  186. * @param exchange the name of the exchange
  187. * @param routingKey the routing key
  188. * @param message a message to send
  189. * @return the response if there is one
  190. * @throws AmqpException if there is a problem
  191. */
  192. Object convertSendAndReceive(String exchange, String routingKey, Object message) throws AmqpException;
  193. /**
  194. * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default
  195. * routing key and attempt to receive a response, converting that to a Java object. Implementations will normally
  196. * set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
  197. *
  198. * @param message a message to send
  199. * @param messagePostProcessor a processor to apply to the message before it is sent
  200. * @return the response if there is one
  201. * @throws AmqpException if there is a problem
  202. */
  203. Object convertSendAndReceive(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
  204. /**
  205. * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a
  206. * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
  207. * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
  208. *
  209. * @param routingKey the routing key
  210. * @param message a message to send
  211. * @param messagePostProcessor a processor to apply to the message before it is sent
  212. * @return the response if there is one
  213. * @throws AmqpException if there is a problem
  214. */
  215. Object convertSendAndReceive(String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
  216. /**
  217. * Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a
  218. * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
  219. * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
  220. *
  221. * @param exchange the name of the exchange
  222. * @param routingKey the routing key
  223. * @param message a message to send
  224. * @param messagePostProcessor a processor to apply to the message before it is sent
  225. * @return the response if there is one
  226. * @throws AmqpException if there is a problem
  227. */
  228. Object convertSendAndReceive(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;
  229. }

 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的重要类的认识的更多相关文章

  1. RabbitMQ学习之spring配置文件rabbit标签的使用

    下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...

  2. (转)RabbitMQ学习之spring整合发送同步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40890543 以下实现使用Exchange类型为DirectExchange. routingke ...

  3. (转)RabbitMQ学习之spring整合发送异步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...

  4. (转) RabbitMQ学习之spring整合发送异步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...

  5. (转) RabbitMQ学习之spring整合发送同步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...

  6. spring amqp初步了解

    Rabbitmq简介 生产者会把消息发送给RabbitMQ的交换中心(Exchange),Exchange的一侧是生产者,另一侧则是一个或多个队列,由Exchange决定一条消息的生命周期--发送给某 ...

  7. Spring AMQP 源码分析 04 - MessageListener

    ### 准备 ## 目标 了解 Spring AMQP 如何实现异步消息投递(推模式) ## 前置知识 <RabbitMQ入门_05_多线程消费同一队列> ## 相关资源 Quick To ...

  8. RabbitMQ学习笔记(5)----RabbitMQ整合Spring

    在Spring AMQP项目中Spring也提供了对RabbitMQ的支持,这里在之前学习SpringBoot的时候也整合过,但是今天这里使用的Spring的xml配置来整个rabbit. Sprin ...

  9. 消息中间件——RabbitMQ(九)RabbitMQ整合Spring AMQP实战!(全)

    前言 1. AMQP 核心组件 RabbitAdmin SpringAMQP声明 RabbitTemplate SimpleMessageListenerContainer MessageListen ...

  10. 译: 1. RabbitMQ Spring AMQP 之 Hello World

    本文是译文,原文请访问:http://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html RabbitMQ 是一个Brocker (消息队 ...

随机推荐

  1. swift-教你如何实现导航上的UISearchController动画效果。

    这个代码片段是我这周我从网上找了各种资料然后经过自己的修改终于弄好了导航的上下动画效果: step1:==>因为这个搜索要有动画效果,所以这个页面必须要有一个导航控制器: //1.自定义创建导航 ...

  2. HDU-1134 卡特兰数+java大数模板

    题意: 给你一个n,然后1,2,3...2n-1,2n围一圈,让每个数都能用一条线配对并且线与线之间不能交叉,问有几种方法数. 思路: 1 可以和2,4,6...连接.假如   一共有8个数,1和2连 ...

  3. django异常--数据库同步

    在新创建的Django项目中执行makemigrations时,遇到: 而仔细观察,这个报错的app名字是我们之前项目中的app名字,但现在却在我们当前的项目中报错了.究其原因,则是因为之前的项目中的 ...

  4. F - Count the Colors

    F - Count the Colors ZOJ - 1610   思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...

  5. 性能优化——mysql数据库

    一 mysql经常使用命令 1. 打开日志 1) show global variables like "%genera%"; 2)set global general_log=o ...

  6. POJ 1673

    可以证明O是三角形ABC的垂心. 作图辅助线,一个很重要的技巧是延长中线等中线. 可以证明三角形DNA全等于ABC.然后通过角度变换容易证明AQ垂直于BC. #include <iostream ...

  7. 王立平--Http中Get() 与 Post()的差别?

    Http协议是基于TCP协议的,而TCP协议是一种有连接.可靠的传输协议.假设丢失的话,会重传.所以这种话,就 不会有数据的丢失了. 而Http协议有三种方法.Get,Post,Head方法.可是用的 ...

  8. iOS开发-自己定义重用机制给ScrollerView加入子视图

    事实上这个问题我非常早就想过,仅仅是没有通过去写程序实现,昨天有人提起,我就巧了一下 不知道大家打印郭tableview:cellforrow中cell初始的次数,也就是重用池中的cell个数.这个是 ...

  9. SPOJ COWPIC(逆序对变形题)

    SPOJ COWPIC 题目链接 题意:一个序列,相邻能够交换.问最少交换几次使得变成循环的1-n的当中一种 思路:对于原来正常的变换成1-n而言,答案就是逆序对了,而多了这么一个变形,事实上仅仅须要 ...

  10. bootstrap搜索样式

    <div class="container"> <div class="input-group"> <input type=&qu ...