0

We are using spring-kafka-2.2.8.RELEASE. I have an specific situation where I need help. I have 4 topics topic, retryTopic, successTopic and errorTopic. If topic fails, should be redirected to retryTopic where the 3 attempts to retry will be made. If those attempts fails, must redirect to errorTopic. In case of sucess on both topic and retryTopic, should be redirected to the sucessTopic. This situation is already implemented based on the question How to retry with spring kafka version 2..2. But now, I have a new situation where I need to call the retryTopic listener from inside the topic listener based on a business logic error without an Exception been thrown(it already calls the retryTopic when an exception is thrown and it must remain with this behavior). And I also need to know on which retry attempt number the retryTopic is been called as a paramater of the listener bellow.

 @KafkaListener(id = "so60172304.2", topics = "retryTopic")
 public void listen2(String in) {
            RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.execute(new RetryCallback<Void, RuntimeException>() {
        @Override
        public Void doWithRetry(RetryContext retryContext) throws RuntimeException {
             // Can I get the retry count here? It didn't work
             Integer count =RetrySynchronizationManager.getContext().getRetryCount());
            return this.doWithRetry(retryContext);
        }
    }); 
  }
Lucas Lopes
  • 1
  • 1
  • 3

1 Answers1

0

There is no reason you can't call one listener from another (but you won't get retries unless you call it using a RetryTemplate in the first method).

If you use a RetryTemplate configured on the container factory to do the retries (rather than adding a BackOff to the SeektoCurrentErrorHandler in versions 2.3.x and higher), you can obtain the retry count (starting at zero) like this...

@KafkaListener(id = "so60172304.2", topics = "retryTopic")
public void listen2(String in) {
    int retryCount = RetrySynchronizationManager.getContext().getRetryCount();
    ...
}

getContext() will return null if you call this directly from the first method (unless you wrap the call in a RetryTemplate.execute()).

In 2.5.x a delivery attempt header will be available (optionally) even if using the SeektoCurrentErrorHandler with a BackOff instead of using a RetryTemplate in the container factory.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I urge you to keep up-to-date with the latest version in your minor release, even if you can't move up to a newer minor release (the current latest is 2.4.4). The latest 2.1.x is 2.1.14 - 6 bug fix releases since your version which is 8 months old. – Gary Russell Mar 23 '20 at 20:31
  • I've edited my question with some code, can I get the retry count like that in spring-kafka-2.2.8.RELEASE? – Lucas Lopes Mar 23 '20 at 23:27
  • You don't need another retry template there; that would be nested inside the main template. You only need a retry template if you call this listener method from another (and you need it there). PLEASE STOP USING OLD RELEASES. – Gary Russell Mar 23 '20 at 23:54
  • It's not my decision to use old releases. I've already told companie's architects to change the archetype and they didn't want to. You don't need to yell at me. – Lucas Lopes Mar 24 '20 at 12:03
  • I'm sorry; it's just very frustrating; it's hard enough to support 4 minor releases 2.4, 2.3, 2.2 and 1.3 without having to answer questions about ancient point releases for each. I spend a lot of my time adding features and fixing bugs; I sometimes wonder if I am wasting my time. Your architects need to get with the plan. Boot 2.1 (and hence SK 2.2) goes [end of life in November](https://spring.io/blog/2019/12/10/spring-boot-2-1-x-eol-november-1st-2020). – Gary Russell Mar 24 '20 at 13:40