В случае исключения A: повторные попытки конечное число раз и, наконец, когда число повторных попыток исчерпано, сообщение записывается в очередь недоставленных сообщений
В случае исключения B: просто сообщение должно быть записано в очередь недоставленных сообщений
Я пытаюсь реализовать тот же вариант использования, что и в, и я выполнил все шаги в соответствии с правильным ответом, но я вижу, что мое настраиваемое исключение переносится в ListenerExecutionFailedException. Я не могу заставить его прекратить повторные попытки для настраиваемого исключения.
Я выполнил шаги в ответах ниже spring amqp разрешает повторную попытку с помощью конфигурации и предотвращает ее в соответствии с указанным исключением
а также
Кролик Spring пытается доставить отклоненное сообщение .. все в порядке ?
@Bean
public SimpleRetryPolicy rejectionRetryPolicy(){
Map<Class<? extends Throwable> , Boolean> exceptionsMap = new HashMap<Class<? extends Throwable> , Boolean>();
exceptionsMap.put(AmqpRejectAndDontRequeueException.class, false);//not retriable
exceptionsMap.put(ListenerExecutionFailedException.class, true); //retriable
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(3 , exceptionsMap ,true);
return retryPolicy;
}
@Bean
public RetryOperationsInterceptor workMessagesRetryInterceptor() {
return RetryInterceptorBuilder.stateless().retryPolicy(rejectionRetryPolicy())
//.backOffOptions(1000, 2, 10000)
.recoverer(new RepublishMessageRecoverer(defaultTemplate, this.getDlqExchange(), this.getDlqroutingkey()))
.build();
}
/* My Rabbit MQ Error handler */
@Component
public class RabbitRetryHandler implements RabbitListenerErrorHandler {
private static final Logger log = LoggerFactory.getLogger(RabbitRetryHandler.class);
@Override
public Object handleError(Message amqpMessage, org.springframework.messaging.Message<?> message,
ListenerExecutionFailedException exception) throws Exception {
if (amqpMessage.getMessageProperties().isRedelivered() || exception.getCause().getMessage().equals("DontRetry")) {
throw new AmqpRejectAndDontRequeueException(exception.getCause());
} else {
throw exception;
}
}
}
/* And finally my Listener */
@Override
@RabbitListener(queues = "${queueconfig.queuename}",containerFactory = "sdRabbitListenerContainerFactory",errorHandler="rabbitRetryHandler")
public void processMessage(Message incomingMsg) throws Exception {
log.info("{} - Correlation ID: {} Received message: {} from {} queue.", Thread.currentThread().getId(),
incomingMsg.getMessageProperties().getCorrelationId(), new String(incomingMsg.getBody()),
incomingMsg.getMessageProperties().getConsumerQueue());
try {
performAction();
} catch(CustomDontRequeueException cex) {
throw cex;
} catch (Exception ex) {
throw ex;
}
}
@Override
public void performAction() throws Exception {
try {
} catch (HttpClientErrorException ex) {
if (ex.getStatusCode() == HttpStatus.NOT_FOUND || ex.getStatusCode() == HttpStatus.REQUEST_TIMEOUT) {
throw new RuntimeException(ex);
} else {
throw new CustomDontRequeueException("DontRetry",ex);
}
}catch (Exception e) {
throw new CustomDontRequeueException(e);
}
}
Ожидаемый результат, если выбрасывается исключение CustomDontRequeueException, сообщение не должно быть повторно поставлено в очередь.
Фактический результат, сообщение повторно ставится в очередь независимо от того, какое исключение было n количество раз, а затем сбрасывается в DLQ.