我想执行以下操作:当一条消息失败并落入我的死信队列时,我想等待5分钟,然后将同一条消息重新发布到我的队列中。
今天,使用Spring Cloud Streams和RabbitMQ,我做了以下代码Based on this documentation:
@Component
public class HandlerDlq {
private static final Logger LOGGER = LoggerFactory.getLogger(HandlerDlq.class);
private static final String X_RETRIES_HEADER = "x-retries";
private static final String X_DELAY_HEADER = "x-delay";
private static final int NUMBER_OF_RETRIES = 3;
private static final int DELAY_MS = 300000;
private RabbitTemplate rabbitTemplate;
@Autowired
public HandlerDlq(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@RabbitListener(queues = MessageInputProcessor.DLQ)
public void rePublish(Message failedMessage) {
Map<String, Object> headers = failedMessage.getMessageProperties().getHeaders();
Integer retriesHeader = (Integer) headers.get(X_RETRIES_HEADER);
if (retriesHeader == null) {
retriesHeader = 0;
}
if (retriesHeader > NUMBER_OF_RETRIES) {
LOGGER.warn("Message {} added to failed messages queue", failedMessage);
this.rabbitTemplate.send(MessageInputProcessor.FAILED, failedMessage);
throw new ImmediateAcknowledgeAmqpException("Message failed after " + NUMBER_OF_RETRIES + " attempts");
}
retriesHeader++;
headers.put(X_RETRIES_HEADER, retriesHeader);
headers.put(X_DELAY_HEADER, DELAY_MS * retriesHeader);
LOGGER.warn("Retrying message, {} attempts", retriesHeader);
this.rabbitTemplate.send(MessageInputProcessor.DELAY_EXCHANGE, MessageInputProcessor.INPUT_DESTINATION, failedMessage);
}
@Bean
public DirectExchange delayExchange() {
DirectExchange exchange = new DirectExchange(MessageInputProcessor.DELAY_EXCHANGE);
exchange.setDelayed(true);
return exchange;
}
@Bean
public Binding bindOriginalToDelay() {
return BindingBuilder.bind(new Queue(MessageInputProcessor.INPUT_DESTINATION)).to(delayExchange()).with(MessageInputProcessor.INPUT_DESTINATION);
}
@Bean
public Queue parkingLot() {
return new Queue(MessageInputProcessor.FAILED);
}
}
我的MessageInputProcessor
界面:
public interface MessageInputProcessor {
String INPUT = "myInput";
String INPUT_DESTINATION = "myInput.group";
String DLQ = INPUT_DESTINATION + ".dlq"; //from application.properties file
String FAILED = INPUT + "-failed";
String DELAY_EXCHANGE = INPUT_DESTINATION + "-DlqReRouter";
@Input
SubscribableChannel storageManagerInput();
@Input(MessageInputProcessor.FAILED)
SubscribableChannel storageManagerFailed();
}
还有我的属性文件:
#dlx/dlq setup - retry dead letter 5 minutes later (300000ms later)
spring.cloud.stream.rabbit.bindings.myInput.consumer.auto-bind-dlq=true
spring.cloud.stream.rabbit.bindings.myInput.consumer.republish-to-dlq=true
spring.cloud.stream.rabbit.bindings.myInput.consumer.dlq-ttl=3000
spring.cloud.stream.rabbit.bindings.myInput.consumer.delayedExchange=true
#input
spring.cloud.stream.bindings.myInput.destination=myInput
spring.cloud.stream.bindings.myInput.group=group
使用此代码,我可以从死信队列中读取,捕获标头,但无法将其放回队列(即使行很慢,行LOGGER.warn("Retrying message, {} attempts", retriesHeader);
也只能运行一次)
我的猜测是方法bindOriginalToDelay
会将交换绑定到一个新队列,而不是我的队列。但是,我没有找到一种方法来让我的队列绑定到那里,而不是创建一个新队列。但是我什至不确定这是错误。
我也尝试发送到MessageInputProcessor.INPUT
而不是MessageInputProcessor.INPUT_DESTINATION
,但是它没有按预期工作。
不幸的是,由于对项目的依赖性,我无法更新Spring框架...
您能帮我一段时间后将失败的消息放回队列中吗?我真的不想在这里放一个thread.sleep
...
答案 0 :(得分:0)
在这种配置下,myInput.group
与路由键myInput
绑定到延迟的(主题)交换#
。
您可能应该删除spring.cloud.stream.rabbit.bindings.myInput.consumer.delayedExchange=true
,因为您不需要延迟主交换。
使用密钥myInput.group
,它也将绑定到您明确的延迟交换。
一切对我来说都是正确的;您应该看到绑定到两个交换的相同(单个)队列:
myInput.group.dlq
通过键DLX
绑定到myInput.group
您应该设置更长的TTL,并检查DLQ中的消息,看看是否有什么突出之处。
编辑
我只是将您的代码复制了5秒的延迟,对我来说效果很好(关闭了主交换机的延迟)。
Retrying message, 4 attempts
和
added to failed messages queue
也许您认为它不起作用是因为您也延迟了主交换?