亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Rabbitmq中的channel接口常用方法詳解

 更新時間:2023年09月01日 08:45:14   作者:輕塵×  
這篇文章主要介紹了Rabbitmq中的channel接口常用方法詳解,為了確保消息一定被消費者處理,rabbitMQ提供了消息確認功能,就是在消費者處理完任務之后,就給服務器一個回饋,服務器就會將該消息刪除,需要的朋友可以參考下

 channel接口常用方法

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

方法作用:

聲明一個隊列

參數(shù):queue

含義:隊列名稱

參數(shù):durable

含義:是否持久化,如果設置為true,服務器重啟了隊列仍然存在

參數(shù):exclusive

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

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

1、只對首次聲明它的連接(Connection)可見

2、會在其連接斷開的時候自動刪除。

參數(shù):autoDelete

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

參數(shù):arguments

含義:其他參數(shù) api解釋

這里寫圖片描述

void basicQos(int prefetchCount) throws IOException;

解釋: 方法作用: 一次獲取多少個消息

參數(shù):prefetchCount 含義:會告訴RabbitMQ不要同時給一個消費者推送多于prefetchCount個消息

String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;

解釋: 方法作用: 訂閱消息并消費

參數(shù):queue 含義:所訂閱的隊列

參數(shù):autoAck 含義:是否開啟自動應答,默認是開啟的,如果需要手動應答應該設置為false

注意:為了確保消息一定被消費者處理,rabbitMQ提供了消息確認功能,就是在消費者處理完任務之后,就給服務器一個回饋,服務器就會將該消息刪除,如果消費者超時不回饋,那么服務器將就將該消息重新發(fā)送給其他消費者,當autoAck設置為true時,只要消息被消費者處理,不管成功與否,服務器都會刪除該消息,而當autoAck設置為false時,只有消息被處理,且反饋結(jié)果后才會刪除。

參數(shù):callback 含義:接收到消息之后執(zhí)行的回調(diào)方法 看一下回調(diào)方法源碼:

 void handleDelivery(String consumerTag,
                        Envelope envelope,
                        AMQP.BasicProperties properties,
                        byte[] body)
        throws IOException;
}

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

之前我有一個RPC的例子,可以參考一下那里的這個方法如何實現(xiàn)的 //blog.csdn.net/leisure_life/article/details/78657935

void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;

解釋: 方法作用: 發(fā)布一個消息

參數(shù):exchange

含義:指定轉(zhuǎn)發(fā)器名稱—-ExchangeName,這里用空字符串,就表示消息會交給默認的Exchange

參數(shù):routingKey

含義:發(fā)布到哪個隊列

參數(shù):props

含義:和消息有關的其他配置參數(shù),路由報頭等

參數(shù):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 >resource-driven alarm</a> is in effect.
     *
     * @see com.rabbitmq.client.AMQP.Basic.Publish
     * @see <a >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;
 void basicAck(long deliveryTag, boolean multiple) throws IOException;

解釋: 方法作用: 另外需要在每次處理完成一個消息后,手動向服務端發(fā)送一次應答。

參數(shù):deliveryTag

含義:當前消息的類似編號的號碼,服務端為每一個消息生成的類似編號的號碼

參數(shù):multiple

含義:是否把小于當前deliveryTag的小于都應答了

注意:這個要在打開應答機制后使用,

 boolean ack = false ; //打開應答機制  
 channel.basicConsume(QUEUE_NAME, ack, consumer);  

當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;

到此這篇關于Rabbitmq中的channel接口常用方法詳解的文章就介紹到這了,更多相關channel接口常用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論