欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

 channel接口常用方法

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

方法作用:

聲明一個(gè)隊(duì)列

參數(shù):queue

含義:隊(duì)列名稱

參數(shù):durable

含義:是否持久化,如果設(shè)置為true,服務(wù)器重啟了隊(duì)列仍然存在

參數(shù):exclusive

含義:是否為獨(dú)享隊(duì)列(排他性隊(duì)列),只有自己可見(jiàn)的隊(duì)列,即不允許其它用戶訪問(wèn)

如果exclusive聲明為true,則該隊(duì)列的特點(diǎn)是:

1、只對(duì)首次聲明它的連接(Connection)可見(jiàn)

2、會(huì)在其連接斷開(kāi)的時(shí)候自動(dòng)刪除。

參數(shù):autoDelete

含義:當(dāng)沒(méi)有任何消費(fèi)者使用時(shí),自動(dòng)刪除該隊(duì)列

參數(shù):arguments

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

這里寫圖片描述

void basicQos(int prefetchCount) throws IOException;

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

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

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

解釋: 方法作用: 訂閱消息并消費(fèi)

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

參數(shù):autoAck 含義:是否開(kāi)啟自動(dòng)應(yīng)答,默認(rèn)是開(kāi)啟的,如果需要手動(dòng)應(yīng)答應(yīng)該設(shè)置為false

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

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

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

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

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

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

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

參數(shù):exchange

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

參數(shù):routingKey

含義:發(fā)布到哪個(gè)隊(duì)列

參數(shù):props

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

參數(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;

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

參數(shù):deliveryTag

含義:當(dāng)前消息的類似編號(hào)的號(hào)碼,服務(wù)端為每一個(gè)消息生成的類似編號(hào)的號(hào)碼

參數(shù):multiple

含義:是否把小于當(dāng)前deliveryTag的小于都應(yīng)答了

注意:這個(gè)要在打開(kāi)應(yīng)答機(jī)制后使用,

 boolean ack = false ; //打開(kāi)應(yīng)答機(jī)制  
 channel.basicConsume(QUEUE_NAME, ack, consumer);  

當(dāng)multiple設(shè)置為false時(shí),只會(huì)為deliveryTag所對(duì)應(yīng)的消息進(jìn)行應(yīng)答,服務(wù)端收到應(yīng)答后將該消息刪除 源碼:

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

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

相關(guān)文章

最新評(píng)論