设为首页 收藏本站
查看: 463|回复: 0

[经验分享] RabbitMQ学习之spring-amqp的重要类的认识

[复制链接]

尚未签到

发表于 2017-12-8 22:01:16 | 显示全部楼层 |阅读模式
  对于大多数应用来说都做了与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?

  • 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都包括一个特定的唯一名字的虚拟主机的代理和一些其他属性。



[java] view plain copy
  print?

  • 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判定来肯定消费者所接收的消息。伪代码如下:



[java] view plain copy
  print?

  • 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;例如



[java] view plain copy
  print?

  • 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是用来发送消息的模板类



[java] view plain copy
  print?

  • /**
  • * 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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-422273-1-1.html 上篇帖子: RabbitMQ(四)消息确认(发送确认,接收确认) 下篇帖子: python RabbitMQ广播
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表