如何使用 Stomp 取消訊息確認 (unack)
常見問題 > 使用 Apache ActiveMQ Classic > 如何使用 Stomp 取消訊息確認 (unack)
在 Stomp 中沒有明確的「取消確認 (unack)」命令。一旦用戶端收到訊息,就不能將其標記為「未消耗」,並發送給另一個訂閱者(或再次重新發送給同一個訂閱者)。這取決於您的應用程式(或 Stomp 用戶端)來處理接收訊息處理失敗的情況,並實作「訊息重新傳遞」。
Stomp 交易常常被誤認為是這個用例的解決方案。但事實並非如此,因為交易僅與發送訊息和確認有關。如果您開始一個交易,在交易中發送訊息確認,最後中止它,訊息將不會再次重新傳遞。這僅表示如果達到預取限制,broker 將不會再向用戶端發送任何訊息。
請看以下範例
StompConnection connection = new StompConnection();
connection.open("localhost", 61613);
connection.connect("system", "manager");
connection.send("/queue/test", "message 1");
connection.send("/queue/test", "message 2");
connection.send("/queue/test", "message 3");
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("activemq.prefetchSize", "1");
connection.subscribe("/queue/test", "client", headers);
connection.begin("tx1");
StompFrame frame = connection.receive();
System.out.println(frame.getBody());
connection.ack(frame, "tx1");
connection.abort("tx1");
connection.begin("tx2");
connection.ack(frame, "tx2"); //sending the ack again
frame = connection.receive();
System.out.println(frame.getBody());
connection.ack(frame, "tx2");
connection.commit("tx2");
connection.begin("tx3");
frame = connection.receive();
System.out.println(frame.getBody());
connection.ack(frame, "tx3");
connection.commit("tx3");
這個簡單的應用程式將列印
message 1
message 2
message 3
由於交易 tx1
已中止,我們需要在 tx2
中再次確認該訊息,才能接收下一個訊息(因為使用的預取大小為 1)。
另請查看以下頁面以獲取更多資訊