RabbitMQ学习(八)之spring-amqp的重要类的认识

来源:互联网 发布:java按层次遍历二叉树 编辑:程序博客网 时间:2024/06/01 07:42

转载来自http://blog.csdn.net/zhu_tianwei/article/details/40889435

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

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. public class Message {    
  2.     private final MessageProperties messageProperties;    
  3.     
  4.     private final byte[] body;    
  5.   
  6.     public Message(byte[] body, MessageProperties messageProperties) {    
  7.         this.body = body;    
  8.         this.messageProperties = messageProperties;    
  9.     }    
  10.   
  11.     public byte[] getBody() {    
  12.         return this.body;    
  13.     }    
  14.     
  15.     public MessageProperties getMessageProperties() {    
  16.         return this.messageProperties;    
  17.     }    
  18.   
  19.     }    

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

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

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. public interface Exchange {    
  2.     
  3.     String getName();    
  4.     
  5.     String getType();    
  6.     
  7.     boolean isDurable();    
  8.     
  9.     boolean isAutoDelete();    
  10.     
  11.     Map<String, Object> getArguments();    
  12.     
  13. }    
其中 AbstractExchange类实现了Exchange类。而DirectExchange、TopicExchange、FanoutExchang、HeadersExchange四个类继承AbstractExchange。并重写了getType()类。根据各自相对应的Exchange类型。DirectExchange、TopicExchange、FanoutExchang、HeadersExchange分别对应的类型为direct,topic,fanout,headers.

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

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  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;例如

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  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是用来发送消息的模板类 
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  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.   
  13.     // send methods for messages  
  14.   
  15.     /** 
  16.      * Send a message to a default exchange with a default routing key. 
  17.      *  
  18.      * @param message a message to send 
  19.      * @throws AmqpException if there is a problem 
  20.      */  
  21.     void send(Message message) throws AmqpException;  
  22.   
  23.     /** 
  24.      * Send a message to a default exchange with a specific routing key. 
  25.      *  
  26.      * @param routingKey the routing key 
  27.      * @param message a message to send 
  28.      * @throws AmqpException if there is a problem 
  29.      */  
  30.     void send(String routingKey, Message message) throws AmqpException;  
  31.   
  32.     /** 
  33.      * Send a message to a specific exchange with a specific routing key. 
  34.      *  
  35.      * @param exchange the name of the exchange 
  36.      * @param routingKey the routing key 
  37.      * @param message a message to send 
  38.      * @throws AmqpException if there is a problem 
  39.      */  
  40.     void send(String exchange, String routingKey, Message message) throws AmqpException;  
  41.   
  42.     // send methods with conversion  
  43.   
  44.     /** 
  45.      * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a default routing key. 
  46.      *  
  47.      * @param message a message to send 
  48.      * @throws AmqpException if there is a problem 
  49.      */  
  50.     void convertAndSend(Object message) throws AmqpException;  
  51.   
  52.     /** 
  53.      * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key. 
  54.      *  
  55.      * @param routingKey the routing key 
  56.      * @param message a message to send 
  57.      * @throws AmqpException if there is a problem 
  58.      */  
  59.     void convertAndSend(String routingKey, Object message) throws AmqpException;  
  60.   
  61.     /** 
  62.      * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange with a specific routing key. 
  63.      *  
  64.      * @param exchange the name of the exchange 
  65.      * @param routingKey the routing key 
  66.      * @param message a message to send 
  67.      * @throws AmqpException if there is a problem 
  68.      */  
  69.     void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;  
  70.   
  71.     /** 
  72.      * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a default routing key. 
  73.      *  
  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(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;  
  79.   
  80.     /** 
  81.      * Convert a Java object to an Amqp {@link Message} and send it to a default exchange with a specific routing key. 
  82.      *  
  83.      * @param routingKey the routing key 
  84.      * @param message a message to send 
  85.      * @param messagePostProcessor a processor to apply to the message before it is sent 
  86.      * @throws AmqpException if there is a problem 
  87.      */  
  88.     void convertAndSend(String routingKey, Object message, MessagePostProcessor messagePostProcessor)  
  89.             throws AmqpException;  
  90.   
  91.     /** 
  92.      * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange with a specific routing key. 
  93.      *  
  94.      * @param exchange the name of the exchange 
  95.      * @param routingKey the routing key 
  96.      * @param message a message to send 
  97.      * @param messagePostProcessor a processor to apply to the message before it is sent 
  98.      * @throws AmqpException if there is a problem 
  99.      */  
  100.     void convertAndSend(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor)  
  101.             throws AmqpException;  
  102.   
  103.     // receive methods for messages  
  104.   
  105.     /** 
  106.      * Receive a message if there is one from a default queue. Returns immediately, possibly with a null value. 
  107.      *  
  108.      * @return a message or null if there is none waiting 
  109.      * @throws AmqpException if there is a problem 
  110.      */  
  111.     Message receive() throws AmqpException;  
  112.   
  113.     /** 
  114.      * Receive a message if there is one from a specific queue. Returns immediately, possibly with a null value. 
  115.      *  
  116.      * @param queueName the name of the queue to poll 
  117.      * @return a message or null if there is none waiting 
  118.      * @throws AmqpException if there is a problem 
  119.      */  
  120.     Message receive(String queueName) throws AmqpException;  
  121.   
  122.     // receive methods with conversion  
  123.   
  124.     /** 
  125.      * Receive a message if there is one from a default queue and convert it to a Java object. Returns immediately, 
  126.      * possibly with a null value. 
  127.      *  
  128.      * @return a message or null if there is none waiting 
  129.      * @throws AmqpException if there is a problem 
  130.      */  
  131.     Object receiveAndConvert() throws AmqpException;  
  132.   
  133.     /** 
  134.      * Receive a message if there is one from a specific queue and convert it to a Java object. Returns immediately, 
  135.      * possibly with a null value. 
  136.      *  
  137.      * @param queueName the name of the queue to poll 
  138.      * @return a message or null if there is none waiting 
  139.      * @throws AmqpException if there is a problem 
  140.      */  
  141.     Object receiveAndConvert(String queueName) throws AmqpException;  
  142.   
  143.     // send and receive methods for messages  
  144.   
  145.     /** 
  146.      * Basic RPC pattern. Send a message to a default exchange with a default routing key and attempt to receive a 
  147.      * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time 
  148.      * limited by a timeout. 
  149.      *  
  150.      * @param message a message to send 
  151.      * @return the response if there is one 
  152.      * @throws AmqpException if there is a problem 
  153.      */  
  154.     Message sendAndReceive(Message message) throws AmqpException;  
  155.   
  156.     /** 
  157.      * Basic RPC pattern. Send a message to a default exchange with a specific routing key and attempt to receive a 
  158.      * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time 
  159.      * limited by a timeout. 
  160.      *  
  161.      * @param routingKey the routing key 
  162.      * @param message a message to send 
  163.      * @return the response if there is one 
  164.      * @throws AmqpException if there is a problem 
  165.      */  
  166.     Message sendAndReceive(String routingKey, Message message) throws AmqpException;  
  167.   
  168.     /** 
  169.      * Basic RPC pattern. Send a message to a specific exchange with a specific routing key and attempt to receive a 
  170.      * response. Implementations will normally set the reply-to header to an exclusive queue and wait up for some time 
  171.      * limited by a timeout. 
  172.      *  
  173.      * @param exchange the name of the exchange 
  174.      * @param routingKey the routing key 
  175.      * @param message a message to send 
  176.      * @return the response if there is one 
  177.      * @throws AmqpException if there is a problem 
  178.      */  
  179.     Message sendAndReceive(String exchange, String routingKey, Message message) throws AmqpException;  
  180.   
  181.     // send and receive methods with conversion  
  182.   
  183.     /** 
  184.      * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default 
  185.      * routing key and attempt to receive a response, converting that to a Java object. Implementations will normally 
  186.      * set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. 
  187.      *  
  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(Object message) throws AmqpException;  
  193.   
  194.     /** 
  195.      * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a 
  196.      * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will 
  197.      * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. 
  198.      *  
  199.      * @param routingKey the routing key 
  200.      * @param message a message to send 
  201.      * @return the response if there is one 
  202.      * @throws AmqpException if there is a problem 
  203.      */  
  204.     Object convertSendAndReceive(String routingKey, Object message) throws AmqpException;  
  205.   
  206.     /** 
  207.      * Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a 
  208.      * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will 
  209.      * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. 
  210.      *  
  211.      * @param exchange the name of the exchange 
  212.      * @param routingKey the routing key 
  213.      * @param message a message to send 
  214.      * @return the response if there is one 
  215.      * @throws AmqpException if there is a problem 
  216.      */  
  217.     Object convertSendAndReceive(String exchange, String routingKey, Object message) throws AmqpException;  
  218.   
  219.     /** 
  220.      * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a default 
  221.      * routing key and attempt to receive a response, converting that to a Java object. Implementations will normally 
  222.      * set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. 
  223.      *  
  224.      * @param message a message to send 
  225.      * @param messagePostProcessor a processor to apply to the message before it is sent 
  226.      * @return the response if there is one 
  227.      * @throws AmqpException if there is a problem 
  228.      */  
  229.     Object convertSendAndReceive(Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;  
  230.   
  231.     /** 
  232.      * Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a 
  233.      * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will 
  234.      * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. 
  235.      *  
  236.      * @param routingKey the routing key 
  237.      * @param message a message to send 
  238.      * @param messagePostProcessor a processor to apply to the message before it is sent 
  239.      * @return the response if there is one 
  240.      * @throws AmqpException if there is a problem 
  241.      */  
  242.     Object convertSendAndReceive(String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;  
  243.   
  244.     /** 
  245.      * Basic RPC pattern with conversion. Send a Java object converted to a message to a specific exchange with a 
  246.      * specific routing key and attempt to receive a response, converting that to a Java object. Implementations will 
  247.      * normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout. 
  248.      *  
  249.      * @param exchange the name of the exchange 
  250.      * @param routingKey the routing key 
  251.      * @param message a message to send 
  252.      * @param messagePostProcessor a processor to apply to the message before it is sent 
  253.      * @return the response if there is one 
  254.      * @throws AmqpException if there is a problem 
  255.      */  
  256.     Object convertSendAndReceive(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) throws AmqpException;  
  257. }  

 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
0 0