//Retry机制
public static class Retry
{
/// <summary>
/// 重试零个参数无返回值的方法
/// </summary>
/// <param name="action">执行方法方法</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute(Action action, TimeSpan retryInterval, int retryCount = 3)
{
Execute<object>(() =>
{
action();
return null;
}, retryInterval, retryCount);
} /// <summary>
/// 重试一个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1>(Action<T1> action, T1 arg1, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, object>((x1) =>
{
action(arg1);
return null;
}, arg1, retryInterval, retryCount);
} /// <summary>
/// 重试两个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, T2, object>((x1, x2) =>
{
action(arg1, arg2);
return null;
}, arg1, arg2, retryInterval, retryCount);
} /// <summary>
/// 重试三个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, T2, T3, object>((x1, x2, x3) =>
{
action(arg1, arg2, arg3);
return null;
}, arg1, arg2, arg3, retryInterval, retryCount);
} /// <summary>
/// 重试四个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <typeparam name="T4">参数类型4</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="arg4">参数4</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, T2, T3, T4, object>((x1, x2, x3, x4) =>
{
action(arg1, arg2, arg3, arg4);
return null;
}, arg1, arg2, arg3, arg4, retryInterval, retryCount);
} /// <summary>
/// 重试零个参数带返回值
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T>(Func<T> func, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func();
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <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 T Execute<T1, T>(Func<T1, T> func, T1 arg1, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <summary>
/// 重试两个参数带返回值
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T1, T2, T>(Func<T1, T2, T> func, T1 arg1, T2 arg2, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1, arg2);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <summary>
/// 重试三个参数带返回值
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T1, T2, T3, T>(Func<T1, T2, T3, T> func, T1 arg1, T2 arg2, T3 arg3, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1, arg2, arg3);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <summary>
/// 重试四个参数带返回值
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <typeparam name="T4">参数类型4</typeparam>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="arg4">参数4</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T1, T2, T3, T4, T>(Func<T1, T2, T3, T4, T> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1, arg2, arg3, arg4);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
}
}

  

C#异常重试通用类Retry的更多相关文章

  1. Spring异常重试框架Spring Retry

    Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...

  2. 异常重试框架Spring Retry实践

    前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...

  3. 使用Apache HttpClient 4.x进行异常重试

    在进行http请求时,难免会遇到请求失败的情况,失败后需要重新请求,尝试再次获取数据. Apache的HttpClient提供了异常重试机制,在该机制中,我们可以很灵活的定义在哪些异常情况下进行重试. ...

  4. Spring错误异常重试框架guava-retrying

    官网:https://github.com/rholder/guava-retrying Maven:https://mvnrepository.com/artifact/com.github.rho ...

  5. Util应用程序框架公共操作类(五):异常公共操作类

    任何系统都需要处理错误,本文介绍的异常公共操作类,用于对业务上的错误进行简单支持. 对于刚刚接触.Net的新手,碰到错误的时候,一般喜欢通过返回bool值的方式指示是否执行成功. public boo ...

  6. 使用C# 操作存储过程,执行sql语句通用类

    如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码:     /// <summary>    /// Sql通用类    /// </summary>    ...

  7. Django子应用里的通用类混合视图处理

    # 子应用的views.py此文件用于编写Web应用视图,处理复杂的业务逻辑 #导入方法视图模板 from django.shortcuts import render #导包 from django ...

  8. poi导出excel通用类

    一.关键的通用类public class PoiExportUtils {    private static HSSFWorkbook workBook; public PoiExportUtils ...

  9. NPOI MVC 模型导出Excel通用类

    通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...

随机推荐

  1. C#开启异步 线程的四种方式

    一.异步委托开启线程public static void Main(string[] args){ Action<int,int> a=add; a.BeginInvoke(3,4,nul ...

  2. composer学习之路01

    以前对composer还是的理解很模糊,直到最近看一些资料,稍微有了一些浅显的了解. /* composer依赖包管理工具,如果一个项目是windows操作系统,那么composer就是360,他可以 ...

  3. Random Forest总结

    一.简介 RF = Bagging + Decision Tree 随机:数据采样随机,特征选择随机 森林:多个决策树并行放在一起 几个误区: 不是每棵树随机选择特征,而是每一个结点都随机选择固定数目 ...

  4. linux下chromedriver的安装

    很多时候,发现phantomjs被一些网站屏蔽导致我们无法达到想要的结果,一方面phantomjs也停止维护,这时候们可以使用chromedriver(谷歌)或者firefoxdriver(火狐)来代 ...

  5. Orange Greenworks

    对于steam游戏开发,成就功能是必不可少的. 而Rpgmaker系列无自带的插件或指令实现,且多数游戏作者并无熟练的脚本编写能力,所以~~ 我们要使用外部插件----Orange  Work. 这里 ...

  6. 【论文速读】Fangfang Wang_CVPR2018_Geometry-Aware Scene Text Detection With Instance Transformation Network

    Han Hu--[ICCV2017]WordSup_Exploiting Word Annotations for Character based Text Detection 作者和代码 caffe ...

  7. nginx 阻止非自己域名解析到服务器

    server模块加入 default_server server { listen 80 default_server; return 403; # return 301 https://$serve ...

  8. STD函数的内部计算公式

    各股票软件的标准差函数STD是不同的,而布林线的上下轨是以STD为基础计算出来的,所以使用布林线应小心.以2008/3/28的上证综指为例,利用如下代码:"收盘价3日STD:STD(CLOS ...

  9. decimal(19,6)什么意思

    decimal(19,6)什么意思 数字长度19位,精确到小数点后6位例如0.123456 mysql中varchar(50)最多能存多少个汉字 首先要确定mysql版本4.0版本以下,varchar ...

  10. 怎么批量删除qq空间说说

    1.打开自己的QQ空间 - 说说——右击说说审查元素,打开审查元素.或者直接按f12也可以. 2.在这里我们看到很多分类 3.点击 Console,进入Console项 4.粘贴删除说说的代码,代码为 ...