问题描述
我的问题与这一问题非常相似: RabbitTemplate接收并重新排队不幸的是,尽管答案不符合我的需要,但已被标记为已回答.
My question is very similar to this one:RabbitTemplate receive and requeueUnfortunately it has been marked as answered though the answer doesn't suit my needs.
我想模仿Rabbit Admin UI的功能,即我想从队列中同步读取消息,但又不想让队列丢失它们,即像偷看一样.
I want to mimic the functionality of the Rabbit Admin UI, i.e. I want to synchronously read messages from a queue, but don't want the queue to lose them, i.e. something like having a peek.
此处的答案 RabbitTemplate接收和重新排队建议使用侦听器,但在这种情况下,会无限期地阅读和排队.我只想获取并重新排列消息一次,所以我想我应该使用RabbitTemplate而不是侦听器.
The answer here RabbitTemplate receive and requeue suggests using a listener, but in that case it'll read and requeue indefinitely. I want to get and requeue the messages just once, so I guess I should be using RabbitTemplate, not a listener.
推荐答案
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);
这篇关于RabbitTemplate接收消息并重新排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!