Rabbitmq中的channel接口常用方法詳解
channel接口常用方法
Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
Map<String, Object> arguments) throws IOException;方法作用:
聲明一個隊列
參數(shù):queue
含義:隊列名稱
參數(shù):durable
含義:是否持久化,如果設(shè)置為true,服務(wù)器重啟了隊列仍然存在
參數(shù):exclusive
含義:是否為獨享隊列(排他性隊列),只有自己可見的隊列,即不允許其它用戶訪問
如果exclusive聲明為true,則該隊列的特點是:
1、只對首次聲明它的連接(Connection)可見
2、會在其連接斷開的時候自動刪除。
參數(shù):autoDelete
含義:當(dāng)沒有任何消費者使用時,自動刪除該隊列
參數(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 含義:是否開啟自動應(yīng)答,默認(rèn)是開啟的,如果需要手動應(yīng)答應(yīng)該設(shè)置為false
注意:為了確保消息一定被消費者處理,rabbitMQ提供了消息確認(rèn)功能,就是在消費者處理完任務(wù)之后,就給服務(wù)器一個回饋,服務(wù)器就會將該消息刪除,如果消費者超時不回饋,那么服務(wù)器將就將該消息重新發(fā)送給其他消費者,當(dāng)autoAck設(shè)置為true時,只要消息被消費者處理,不管成功與否,服務(wù)器都會刪除該消息,而當(dāng)autoAck設(shè)置為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,這里用空字符串,就表示消息會交給默認(rèn)的Exchange
參數(shù):routingKey
含義:發(fā)布到哪個隊列
參數(shù):props
含義:和消息有關(guān)的其他配置參數(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;
解釋: 方法作用: 另外需要在每次處理完成一個消息后,手動向服務(wù)端發(fā)送一次應(yīng)答。
參數(shù):deliveryTag
含義:當(dāng)前消息的類似編號的號碼,服務(wù)端為每一個消息生成的類似編號的號碼
參數(shù):multiple
含義:是否把小于當(dāng)前deliveryTag的小于都應(yīng)答了
注意:這個要在打開應(yīng)答機(jī)制后使用,
boolean ack = false ; //打開應(yīng)答機(jī)制 channel.basicConsume(QUEUE_NAME, ack, consumer);
當(dāng)multiple設(shè)置為false時,只會為deliveryTag所對應(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決新版idea新建文件沒有XML和Resource Bundle文件問題
這篇文章主要介紹了解決新版idea新建文件沒有XML和Resource Bundle文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring MVC參數(shù)校驗詳解(關(guān)于`@RequestBody`返回`400`)
這篇文章主要介紹了Spring MVC參數(shù)校驗的相關(guān)資料,主要是針對`@RequestBody`返回`400`的問題,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
詳解Spring FactoryBean靈活創(chuàng)建復(fù)雜對象的秘密武器
FactoryBean是Spring框架中用于創(chuàng)建復(fù)雜Bean的接口,通過編程方式控制Bean的創(chuàng)建過程,它允許開發(fā)者自定義Bean的創(chuàng)建邏輯,適用于集成第三方庫、延遲初始化、動態(tài)代理和統(tǒng)一管理資源等場景,本文介紹Spring FactoryBean創(chuàng)建復(fù)雜對象的相關(guān)操作,感興趣的朋友一起看看吧2025-02-02

