RabbitTemplate receives messages and requests
My question is very similar to this one: RabbitTemplate receive and request Unfortunately, it was marked as an answer, although the answer does not suit my needs.
I want to emulate the functionality of the Rabbit Admin interface, that is, I want to synchronously read messages from the queue, but I don't want the queue to lose them, that is, something like viewing.
The answer here RabbitTemplate receive and requeue suggests using a listener, but in this case it will read and request endlessly. I only want to receive and request messages once, so I assume I should be using a RabbitTemplate and not a listener.
+3
source to share
1 answer
class Peeker implements ChannelCallback<Message> {
final MessagePropertiesConverter propertiesConverter = new DefaultMessagePropertiesConverter();
@Override
public Message doInRabbit(Channel channel) throws Exception {
GetResponse result = channel.basicGet("someQ", 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"));
}
}
Peeker peeker = new Peeker();
...
Message peek = this.rabbitTemplate.execute(peeker);
+4
source to share