C# 实现retry

C# 有try-catch ,但是没有retry 功能,通过用有限次循环的办法来模拟Retry,当然中间需要加一个等待的过程。

我们可以利用C#的匿名方法(anonymous methods)匿名委托(anonymous delegate)修饰此功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Exchange.Common
{
    public class ActionExecutor
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <param name="action"></param>
        /// <param name="arg1"></param>
        /// <param name="customerName"></param>
        /// <param name="actionName"></param>
        /// <param name="poNumber"></param>
        /// <param name="retryCount"></param>
        public static void Excute<T1>(Action<T1> action, T1 arg1, string customerName, string actionName, string poNumber, int retryCount = 3)
        {
            Excute<T1>(action, arg1, customerName, actionName, poNumber, new TimeSpan(0, 0, 3));
        }
        /// <summary>
        /// 重试一个参数带返回值
        /// </summary>
        /// <typeparam name="T1">参数类型1</typeparam>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="func">执行的方法</param>
        /// <param name="arg1">参数1</param>
        /// <param name="retryInterval">重试间隔</param>
        /// <param name="retryCount">重试次数</param>
        /// <returns>返回类型T</returns>
        public static void Excute<T1>(Action<T1> action, T1 arg1,string  customerName,string actionName, string poNumber, TimeSpan retryInterval, int retryCount = 3)
        {
            //var exceptions = new List<Exception>();

            for (int retry = 0; retry < retryCount; retry++)
            {
                try
                {
                    action(arg1);
                    return;
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Do(string.Format("{0} {1} \"{2}\"  error", customerName, actionName, poNumber, retry), ex);
                    //exceptions.Add(ex);
                    Thread.Sleep(retryInterval);
                }
            }
        }
    }
}
View Code

 

在其他类中调用

ActionExecutor.Excute(RequesetOrder, order,
                                        CUSTOMER_NAME,
                                                "request order detail", order.OrderNumber);

//RequesetOrder 类方法
//order 是RequesetOrder的参数

 

posted on 2017-04-18 17:46 szsunny 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/sxypeace/p/6729076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
axios-retry 是一个 Axios 的插件,用于在请求失败时自动重试。 使用 axios-retry 需要先安装 axios,然后再安装 axios-retry: ```bash npm install axios axios-retry ``` 然后在代码中引入 axios 和 axios-retry: ```javascript const axios = require('axios'); const axiosRetry = require('axios-retry'); ``` 接着,使用 axiosRetry() 函数将 axios-retry 插件应用到 axios 实例中: ```javascript const axiosInstance = axios.create(); axiosRetry(axiosInstance); ``` 现在,axiosInstance 实例就可以自动重试请求了。axios-retry 默认会在请求失败时自动重试 3 次,每次重试之间的间隔时间会逐渐增加。 如果需要自定义重试策略,可以通过传递一个配置对象来实现。下面是一个自定义重试策略的示例: ```javascript axiosRetry(axiosInstance, { retries: 5, // 最大重试次数 retryDelay: (retryCount) => { return retryCount * 1000; // 重试间隔时间,每次重试之间间隔时间逐渐增加 }, retryCondition: (error) => { // 重试条件,只有在返回 true 时才会重试 return axiosRetry.isNetworkError(error) || (error.response && error.response.status >= 500); }, }); ``` 这里的配置项包括: - retries:最大重试次数,默认为 3。 - retryDelay:重试间隔时间(毫秒),可以是一个数字或一个函数。如果是一个数字,则每次重试之间的间隔时间相同;如果是一个函数,则可以根据重试次数自定义间隔时间。默认间隔时间为 1000 毫秒。 - retryCondition:重试条件,只有满足该条件时才会重试。可以是一个函数,也可以是一个正则表达式或一个状态码数组。默认重试条件是网络错误或状态码为 5xx。 以上就是 axios-retry 的使用方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值