Denis Denis
Denis Denis

Reputation: 77

How to remove a message from the queue

I have a code in which I get the message:

    @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"));
    }

I call this method in the method with the scheduled flag:

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

I want to try, if everything was successful, delete the message from the queue, but I don't understand how to do this, because I only have a message, but no channel. How can I solve this problem?

P.S. Perhaps this can be done somehow with the help of rabbitTemplate or you can somehow get a channel? I can't find an example.

Upvotes: 1

Views: 485

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

You need to do all the processing with the the doInRabbit() and call basicAck() or basicReject() depending on the success/failure of processing.

Calling basicReject() like that unconditionally, will always requeue the message.

You could also run the RabbitTemplate.receive() in a transaction and the transaction manager will either ack or nack the message depending on whether the transaction commits or rolls back.

Upvotes: 2

Related Questions