Java Springboot @retryable 不重试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38212471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:05:05  来源:igfitidea点击:

Springboot @retryable not retrying

javaspringmavenspring-bootspring-retry

提问by engg

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

以下代码未重试。我错过了什么?

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


    @Retryable()
    ResponseEntity<String> authenticate(RestTemplate restTemplate, HttpEntity<MultiValueMap<String, String>> entity) throws Exception
    {
        System.out.println("try!");
        throw new Exception();
        //return restTemplate.exchange(auth_endpoint, HttpMethod.POST, entity, String.class);
    }

I have added the following to the pom.xml.

我已将以下内容添加到 pom.xml。

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

I also tried providing different combinations of arguments to @Retryable.

我还尝试为@Retryable 提供不同的参数组合。

@Retryable(maxAttempts=10,value=Exception.class,backoff=@Backoff(delay = 2000,multiplier=2))

Thanks.

谢谢。

回答by UserF40

For the @Retryableannotation on the method to be discovered it needs to be called correctly from an initialised context. Is the method invoked from a bean from the spring context or called by other means?

@Retryable要发现方法上的注释,需要从初始化的上下文中正确调用它。该方法是从 spring 上下文中的 bean 调用还是通过其他方式调用?

If testing this is your runner using the SpringJunit4ClassRunner?

如果测试这是您的跑步者使用SpringJunit4ClassRunner?

回答by Ferdous Wahid

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

我解决了。我发现如果从您尝试重试的方法中返回某些内容,则 @Retryable() 不起作用。

maven dependency in pom.xml

pom.xml 中的 maven 依赖

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.1.5.RELEASE</version>
    </dependency>

Spring boot Application.java

Spring启动应用程序.java

@SpringBootApplication
@EnableTransactionManagement
@EnableRetry
public class Application {

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

}

in controller.java

在控制器.java

@RestController
public class JavaAllDataTypeController {

@Autowired
JavaAllDataTypeService JavaAllDataTypeService;


@RequestMapping(
        value = "/springReTryTest",
        method = RequestMethod.GET
)
public ResponseEntity<String> springReTryTest() {

    System.out.println("springReTryTest controller");

    try {
         JavaAllDataTypeService.springReTryTest();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new  ResponseEntity<String>("abcd", HttpStatus.OK);
  }

}

in service.java

在 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.

输出:它尝试了 9 次然后抛出异常。

enter image description here

在此处输入图片说明

回答by Chinmay Samant

It work for return type as well

它也适用于返回类型

@Service
public class RetryService {

private int count = 0;

// try the method 9 times with 2 seconds delay.
@Retryable(maxAttempts = 9, value = Exception.class, backoff = @Backoff(delay = 2000))
public String springReTryTest() throws Exception {
    count++;
    System.out.println("try!");

    if (count < 4)
        throw new Exception();
    else
        return "bla";
  }

}

回答by nkharche

In spring boot 2.0.2 Release, I have observed that the @Retryable is not working if you have retryable and called method in same class. On debugging found that the pointcut is not getting built properly. For now, the workaround for this problem is that we need to write the method in a different class and call it.

在 spring boot 2.0.2 Release 中,我观察到如果您在同一个类中具有可重试和调用的方法,则 @Retryable 不起作用。在调试时发现切入点没有正确构建。目前,这个问题的解决方法是我们需要在不同的类中编写方法并调用它。

Working Example could be found here.

可以在此处找到工作示例。

回答by BiScOtTiNo

An alternative could be RetryTemplate

另一种可能是 RetryTemplate

@Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();

        FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        fixedBackOffPolicy.setBackOffPeriod(2000l);
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(2);
        retryTemplate.setRetryPolicy(retryPolicy);

        return retryTemplate;
    }

and

retryTemplate.execute(new RetryCallback<Void, RuntimeException>() {
    @Override
    public Void doWithRetry(RetryContext arg0) {
        myService.templateRetryService();
        ...
    }
});

worked out for me

为我工作

source

来源

回答by Bas Cancrinus

I had exactly the same issue as described in the original question.

我遇到了与原始问题中描述的完全相同的问题。

In my case it turned out that the spring-boot-starter-aopdependency was accidentally not included. After adding it to my pom.xml, my @Retryablemethods worked as expected.

在我的情况下,结果spring-boot-starter-aop意外地不包括依赖项。将其添加到 my 后pom.xml,我的@Retryable方法按预期工作。

Returning values from @Retryablemethods works fine for me.

@Retryable方法返回值对我来说很好用。