logging messages on retry of rabbit message

旧街凉风 提交于 2019-12-11 03:01:52

问题


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>


来源:https://stackoverflow.com/questions/26283699/logging-messages-on-retry-of-rabbit-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!