zoukankan      html  css  js  c++  java
  • (三)RabbitMQ:客户端开发向导

    1.使用交换器和队列

    1.1exchangeDeclare方法详解

       /**
         * Declare an exchange, via an interface that allows the complete set of
         * arguments.
         * @see com.rabbitmq.client.AMQP.Exchange.Declare
         * @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
         * @param exchange the name of the exchange
         * @param type the exchange type
         * @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
         * @param autoDelete true if the server should delete the exchange when it is no longer in use
         * @param internal true if the exchange is internal, i.e. can't be directly
         * published to by a client.
         * @param arguments other properties (construction arguments) for the exchange
         * @return a declaration-confirm method to indicate the exchange was successfully declared
         * @throws java.io.IOException if an error is encountered
         */
        Exchange.DeclareOk exchangeDeclare(String exchange,
                                                  String type,
                                                  boolean durable,
                                                  boolean autoDelete,
                                                  boolean internal,
                                                  Map<String, Object> arguments) throws IOException;
    

    这个方法的返回值是Exchange.DeclareOK,用来标识成功声明了一个交换器。各个参数详细说明如下:

    • exchange:交换器的名称。
    • type:交换器的类型,常见的如fanout,direct,topic等。
    • durable:设置是否持久化。durable设置true表示持久化,反之是非持久化。持久化可以将交换机存盘,在服务器重启的时候不会丢失相关信息。
    • autoDelete:设置是否自动删除。autoDelete设置true则表示自动删除。自动删除的前提是至少有一个队列或者交换器与这个交换器绑定,之后所有与这个交换器绑定的队列或者交换器都与此解绑。注意不能错误地把这个参数 理解为:“当与此交换器连接的客户端都断开时,RabbitMQ会自动删除本交换器”。
    • internal:设置是否时内置的。如果设置为true,则表示是内置的交换器,客户端程序无法直接发送消息到这和交换器中,只能通过交换器路由到交换器这种方式。
    • arguments:其他一些结构化参数,比如alternate-exchange等。

    1.2queueDeclare方法详解

    不带任何参数的queueDeclare方法默认创建一个由RabbitMQ命名的(类似amq.gen-LhQz1gv3Uhusdfhs名称,这种队列也称为匿名队列),排他的,自动删除的,非持久化的队列。方法参数详细说明如下:

       /**
         * Declare a queue
         * @see com.rabbitmq.client.AMQP.Queue.Declare
         * @see com.rabbitmq.client.AMQP.Queue.DeclareOk
         * @param queue the name of the queue
         * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
         * @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
         * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
         * @param arguments other properties (construction arguments) for the queue
         * @return a declaration-confirm method to indicate the queue was successfully declared
         * @throws java.io.IOException if an error is encountered
         */
        Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                     Map<String, Object> arguments) throws IOException;
    
    • queue:队列的名称。
    • durable:设置是否持久化。为true则设置队列为持久化。持久化的队列会存盘,在服务器重启的时候可以保证不丢失相关信息。
    • exclusive:设置是否排他。为true则设置队列为排他的,如果一个队列被声明为排他队列,该队列仅对首次声明它的连接可见,并在连接断开时自动删除。

    这里需要注意三点:

    1. 排他队列是基于连接(Connection)可见的,同一个连接的不同信道(Channel)是可以同时访问同一连接创建的排他队列;
    2. “首次”是指如果一个连接已经声明了一个排他队列,其他连接是不允许建立同名的排他队列,这个与普通队列不同;
    3. 即使该队列是持久化的,一旦连接关闭或者客户端退出,该排他队列都会被自动删除,这种队列适用于一个客户端同时发送和读取消息的应用场景。
    • autoDelete:设置是否自动删除。为true则设置队列为自动删除。自动删除的前提是:至少有一个消费者连接到这个队列,之后所有与这个队列连接的消费者都断开时,才会自动删除。不能把这个参数错误地理解为:“当连接到此队列的所有客户端断开时,这个队列自动删除”,因为生产者客户端创建这个队列,或者没有消费者客户端与这个队列连接时,都不会自动删除这个队列。
    • arguments:设置队列的其他一些参数,如x-message-ttl,x-expires,x-max-leanth,x-max-length-bytes,x-dead-letter-ecchange,x-dead-letter-routing-key,x-max-priority等。

    注意要点:生产者和消费者都能够使用queueDeclare来声明一个队列,但是如果消费者在同一个信道上订阅了另一个队列,就无法再声明队列了,必须先取消订阅,然后将信道设置为“传输”模式,之后才能声明队列。

    1.3queueBind方法详解

    将队列和交换器绑定的方法如下:

        /**
         * Bind a queue to an exchange.
         * @see com.rabbitmq.client.AMQP.Queue.Bind
         * @see com.rabbitmq.client.AMQP.Queue.BindOk
         * @param queue the name of the queue
         * @param exchange the name of the exchange
         * @param routingKey the routine key to use for the binding
         * @param arguments other properties (binding parameters)
         * @return a binding-confirm method if the binding was successfully created
         * @throws java.io.IOException if an error is encountered
         */
        Queue.BindOk queueBind(String queue, String exchange, String routingKey, Map<String, Object> arguments) throws IOException;
    
    • queue:队列名称。
    • exchange:交换器的名称。
    • routingKey:用来绑定队列和交换器的路由键。
    • arguments:定义绑定的一些参数。

    不仅可以将队列和交换器绑定起来,也可以将已经被绑定的队列和交换器进行解绑。具体方法如下:

        /**
         * Unbind a queue from an exchange.
         * @see com.rabbitmq.client.AMQP.Queue.Unbind
         * @see com.rabbitmq.client.AMQP.Queue.UnbindOk
         * @param queue the name of the queue
         * @param exchange the name of the exchange
         * @param routingKey the routine key to use for the binding
         * @param arguments other properties (binding parameters)
         * @return an unbinding-confirm method if the binding was successfully deleted
         * @throws java.io.IOException if an error is encountered
         */
        Queue.UnbindOk queueUnbind(String queue, String exchange, String routingKey, Map<String, Object> arguments) throws IOException;
    

    1.4exchangeBind方法详解

    不仅可以将交换器与队列绑定,也可以将交换器与交换器绑定:

        /**
         * Bind an exchange to an exchange.
         * @see com.rabbitmq.client.AMQP.Exchange.Bind
         * @see com.rabbitmq.client.AMQP.Exchange.BindOk
         * @param destination the name of the exchange to which messages flow across the binding
         * @param source the name of the exchange from which messages flow across the binding
         * @param routingKey the routine key to use for the binding
         * @param arguments other properties (binding parameters)
         * @return a binding-confirm method if the binding was successfully created
         * @throws java.io.IOException if an error is encountered
         */
        Exchange.BindOk exchangeBind(String destination, String source, String routingKey, Map<String, Object> arguments) throws IOException;
    

    绑定之后,消息从source交换器转发到destination交换器,某种程度上来说destination交换器可以看作一个队列。

    2.发送消息

        /**
         * Publish a message.
         *
         * Publishing to a non-existent exchange will result in a channel-level
         * protocol exception, which closes the channel.
         *
         * Invocations of <code>Channel#basicPublish</code> will eventually block if a
         * <a href="http://www.rabbitmq.com/alarms.html">resource-driven alarm</a> is in effect.
         *
         * @see com.rabbitmq.client.AMQP.Basic.Publish
         * @see <a href="http://www.rabbitmq.com/alarms.html">Resource-driven alarms</a>
         * @param exchange the exchange to publish the message to
         * @param routingKey the routing key
         * @param mandatory true if the 'mandatory' flag is to be set
         * @param immediate true if the 'immediate' flag is to be
         * set. Note that the RabbitMQ server does not support this flag.
         * @param props other properties for the message - routing headers etc
         * @param body the message body
         * @throws java.io.IOException if an error is encountered
         */
        void basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body)
                throws IOException;
    
    • exchange:交换器的名称,指明消息需要发送到哪个交换器中。如果设置为空字符串,则消息会被发送到RabbitMQ默认的交换器中。
    • routingKey:路由键,交换器根据路由键将消息存储到相应的队列之中。
    • body:消息体(payload),真正需要发送的消息。
    • mandatory:当mandatory参数设置为true时,交换器无法根据自身的类型和路由键找到一个符合条件的队列,那么RabbitMQ会调用Basic.Return命令将消息返回给生产者。当mandatory参数设置为false时,出现上述情形,则消息直接被丢弃。(mandatory参数告诉服务器至少将消息路由到一个队列中,否则将消息返回给生产者。)
    • immediate:immediate参数设置为true时,如果交换器在将消息路由到队列时发现队列上并不存在任何消费者,那么这条消息将不会存入队列中。当与路由键匹配的所有队列都没有消费者时,该消息会通过Basic.Return返回至生产者。(immediate参数告诉服务器,如果该消息关联的队列上有消费者,则立刻投递;如果所有匹配的队列上都没有消费者,则直接将消息返还给生产者,不用将消息存入队列而等待消费者了。)
    • props:消息的基本属性集
            private String contentType;
            private String contentEncoding;
            private Map<String,Object> headers;
            private Integer deliveryMode;
            private Integer priority;
            private String correlationId;
            private String replyTo;
            private String expiration;
            private String messageId;
            private Date timestamp;
            private String type;
            private String userId;
            private String appId;
            private String clusterId;
    

    3.消费消息

    RabbitMQ的消费模式分为两种:推(Push)模式和拉(Pull)模式。推模式采用Basic.Consume进行消费,而拉模式则是调用Basic.Get进行消费。

    3.1推模式

    接受消息一般通过实现Consumer接口或者继承DefaultConsumer类来实现。当调用与Consumer相关的API方法时,不同的订阅采用不同的消费者标签(consumerTag)来区分彼此,在同一个Channel中的消费者也需要通过唯一的消费者标签以作区分:

            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
    
                    System.out.println("recv message:" + new String(body));
    
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            };
            boolean autoAck = false;
            channel.basicConsume(QUEUE_NAME, autoAck, "myConsumer", consumer);
    

    channel.basicConsume方法详解:

        /**
         * Start a consumer. Calls the consumer's {@link Consumer#handleConsumeOk}
         * method.
         * @param queue the name of the queue
         * @param autoAck true if the server should consider messages
         * acknowledged once delivered; false if the server should expect
         * explicit acknowledgements
         * @param consumerTag a client-generated consumer tag to establish context
         * @param noLocal true if the server should not deliver to this consumer
         * messages published on this channel's connection
         * @param exclusive true if this is an exclusive consumer
         * @param callback an interface to the consumer object
         * @param arguments a set of arguments for the consume
         * @return the consumerTag associated with the new consumer
         * @throws java.io.IOException if an error is encountered
         * @see com.rabbitmq.client.AMQP.Basic.Consume
         * @see com.rabbitmq.client.AMQP.Basic.ConsumeOk
         */
        String basicConsume(String queue, boolean autoAck, String consumerTag, boolean noLocal, boolean exclusive, Map<String, Object> arguments, Consumer callback) throws IOException;
    
    
    • queue:队列名称;
    • autoAck:设置是否自动确认。建议设置成false,即不自动确认。
    • consumerTag:消费者标签,用来区分多个消费者。
    • noLocal:设置为true则表示不能将同一个Connection中生产者发送的消息传送给这个Connection中的消费者。
    • exclusive:设置是否排他。
    • arguments:设置消费者的其他参数。
    • callback:设置消费者的回调函数。用来处理RabbitMQ推送过来的消息,比如DefaultConsumer,使用时需要客户端重写(override)其中的方法。
      和生产者一样,消费者客户端同样需要考虑线程安全问题。消费者客户端的这些callback会被分配到与Channel不同的线程池上,这意味着消费者客户端可以安全地调用这些阻塞方法,比如channel.queueDeclare,channel.basicCancel等。
      每个channel都拥有自己独立的线程。最常用的做法是一个Channel对应一个消费者,也就是意味着消费者都彼此之间没有任何关联。当然也可以在一个Channel中维持多个消费者,但是需要注意一个问题,如果Chanel中的一个消费者一直在运行,那么其他消费者的callback会被”耽搁“。

    3.2拉模式

    通过channel.basicGet方法可以单条地获取消息,其返回值是GetRespone。

        /**
         * Retrieve a message from a queue using {@link com.rabbitmq.client.AMQP.Basic.Get}
         * @see com.rabbitmq.client.AMQP.Basic.Get
         * @see com.rabbitmq.client.AMQP.Basic.GetOk
         * @see com.rabbitmq.client.AMQP.Basic.GetEmpty
         * @param queue the name of the queue
         * @param autoAck true if the server should consider messages
         * acknowledged once delivered; false if the server should expect
         * explicit acknowledgements
         * @return a {@link GetResponse} containing the retrieved message data
         * @throws java.io.IOException if an error is encountered
         */
        GetResponse basicGet(String queue, boolean autoAck) throws IOException;
    

    4.消费端的确认与拒绝

    为了保证消息从队列可靠地达到消费者,RabbitMQ提供了消息确认机制(message acknowledgment)。消费者在订阅队列时,可以指定autoAck参数,当autoAck等于false时,RabbitMQ会等待消费者显示地回复确认信号后才从内存(或者磁盘)中移去消息(实质上是先打上删除标记,之后再删除)。当autoAck等于true时,RabbitMQ会自动把发送出去的消息设置为确认,然后从内存(或者磁盘)中删除,而不管消费者是否真正地消费到了这些消息。

    采用消息确认机制后,只有设置autoAck参数为false,消费者就有足够的时间处理消息(任务),不用担心处理消息过程中消费者进程挂掉后消息丢失的问题,因为RabbitMQ会一直等待持有消息直到消费者显示调用Basic.Ack命令为止。

    当autoAck参数置为false,对于RabbitMQ服务器而言,队列中的消息分为两个部分:一部分时等待投递给消费者的消息;一部分时已经投递给消费者,但是还没有收到消费者确认信号的消息。如果RabbitMQ一直没有收到消费者的确认信号,并且消费此消息的消费者已经断开连接,则RabbitMQ 会安排该消息重新进入队列,等待投递给下一个消费者,当然也有可能还是原来的那个消费者。

    RabbitMQ不会为未确认的消息设置过期时间,它判断此消息是否需要重新投递给消费者的唯一依据是消费该消息的消费者连接是否已经断开,这么设计的原因是RabbitMQ允许消费者消费一条消息的时间可以很久很久。

    在消费者接收到消息后,如果想明确拒绝当前的消息而不是确认,那么应该怎么做呢?
    RabbitMQ在2.0.0版本开始引入了Basic.Reject这个命令,消费者客户端可以调用与其对应的channel.basicReject方法来告诉RabbitMQ拒绝这个消息。

        /**
         * Reject a message. Supply the deliveryTag from the {@link com.rabbitmq.client.AMQP.Basic.GetOk}
         * or {@link com.rabbitmq.client.AMQP.Basic.Deliver} method
         * containing the received message being rejected.
         * @see com.rabbitmq.client.AMQP.Basic.Reject
         * @param deliveryTag the tag from the received {@link com.rabbitmq.client.AMQP.Basic.GetOk} or {@link com.rabbitmq.client.AMQP.Basic.Deliver}
         * @param requeue true if the rejected message should be requeued rather than discarded/dead-lettered
         * @throws java.io.IOException if an error is encountered
         */
        void basicReject(long deliveryTag, boolean requeue) throws IOException;
    
    • deliveryTag:可以看作消息的编号,它是一个64位的长整型值。
    • requeue:设置为true时,RabbitMQ会重新将这条消息存入队列,以便可以发送给下一个订阅的消费者;如果设置为false,则RabbitMQ会立即把消息从队列中移除,而不会把它发送给新的消费者。

    Basic.Reject命令一次只能拒绝一条消息,如果想要批量拒绝消息,则可以使用Basic.Nack这个命令。

  • 相关阅读:
    java去掉List中的重复值代码
    jquery 请求jsp传递json数据的方法
    jsp自定义标签分析
    jquery mouseout事件冒泡解决方法
    java split函数 对空的处理
    log4j使用感受
    mysql数据库主外键级联删除脚本RESTRICT --> CASCADE
    jquery 实现层级下拉框联动效果 代码
    JSP图片上传 公共工具类
    Apache和Nginx的对比
  • 原文地址:https://www.cnblogs.com/everyingo/p/12902304.html
Copyright © 2011-2022 走看看