簡體   English   中英

如何從隊列中刪除消息

[英]How to remove a message from the queue

我有一個代碼,我在其中收到消息:

    @Override
    public Message doInRabbit(Channel channel) throws Exception {
        GetResponse result = channel.basicGet("club-pro-not-available", false);
        if (result == null) {
            return null;
        }
        channel.basicReject(result.getEnvelope().getDeliveryTag(), true);
        return new Message(result.getBody(), propertiesConverter.toMessageProperties(
                result.getProps(), result.getEnvelope(), "UTF-8"));
    }

我在帶有預定標志的方法中調用此方法:

  @Scheduled(fixedDelay = 10000)
    public void sendMessageClubPro() throws IOException {
        while (true) {
            try {
                 Message message = getMessagesOfRabbit();
              Logic
            } catch (Exception e) {
                Error logic
            }
        }
    }

我想試試,如果一切順利,就從隊列中刪除消息,但是我不明白怎么做,因為我只有一條消息,沒有通道。 我怎么解決這個問題?

PS也許這可以在rabbitTemplate的幫助下以某種方式完成,或者你可以通過某種方式獲得一個頻道? 我找不到例子。

您需要使用doInRabbit()進行所有處理,並根據處理的成功/失敗調用basicAck()basicReject()

像這樣無條件地調用basicReject()將始終將消息重新排隊。

您還可以在事務中運行RabbitTemplate.receive() ,事務管理器將根據事務是提交還是回滾來確認或取消消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM