//Retry机制
public static class RetryExecutor
{
/// <summary>
/// 重试零个参数无返回值的方法
/// </summary>
/// <param name="action">执行方法方法</param>
/// <param name="retryInterval">重试间隔,单位秒</param>
/// <param name="retryCount">重试次数</param>
public static void Execute(Action action, int retryInterval = , int retryCount = )
{
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, int retryInterval = , int retryCount = )
{
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, int retryInterval = , int retryCount = )
{
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, int retryInterval = , int retryCount = )
{
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, int retryInterval = , int retryCount = )
{
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, int retryInterval = , int retryCount = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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, int retryInterval = , int retryCount = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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, int retryInterval = , int retryCount = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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, int retryInterval = , int retryCount = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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, int retryInterval = , int retryCount = )
{
var exceptions = new List<Exception>(); for (int retry = ; retry < retryCount; retry++)
{
try
{
return func(arg1, arg2, arg3, arg4);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
}
}

C#重试公用类的更多相关文章

  1. thinkphp3.2.2前后台公用类架构问题

    3.13之前好多项目都使用前后台公用类,在lib/action下创建Baseaction做为公共继承类,发现3.2.2里面很多人都用A调用,这样每用一次要用A调用,好麻烦,小编特意偷懒.亲测使用以下方 ...

  2. 第一步 使用sencha touch cmd 4.0 创建项目、打包(加入全局变量、公用类、自定义扩展、资源文件)

    参考资料: http://www.cnblogs.com/qqloving/archive/2013/04/25/3043606.html http://www.admin10000.com/docu ...

  3. ADO.NET连接数据库增删查改创建公用类

    顺序如下:再web.config中添加数据库的用户名和密码 创建公用类. //添加进web.config中的账号和密码 /*<connectionStrings> <add name ...

  4. sencha touch 我的公用类myUtil(废弃 仅参考)

    /*公共类*/ Ext.define('myUtil', { statics: { //store公用加载方法 storeLoadById: function (id) { var store = E ...

  5. Tomcat 7下如何利用 catalina.properties 部署公用类

    Tomcat 有很多配置文件,其中一个是  catalina.properties ,本文介绍catalina.properties 中的设置项. 一.组成   catalina.properties ...

  6. C#异常重试通用类Retry

    //Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...

  7. C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类

    最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...

  8. 2015-09-17 001 日志与对话框公用类_public

    using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...

  9. C# HTTP请求对外接口、第三方接口公用类

    /// <summary> /// 网络数据请求公共函数 /// </summary> public class HttpWebRequestCommon { #region ...

随机推荐

  1. Liferay7 BPM门户开发之15: Liferay开发体系简介

    Liferay SDK 开发体系 主要分6种: Portlet Hook Theme Layout Templates Web Modules Ext Portlet :类似于servlet的web组 ...

  2. Selenium3 + Python3自动化测试系列一——安装Python+selenium及selenium3 浏览器驱动

    一.安装Python https://www.python.org/downloads/ 验证Python是否安装成功.打开Windows命令提示符(cmd),输入python,回车 注意:在安装的过 ...

  3. ajax 请求被终止 chrome查询发现请求状态status为canceled

    检查页面的network执行中发现页面被刷新了url改变了导致请求在请求过程中被终止了. 检查代码发现在 submit方法中最后写了个  location.reload();方法 来重载页面 虽然是卸 ...

  4. 安装Elasticsearch中Head插件并使用

    基础环境 Elasticsearch集群搭建请参考前一篇文章http://www.cnblogs.com/aubin/p/8012840.html 系统 节点名 IP 软件版本 CentOS7.3 e ...

  5. Alienware 15 R3 装Ubuntu 和 win10 双系统

    一.安装环境 Alienware 15 R3 win10 专业版64位 ubuntu16.04 二.软件下载 1.Ubuntu16.04 下载地址:https://www.ubuntu.com/dow ...

  6. vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...

  7. 持续集成工具-Jenkins 使用介绍

    Jenkins 是一个可扩展的持续集成引擎,可以为我们提供代码自动编译.打包和发布工作,减少部署成本. 一.安装与启动 Jenkins 提供了多种便捷的安装方式,比较推荐使用执行 war 包的方式. ...

  8. underscore.js源码解析【对象】

    // Object Functions // ---------------- // Keys in IE < 9 that won't be iterated by `for key in . ...

  9. Jstl标签汇总

    JSTL的核心标签库标签共13个,使用这些标签能够完成JSP页面的基本功能,减少编码工作. 从功能上可以分为4类:表达式控制标签.流程控制标签.循环标签.URL操作标签.  (1)表达式控制标签:ou ...

  10. 如何设计和实现高可用的MySQL

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 王甲坤,腾讯高级工程师.腾讯云关系型数据库MySQL负责人,拥有多年客户端.数据库 ...