Springboot @retryable not retrying

前端 未结 6 1198
再見小時候
再見小時候 2021-02-19 11:03

The following code is not retrying. What am I missing?

@EnableRetry
@SpringBootApplication
public class App implements CommandLineRunner
{
    .........
    ....         


        
6条回答
  •  孤街浪徒
    2021-02-19 12:01

    I solved it. I figured out that if return something from the method that you trying to retry, then @Retryable() is not working.

    maven dependency in pom.xml

        
            org.springframework.retry
            spring-retry
            1.1.5.RELEASE
        
    

    Spring boot Application.java

    @SpringBootApplication
    @EnableTransactionManagement
    @EnableRetry
    public class Application {
    
         public static void main(String[] args) throws Exception {
           SpringApplication.run(Application.class, args);
         }
    
    }
    

    in controller.java

    @RestController
    public class JavaAllDataTypeController {
    
    @Autowired
    JavaAllDataTypeService JavaAllDataTypeService;
    
    
    @RequestMapping(
            value = "/springReTryTest",
            method = RequestMethod.GET
    )
    public ResponseEntity springReTryTest() {
    
        System.out.println("springReTryTest controller");
    
        try {
             JavaAllDataTypeService.springReTryTest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return new  ResponseEntity("abcd", HttpStatus.OK);
      }
    
    }
    

    in service.java

    @Service
    @Transactional
    public class JavaAllDataTypeService {
    
     // try the method 9 times with 2 seconds delay.
     @Retryable(maxAttempts=9,value=Exception.class,backoff=@Backoff(delay = 2000))
     public void springReTryTest() throws Exception {
    
        System.out.println("try!");
        throw new Exception();
      }
    
    }
    

    output: It' trying 9 times then throwing exception.

提交回复
热议问题