Rabbitmq訊息手動確認-防止丟失訊息
1、Channel 1.1 channel.exchangeDeclare():
type:有direct、fanout、topic三種 durable:true、false true:伺服器重啟會保留下來Exchange。警告:僅設定此選項,不代表訊息持久化。即不保證重啟後訊息還在。原文:true if we are declaring a durable exchange (the exchange will survive a server restart) autoDelete:true、false.true:當已經沒有消費者時,伺服器是否可以刪除該Exchange。原文1:true if the server should delete the exchange when it is no longer in use。
/** * Declare an exchange. * @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 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, Map<String, Object> arguments) throws IOException;
1.2 chanel.basicQos()
prefetchSize:0
prefetchCount:會告訴RabbitMQ不要同時給一個消費者推送多於N個訊息,即一旦有N個訊息還沒有ack,則該consumer將block掉,直到有訊息ack
global:true\false 是否將上面設定應用於channel,簡單點說,就是上面限制是channel級別的還是consumer級別
備註:據說prefetchSize 和global這兩項,rabbitmq沒有實現,暫且不研究
/** * Request specific "quality of service" settings. * * These settings impose limits on the amount of data the server * will deliver to consumers before requiring acknowledgements. * Thus they provide a means of consumer-initiated flow control. * @see com.rabbitmq.client.AMQP.Basic.Qos * @param prefetchSize maximum amount of content (measured in * octets) that the server will deliver, 0 if unlimited * @param prefetchCount maximum number of messages that the server * will deliver, 0 if unlimited * @param global true if the settings should be applied to the * entire channel rather than each consumer * @throws java.io.IOException if an error is encountered */ void basicQos(int prefetchSize, int prefetchCount, boolean global) throws IOException;
1.3 channel.basicPublish()
routingKey:路由鍵,#匹配0個或多個單詞,*匹配一個單詞,在topic exchange做訊息轉發用 mandatory:true:如果exchange根據自身型別和訊息routeKey無法找到一個符合條件的queue,那麼會呼叫basic.return方法將訊息返還給生產者。false:出現上述情形broker會直接將訊息扔掉
immediate:true:如果exchange在將訊息route到queue(s)時發現對應的queue上沒有消費者,那麼這條訊息不會放入佇列中。當與訊息routeKey關聯的所有queue(一個或多個)都沒有消費者時,該訊息會通過basic.return方法返還給生產者。
BasicProperties :需要注意的是BasicProperties.deliveryMode,0:不持久化 1:持久化 這裡指的是訊息的持久化,配合channel(durable=true),queue(durable)可以實現,即使伺服器宕機,訊息仍然保留
簡單來說:mandatory標誌告訴伺服器至少將該訊息route到一個佇列中,否則將訊息返還給生產者;immediate標誌告訴伺服器如果該訊息關聯的queue上有消費者,則馬上將訊息投遞給它,如果所有queue都沒有消費者,直接把訊息返還給生產者,不用將訊息入佇列等待消費者了。
/** * 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;
1.4 channel.basicAck();
deliveryTag:該訊息的index multiple:是否批量.true:將一次性ack所有小於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;
1.5 channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
deliveryTag:該訊息的index
multiple:是否批量.true:將一次性拒絕所有小於deliveryTag的訊息。
requeue:被拒絕的是否重新入佇列
/** * Reject one or several received messages. * * Supply the <code>deliveryTag</code> from the {@link com.rabbitmq.client.AMQP.Basic.GetOk} * or {@link com.rabbitmq.client.AMQP.Basic.GetOk} method containing the message to be rejected. * @see com.rabbitmq.client.AMQP.Basic.Nack * @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 reject all messages up to and including * the supplied delivery tag; false to reject just the supplied * delivery tag. * @param requeue true if the rejected message(s) should be requeued rather * than discarded/dead-lettered * @throws java.io.IOException if an error is encountered */ void basicNack(long deliveryTag, boolean multiple, boolean requeue) throws IOException;
1.5 channel.basicReject(delivery.getEnvelope().getDeliveryTag(), false);
deliveryTag:該訊息的index
requeue:被拒絕的是否重新入佇列
channel.basicNack 與 channel.basicReject 的區別在於basicNack可以拒絕多條訊息,而basicReject一次只能拒絕一條訊息
/** * 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;
1.6 channel.basicConsume(QUEUE_NAME, true, consumer);
autoAck:是否自動ack,如果不自動ack,需要使用channel.ack、channel.nack、channel.basicReject 進行訊息應答
/** * Start a non-nolocal, non-exclusive consumer, with * a server-generated consumerTag. * @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 callback an interface to the consumer object * @return the consumerTag generated by the server * @throws java.io.IOException if an error is encountered * @see com.rabbitmq.client.AMQP.Basic.Consume * @see com.rabbitmq.client.AMQP.Basic.ConsumeOk * @see #basicConsume(String, boolean, String, boolean, boolean, Map, Consumer) */ String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
1.7 chanel.exchangeBind()
channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); 用於通過繫結bindingKey將queue到Exchange,之後便可以進行訊息接收
/** * Bind an exchange to an exchange, with no extra arguments. * @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 * @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) throws IOException;
1.8 channel.queueDeclare(QUEUE_NAME, false, false, false, null);
durable:true、false true:在伺服器重啟時,能夠存活
exclusive :是否為當前連線的專用佇列,在連線斷開後,會自動刪除該佇列,生產環境中應該很少用到吧。 autodelete:當沒有任何消費者使用時,自動刪除該佇列。this means that the queue will be deleted when there are no more processes consuming messages from it.
/** * 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;
相關推薦
Rabbitmq訊息手動確認-防止丟失訊息
1、Channel 1.1 channel.exchangeDeclare():type:有direct、fanout、topic三種 durable:true、false true:伺服器重啟會保留下來Exchange。警告:僅設定此選項,不代表訊息持久化。即不保證重啟後訊息還在。原文:true if
rabbitmq訊息傳送確認和消費訊息手動刪除訊息
0.application.properties新增如下配置 # 訊息傳送至exchange callback spring.rabbitmq.publisher-confirms=true # 訊息傳送至queue 失敗才callback spring.rabbitmq.publi
使用rabbitmq手動確認訊息的,定時獲取佇列訊息實現
描述問題 最近專案中因為有些資料,需要推送到第三方系統中,因為資料會一直增加,並且需要與第三方系統做相關互動。 相關業務 本著不影響線上執行效率的思想,我們將增加的訊息放入rabbitmq,使用另一個應用獲取消費,因為資料只是推送,並且業務的資料有15分鐘左右的更新策略,對實時性不是很高所以我們需要一
基於springboot工程淺談整合rabbitmq怎麼樣防止訊息傳送mq不丟失和消費mq的訊息防止丟失
本文只針對springboot整合rabbitmq的訊息防丟失,話不多說,上乾貨.... 設定傳送mq訊息不丟失實現思路 執行的方案: 第一步,要對佇列,訊息以及交換機進行持久化操作(儲存到物理磁碟中) 因為mq訊息預設是儲存在記憶體中 交換機我們在宣告的時候可以進行持久化
RabbitMQ系列(四)RabbitMQ事務和Confirm傳送方訊息確認——深入解讀(轉載)
原文地址:https://yq.aliyun.com/articles/629858 RabbitMQ事務和Confirm傳送方訊息確認——深入解讀 RabbitMQ系列文章 RabbitMQ在Ubuntu上的環境搭建 深入瞭解RabbitMQ工作原理及簡單使用 Rabbi
(六)RabbitMQ訊息佇列-訊息任務分發與訊息ACK確認機制(PHP版)
在前面一章介紹了在PHP中如何使用RabbitMQ,至此入門的的部分就完成了,我們內心中一定還有很多疑問:如果多個消費者消費同一個佇列怎麼辦?如果這幾個消費者分任務的權重不同怎麼辦?怎麼把同一個佇列不同級別的任務分發給不同的消費者?如果消費者異常離線怎麼辦?不要著急,後面將慢慢解開面紗。我們
RabbitMQ事務和Confirm傳送方訊息確認
RabbitMQ事務和Confirm傳送方訊息確認——深入解讀 RabbitMQ系列文章 RabbitMQ在Ubuntu上的環境搭建 深入瞭解RabbitMQ工作原理及簡單使用 RabbitMQ交換器Exchange介紹與實踐 RabbitMQ事務和Confirm傳送方
RabbitMQ ——與SpringBoot整合並實現訊息確認機制
不囉嗦直接上程式碼 目錄結構如下: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc
RabbitMQ事務和Confirm傳送方訊息確認——深入解讀
引言 根據前面的知識(深入瞭解RabbitMQ工作原理及簡單使用、Rabbit的幾種工作模式介紹與實踐)我們知道,如果要保證訊息的可靠性,需要對訊息進行持久化處理,然而訊息持久化除了需要程式碼的設定之外,還有一個重要步驟是至關重要的,那就是保證你的訊息順利進入
RabbitMQ實戰-訊息確認機制之訊息的正確消費
上節中我們講了如何確保訊息的準確釋出,今天我們來看看如何確保訊息的正確消費。 在之前的基礎上我們對消費者(倉庫服務)進行完善。 修改配置檔案application.yml 消費者的ack方式預設是自動的,也就是說訊息一旦被消費(無論是否處理成功),訊息都會被確認,然後會從
RabbitMQ使用教程(四)如何通過持久化保證訊息99.99%不丟失?
1. 前情回顧 RabbitMQ使用教程(一)RabbitMQ環境安裝配置及Hello World示例 RabbitMQ使用教程(二)RabbitMQ使用者管理,角色管理及許可權設定 RabbitMQ使用教程(三)如何保證訊息99.99%被髮送成功? 在上一篇部落格中,我們講解了如何通過RabbitMQ的
python採用pika庫使用rabbitmq(七)Publish\Subscribe(訊息釋出\訂閱)
之前的例子都基本都是1對1的訊息傳送和接收,即訊息只能傳送到指定的queue裡,但有些時候你想讓你的訊息被所有的Queue收到,類似廣播的效果,這時候就要用到exchange了, Exchange在定義的時候是有型別的,以決定到底是哪些Queue符合條件,可以接收訊息 fanout: 所有bin
RabbitMQ 訊息佇列 - topic 模式分發訊息
推薦閱讀 https://blog.csdn.net/column/details/15500.html topic 模式 根據 Binding 指定的 RoutingKey, Exchange 對 key 進行模式匹配後投遞到相應的 Queue, 模式匹配時符號
RabbitMQ 訊息佇列 - fanout 模式分發訊息
推薦閱讀 https://blog.csdn.net/column/details/15500.html fanout 模式 將同一個 message 傳送到所有同該 Exchange 繫結的 queue, 只要 RoutingKey 是一樣, 這條訊息都會被投遞
RabbitMQ 訊息佇列 - direct 模式分發訊息
推薦閱讀 https://blog.csdn.net/column/details/15500.html direct 模式 根據 Binding 指定的 Routing Key, 將符合Key的訊息傳送到 Binding 的 Queue p_direc
rabbitmq實現向各服務廣播訊息
廣播fanout 主要是將一個訊息,分發到綁定了它的佇列上,而這些佇列如消費者自己去建立和繫結! 對生產者是解耦的 生產者不需要關心消費者有多少,消費者如果需要這種訊息,只需要把佇列繫結到exchange上即可 流程 開啟rabbitmq的ui 建立兩個佇列fanout1,fanout2
Kafka、RabbitMQ、RocketMQ等訊息中介軟體的對比 —— 訊息傳送效能和區別
分散式系統中,我們廣泛運用訊息中介軟體進行系統間的資料交換,便於非同步解耦。現在開源的訊息中介軟體有很多,前段時間我們自家的產品 RocketMQ (MetaQ的核心) 也順利開源,得到大家的關注。 那麼,訊息中介軟體效能究竟哪家強? 帶著這個疑問,我們中介軟體測
訊息中介軟體系列五、rabbit訊息的確認機制
一、訊息的確認機制 1、消費者收到的每一條訊息都必須進行確認。(分為自動確認和消費者自行確認) 消費者在宣告佇列時,指定autoAck引數,true自動確認,false時rabbitmq會等到消費者顯示的發回一個ack訊號才會刪除訊息。autoAck=fals
易學筆記-RabbitMQ教程4:選擇性接收訊息(路由器型別為:direct)
易學筆記 十年IT經驗個人學習筆記分享: 開發語言:C/C++/JAVA/PYTHON/GO/JSP WEB架構:Servlets/springMVC/springBoot/springClound 容器架構:Docker容器/Docker叢集/Docker與微服務整合/
Kafka、RabbitMQ、RocketMQ訊息中介軟體的對比 —— 訊息傳送效能(轉自阿里中介軟體)
引言分散式系統中,我們廣泛運用訊息中介軟體進行系統間的資料交換,便於非同步解耦。現在開源的訊息中介軟體有很多,前段時間我們自家的產品 RocketMQ (MetaQ的核心) 也順利開源,得到大家的關注。那麼,訊息中介軟體效能究竟哪家強?帶著這個疑問,我們中介軟體測試組對常見的三類訊息產品(Kafka、Rabb