Spring retry 和 guava retryer重试、@async 异步执行注解

Spring retry 和 guava retryer重试、@async 异步执行注解

spring retry

  • 依赖

    <!-- spring-retry -->
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.46</version>
    </dependency>
    <!-- /spring-retry -->
    
  • 代码实现

    • 异常类

      package com.retry.demo.springretry.exception;
      
      import lombok.Builder;
      import lombok.Getter;
      
      @Builder
      @Getter
      public class RetryException extends RuntimeException {
             
          private String code;
          private String message;
      }
      
    • 主类

      package net.fanci.stars;
      
      import net.fanci.stars.listener.PropertiesListener;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.context.annotation.Bean;
      import org.springframework.retry.annotation.EnableRetry;
      import org.springframework.scheduling.annotation.EnableAsync;
      import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
      
      import java.util.concurrent.Executor;
      
      @SpringBootApplication
      @EnableRetry
      public class StarsParentApplication {
             
          
          public static void main(String[] args) {
             
      //        SpringApplication.run(StarsParentApplication.class, args);
              SpringApplication application = new SpringApplication(StarsParentApplication.class);
              //加载配置文件
              application.addListeners(new PropertiesListener("config.properties"));
              application.run(args);
          }
      
          @Bean(name = "threadPoolTaskExecutor")
          public Executor threadPoolTaskExecutor() {
             
              return new ThreadPoolTaskExecutor();
          }
          
      }
      
    • 重试类

      package com.retry.demo.springretry.serivce;
      
      import com.retry.demo.springretry.exception.RetryException;
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.retry.annotation.Backoff;
      import org.springframework.retry.annotation.Recover;
      import org.springframework.retry.annotation.Retryable;
      import org.springframework.stereotype.Service;
      
      @Service
      @Slf4j
      public class RetryServiceImpl implements RetryService {
             
      
          int i = 1;
      	
          // 需要指定 maxDelay
          @Override
          @Retryable(include = {
             RetryException.class}, maxAttempts = 4, backoff = @Backoff(delay = 3000l,maxDelay = 60 * 1000L, multiplier = 1))
          public String retry() {
             
              log.info("测试retry");
              //生产环境此处应该为调用第三方接口,判断接口返回code
              if (i == 3) {
             
                  return i++ + "";
              }
              i++;
              log.info("============" + i);
              RetryException retryException = RetryException.builder().code("9999").message("连接超时").build();
              throw retryException;
          }
      
      
          @Recover
          public String recover(RetryException e) {
             
              log.info(e.getMessage());
              return "6";
          }
      }
      

guava retryer

  • 依赖

    <!-- guava-retrying -->
    <dependency>
        <groupId>com.github.rholder</groupId>
        <artifactId>guava-retrying</artifactId>
        <version>2.0.0</version>
    </dependency>
    <!-- /guava-retrying -->
    
  • 实现类

    package com.retry.demo.guavaretrying;
    
    import com.github.rholder
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值