zoukankan      html  css  js  c++  java
  • spring-retry 重试机制的使用

    场景:由于网络抖动原因,或者其他原因,需要对代码重新执行,这个就需要重试了。

    import org.springframework.context.annotation.Configuration;
    import org.springframework.retry.annotation.EnableRetry;
    
    /**
     * @desc 重试机制配置
     */
    @Configuration
    @EnableRetry
    public class RetryConfig {
    
    }
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.retry.annotation.Backoff;
    import org.springframework.retry.annotation.Recover;
    import org.springframework.retry.annotation.Retryable;
    import org.springframework.stereotype.Service;
    
    import java.time.LocalTime;
    
    @Service
    public class PayService {
        private Logger logger = LoggerFactory.getLogger(getClass());
    
        private final int totalNum = 100000;
    
        @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 1.5))
        public int minGoodsnum(int num) throws Exception {
            logger.info("减库存开始" + LocalTime.now());
            try {
                int i = 1 / 0;
            } catch (Exception e) {
                logger.error("illegal");
            }
            if (num <= 0) {
                throw new IllegalArgumentException("数量不对");
            }
            logger.info("减库存执行结束" + LocalTime.now());
            return totalNum - num;
        }
    
        @Recover
        public int recover(Exception e) {
            logger.warn("减库存失败!!!" + LocalTime.now());
            //记日志到数据库
            return totalNum;
        }
    }
        @Autowired
        private PayService payService;
    
        @GetMapping("/retry")
        public String getNum() throws Exception {
            int i = payService.minGoodsnum(-1);
            System.out.println("===="+i);
            return "succeess";
        }

    其他使用方法:https://blog.csdn.net/easy_to_know/article/details/86611839

  • 相关阅读:
    SCCM 2012系列之新特性
    本地用户管理
    ISA中的WEB链
    Windows Server 2012远程刷新客户端组策略,IE代理设置
    关于单一网络适配器拓扑TMG
    IP及DNS设置(Netsh)
    MIPI接口
    液晶屏MIPI接口与LVDS接口区别(总结)
    色彩和光的知识
    LED全彩显示屏色度空间
  • 原文地址:https://www.cnblogs.com/huzi007/p/11692043.html
Copyright © 2011-2022 走看看