rabbitmq channel接口常用方法詳解

轉載自:https://blog.csdn.net/leisure_life/article/details/78663244

Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments) throws IOException;
  • 1
  • 2

解釋:


方法作用:
聲明一個隊列

參數:queue
含義:隊列名稱

參數:durable
含義:是否持久化,如果設置爲true,服務器重啓了隊列仍然存在

參數:exclusive
含義:是否爲獨享隊列(排他性隊列),只有自己可見的隊列,即不允許其它用戶訪問

如果exclusive聲明爲true,則該隊列的特點是:

1、只對首次聲明它的連接(Connection)可見
2、會在其連接斷開的時候自動刪除。

此參數詳情請移步:RabbitMQ:排他性隊列(Exclusive Queue)

參數:autoDelete
含義:當沒有任何消費者使用時,自動刪除該隊列

參數:arguments
含義:其他參數

api解釋

這裏寫圖片描述

void basicQos(int prefetchCount) throws IOException;
  • 1

解釋:


方法作用:

一次獲取多少個消息
參數:prefetchCount
含義:會告訴RabbitMQ不要同時給一個消費者推送多於prefetchCount個消息
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
  • 1

解釋:


方法作用:

訂閱消息並消費
參數:queue
含義:所訂閱的隊列
參數:autoAck
含義:是否開啓自動應答,默認是開啓的,如果需要手動應答應該設置爲false

注意:爲了確保消息一定被消費者處理,rabbitMQ提供了消息確認功能,

就是在消費者處理完任務之後,就給服務器一個回饋,服務器就會將該消息刪除,

如果消費者超時不回饋,那麼服務器將就將該消息重新發送給其他消費者,

當autoAck設置爲true時,只要消息被消費者處理,不管成功與否,服務器都會刪除該消息,

而當autoAck設置爲false時,只有消息被處理,且反饋結果後纔會刪除。
參數:callback
含義:接收到消息之後執行的回調方法
看一下回調方法源碼:
 void handleDelivery(String consumerTag,
                        Envelope envelope,
                        AMQP.BasicProperties properties,
                        byte[] body)
        throws IOException;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

額,看不太懂,反正就是接到消息之後你對消息的處理都要寫在這裏!

之前我有一個RPC的例子,可以參考一下那裏的這個方法如何實現的
http://blog.csdn.net/leisure_life/article/details/78657935
void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
  • 1

解釋:


方法作用:

發佈一個消息
參數:exchange
含義:指定轉發器名稱—-ExchangeName,這裏用空字符串,就表示消息會交給默認的Exchange
參數:routingKey
含義:發佈到哪個隊列
參數:props
含義:和消息有關的其他配置參數,路由報頭等
參數:body
含義:消息體

源碼:
/**
     * 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 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, BasicProperties props, byte[] body) throws IOException;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
 void basicAck(long deliveryTag, boolean multiple) throws IOException;
  • 1

解釋:


方法作用:

另外需要在每次處理完成一個消息後,手動向服務端發送一次應答。
參數:deliveryTag
含義:當前消息的類似編號的號碼,服務端爲每一個消息生成的類似編號的號碼
參數:multiple
含義:是否把小於當前deliveryTag的小於都應答了
注意:這個要在打開應答機制後使用,
 boolean ack = false ; //打開應答機制  
 channel.basicConsume(QUEUE_NAME, ack, consumer);  
  • 1
  • 2

當multiple設置爲false時,只會爲deliveryTag所對應的消息進行應答,服務端收到應答後將該消息刪除

源碼:

 /**
     * Acknowledge one or several received
     * messages. 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 acknowledged.
     * @see com.rabbitmq.client.AMQP.Basic.Ack
     * @param deliveryTag the tag from the received {@link com.rabbitmq.client.AMQP.Basic.GetOk} or {@link com.rabbitmq.client.AMQP.Basic.Deliver}
     * @param multiple true to acknowledge all messages up to and
     * including the supplied delivery tag; false to acknowledge just
     * the supplied delivery tag.
     * @throws java.io.IOException if an error is encountered
     */
    void basicAck(long deliveryTag, boolean multiple) throws IOException;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章