搜索

spring-retry 20220929 - cn2022 - 博客园


发布时间: 2022-11-24 18:24:00    浏览次数:32 次

 

1、pom.xml

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
  </dependency>
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
  </dependency>
</dependencies>


2、application.properties

# 应用名称
spring.application.name=retry2036
# 应用服务 WEB 访问端口
server.port=2036

 

3、Retry2036Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;

@EnableRetry
@SpringBootApplication
public class Retry2036Application {

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

}

4、RetryService
public interface RetryService {
  void testRetry();
}


5、RetryServiceImpl
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class RetryServiceImpl implements RetryService {

  @Override
  @Retryable(value = Exception.class,maxAttempts = 5,backoff = @Backoff(
    delay = 3000,multiplier = 2,maxDelay = 20000
  ))
  public void testRetry() {
    System.out.println("in test retry:"+System.currentTimeMillis()/1000);
    int a=1/0;
      System.out.println("end in test retry");
  }

  @Recover
  public void reoover(Exception e){
    System.out.println("回调执行方法!!!");
  }
}


6、RetryController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RetryController {
  @Autowired
  private RetryService retryService;

  @GetMapping("/test_retry")
  @ResponseBody
  public String testRetry(){
  try {
    retryService.testRetry();
    return "ok";
  }catch (Exception e){
    e.printStackTrace();
  }
  return "error";
  }
}

 

 

 

 

 

免责声明 spring-retry 20220929 - cn2022 - 博客园,资源类别:文本, 浏览次数:32 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 06:24:00。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/smallfa/p/16734528.html