logging messages on retry of rabbit message

2019-07-24 19:51发布

问题:

We're using RabbitMQ in our application to queue payment requests, and have another queue to send results back to the caller. In both cases, the client has requested a retry policy that will retry forever, but will put something in the log on each retry like "Retrying transaction for the xth time..." so that an external system can detect stuff backing up by monitoring the log file.

I'm creating listener container thus:

public SimpleMessageListenerContainer paymentListenerContainer() {
    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(rabbitConnectionFactory());
    container.setQueues(paymentQueue());
    container.setMessageListener(cpmPaymentListener());
    container.setConcurrentConsumers(configurationService.getAmqpConcurrentConsumers());
    container.setMaxConcurrentConsumers(configurationService.getAmqpMaxConcurrentConsumers());
    container.setAdviceChain(new Advice[] { paymentRetryInterceptor() });
    return container;
}

and defining the retry logic thus:

public RetryOperationsInterceptor paymentRetryInterceptor() {
    return RetryInterceptorBuilder.stateless()
            .maxAttempts(configurationService.getPaymentRetryMaxAttempts())
            .backOffOptions(configurationService.getPaymentRetryInitialInterval(), configurationService.getPaymentRetryMultiplier(), configurationService.getPaymentRetryMaxInterval()) // initialInterval, multiplier, maxInterval
            .build();
}

So the retry works flawlessly, but I can't find a hook to actually log anything on the retries. Is there something I'm missing? Is there a hook in there somewhere to execute something on the retry? Something I can subclass? Or is there some exsting logging buried within the retry logic in Spring that I can just enable in my logger config?

Thanks.

Chris.

回答1:

You can switch on DEBUG level for the org.springframework.retry.support.RetryTemplate category and you'll see in the logs something like:

2014-10-09 20:18:51,126 DEBUG main [org.springframework.retry.support.RetryTemplate] - <Retry: count=0>
2014-10-09 20:18:51,140 DEBUG main [org.springframework.retry.support.RetryTemplate] - <Checking for rethrow: count=1>
2014-10-09 20:18:51,140 DEBUG main [org.springframework.retry.support.RetryTemplate] - <Retry: count=1>
2014-10-09 20:18:51,141 DEBUG main [org.springframework.retry.support.RetryTemplate] - <Checking for rethrow: count=2>
2014-10-09 20:18:51,141 DEBUG main [org.springframework.retry.support.RetryTemplate] - <Retry: count=2>
2014-10-09 20:18:51,141 DEBUG main [org.springframework.retry.support.RetryTemplate] - <Checking for rethrow: count=3>
2014-10-09 20:18:51,141 DEBUG main [org.springframework.retry.support.RetryTemplate] - <Retry failed last attempt: count=3>