我正在使用RabbitMQ . 消费者-api正在使用spring-cloud-steam . 目前,当maxAttempts为3时,如果消费者未能处理该消息,则它将再次排队 . 这将发生3次 . 如果第3次也是消息失败,则它将被发送到DLX-> DLQ .
如下图所示
现在我想跳过重试,如果在侦听器端发生了一些specificException . 如何跳过此重试并直接将消息发送到DLX-> DLQ以获取specificException?
以下是配置文件 .
spring:
application:
name: consumer-api
cloud:
stream:
overrideCloudConnectors: true
bindings:
test_channel:
destination: destinationName
contentType: application/json
group: groupName
consumer:
maxAttempts: 3
rabbit:
bindings:
test_channel:
consumer:
durableSubscription: true
exchangeType: direct
bindingRoutingKey: do.test
autoBindDlq: true
deadLetterExchange: destinationName.dlx
deadLetterQueueName: destinationName.dlx.groupName.dlq
deadLetterRoutingKey: do.test
下面是监听器代码 .
@Slf4j
@Component
public class groupNameListener {
@StreamListener(TEST_CHANNEL)
public void doTest(Message<DoTestMessage> doTestMessage) {
log.info("doTest message received: {}", doTestMessage);
try {
if ("someCondition".equals(doTestMessage.getPayload().getMessage())) {
throw new SpecificException();
} else {
log.info("do normal work");
throw new Exception(); //if something fails
}
} catch (SpecificException specificException) {
log.error("Don't requeue but send to DLQ.... how ????");
} catch (Exception ex) {
log.error("Error in processing do test message");
throw new AmqpRejectAndDontRequeueException("Reject and Don't requeue exception");
//this will requeue the message maximum 3 times. If maxAttempts has reached then will be send to DLX->DLQ
}
}
}
有人可以帮帮我吗?如果我犯了任何错误,请告诉我 .
1 回答
即将发布的2.1发布adds the feature to specify which exceptions are, or are not, retryable .
2.1.0.M3(里程碑)现已上市 .
不可重试的异常将直接进入DLQ .