The services consumes messages from Kafka and then sending them as emails (org.springframework.mail.javamail). Sometimes there is connection interruption happening and I would like to retry to send the message again. I'm using RetryTemplate as following:
@StreamRetryTemplate
RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();
AlwaysRetryPolicy alwaysRetryPolicy = new AlwaysRetryPolicy();
retryTemplate.setRetryPolicy(alwaysRetryPolicy);
ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
exponentialBackOffPolicy.setInitialInterval(backOffInitialInterval);
exponentialBackOffPolicy.setMaxInterval(backOffMaxInterval);
exponentialBackOffPolicy.setMultiplier(backOffMultiplier);
retryTemplate.setBackOffPolicy(exponentialBackOffPolicy);
log.info("Configured email consumer's back off: backOffInitialInterval={}, backOffMaxInterval={}, backOffMultiplier={}", backOffInitialInterval, backOffMaxInterval, backOffMultiplier);
return retryTemplate;
}
It works as expected (retries same message until connection is established), but for some reason after certain amount (actually can vary as I have noticied) of messages has been sent, the service (consumer) just hangs out and nothing is happening. When I restart the service sometimes it can hang out in the same way, but after some restarts it behaves correctly.
It is somehow related to partitions which are assigned to the consumer? Or what's going on? Any other better and simple approach of retrying?