ActiveMQ中Session以及消息ACKNOWLEDGE不一樣方式的說明

參考博文:http://www.cnblogs.com/SzeCheng/p/4792084.htmlhtml

參考博文:http://activemq.apache.org/producer-flow-control.htmlapache

名詞解釋:api

P:生產者session

C:消費者app

服務端:P 或者 ActiveMQ服務maven

客戶端:ActiveMQ服務 或者 Cthis

客戶端成功接收一條消息的標誌是這條消息被簽收。成功接收一條消息通常包括以下三個階段: google

1.客戶端接收消息; spa

2.客戶端處理消息; code

3.消息被簽收。

session = connection.createSession(Boolean.false, Session.CLIENT_ACKNOWLEDGE);##第一個參數控制事務,第二個參數控制消息

另外ACKNOWLEDGE說明:

根據API說明

http://activemq.apache.org/maven/apidocs/org/apache/activemq/ActiveMQSession.html#acknowledge--

若是調用ack將會ack這個session的全部消息。

Acknowledges all consumed messages of the session of this consumed message.

All consumed JMS messages support the acknowledge method for use when a client has specified that its JMS session's consumed messages are to be explicitly acknowledged. By invokingacknowledge on a consumed message, a client acknowledges all messages consumed by the session that the message was delivered to.

Calls to acknowledge are ignored for both transacted sessions and sessions specified to use implicit acknowledgement modes.

A client may individually acknowledge each message as it is consumed, or it may choose to acknowledge messages as an application-defined group (which is done by calling acknowledge on the last received message of the group, thereby acknowledging all messages consumed by the session.)Messages that have been received but not acknowledged may be redelivered.

還好AcitveMq提供了另一種方法來避免一個消息一次ack 將會ack掉所有消息的問題。

 session = connection.createSession(Boolean.FALSE,
                    ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);

參考 Java Enterprise Edition : A Practical Approach - 第 228 頁

在不帶事務的 Session 中,一條消息什麼時候和如何被簽收取決於Session的設置。 

1.Session.AUTO_ACKNOWLEDGE 

當客戶端從 receive 或 onMessage成功返回時,Session 自動簽收客戶端的這條消息的收條。

2.Session.CLIENT_ACKNOWLEDGE 

    客戶端經過調用消息的 acknowledge 方法簽收消息。

message.acknowledge();

在帶事務的 Session 中,簽收自動發生在事務提交時。若是事務回滾,全部已經接收的消息將會被再次傳送。其實這裏的Session.CLIENT_ACKNOWLEDGE 用處不大。

session = connection.createSession(Boolean.TRUE, Session.CLIENT_ACKNOWLEDGE);
session.commit();

 

總結:

一、對於生產者:服務端端爲P,客戶端爲ActiveMQ服務。  Session設置爲AUTO_ACKNOWLEDGE 和CLIENT_ACKNOWLEDGE ,相對來講區別不是很大,根據狀況考慮。

二、對於消費者:服務端爲ActiveMQ爲服務,客戶端爲C。 Session設置爲AUTO_ACKNOWLEDGE ,接收到消息(receive 或 onMessage成功返回時),即爲消費成功,而後從隊列裏移除該數據。不關心該數據有沒有正確被處理成咱們想要的結果;Session設置爲CLIENT_ACKNOWLEDGE 時,必須手動調用acknowledge 方法才爲消費成功,而後從隊列裏移除該條數據。

三、P和C的Session設置成哪一種模式,互不影響。

相關文章
相關標籤/搜索