首页 > 其他分享 >spring-retry 20220929

spring-retry 20220929

时间:2022-09-27 14:58:48浏览次数:66  
标签:retry 20220929 spring springframework org import annotation

 

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";
  }
}

 

 

 

 

 

标签:retry,20220929,spring,springframework,org,import,annotation
From: https://www.cnblogs.com/smallfa/p/16734528.html

相关文章