0

I have a strange case with spring-retry @Retryable

I have the following play code in order to test the retry functionality of Spring and on pseudo random times to either retry or finish an execution.

@Scheduled(fixedRate = 10000)
  @Retryable(value = {IOException.class}, maxAttempts = 100, backoff = @Backoff(delay = 100000)) //delay 5 min
  public void playWithretry() {
    int retry = RetrySynchronizationManager.getContext().getRetryCount();
    try {
      log.debug("EXECUTING TEST RETRY ... ");
      if(System.currentTimeMillis()%3 != 0){
        log.debug(" NOW I WILL RETRY! thread name {} retry no {}", Thread.currentThread().getName(), retry);
        throw new IOException();
      }else {
        log.debug("NO RETRIES, I am DEAD! thread name {}", Thread.currentThread().getName());
        throw new JSchException();
      }
    } catch (IOException | JSchException e) {
      log.error("SOME ERROR");
    }
  }

Normally I would expect an output similar to the following with the RetrySynchronizationManager.getContext().getRetryCount() counting the retries

    EXECUTING TEST RETRY ...  
    NOW I WILL RETRY! thread name scheduling-1 retry no 0  
    SOME ERROR  
    EXECUTING TEST RETRY ...   
    NOW I WILL RETRY! thread name scheduling-1 retry no 1  
    SOME ERROR  
    EXECUTING TEST RETRY ...   
    NOW I WILL RETRY! thread name scheduling-1 retry no 2  
    SOME ERROR  
    EXECUTING TEST RETRY ...   
    NO RETRIES, I am DEAD! thread name scheduling-1  
    SOME ERROR  

But to my surprise I am getting no count on the retry like so:

    EXECUTING TEST RETRY ...  
    NOW I WILL RETRY! thread name scheduling-1 retry no 0  
    SOME ERROR  
    EXECUTING TEST RETRY ...   
    NOW I WILL RETRY! thread name scheduling-1 retry no 0  
    SOME ERROR  
    EXECUTING TEST RETRY ...   
    NO RETRIES, I am DEAD! thread name scheduling-1  
    SOME ERROR  

Does anyone have any idea why this could be happening?

In my pom.xml I have

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.9</version>
        <relativePath/>
</parent>
...
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
</dependency>

And I have in the configuration of the application I have added @EnableScheduling @EnableRetry

Thanks

Alexius DIAKOGIANNIS
  • 2,465
  • 2
  • 21
  • 32

0 Answers0