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 = )
{
Excute<T1>(action, arg1, customerName, actionName, poNumber, new TimeSpan(, , ));
}
/// <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 = )
{
//var exceptions = new List<Exception>(); for (int retry = ; 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);
}
}
}
}
}
在其他类中调用
ActionExecutor.Excute(RequesetOrder, order,
CUSTOMER_NAME,
"request order detail", order.OrderNumber); //RequesetOrder 类方法
//order 是RequesetOrder的参数
C# 实现retry的更多相关文章
- 第3月第21天 nsclassfromstring返回null SVN报错:clean the working copy and then retry the operation
1. xcodeproj工程损坏时,.m文件没有加入编译. 2. SVN报错:clean the working copy and then retry the operation http://bl ...
- -bash: fork: retry: Resource temporarily unavailable
登陆不了服务器The server refused to start a shell. 登陆服务器后执行ls命令报错: 1 2 $ls -bash: fork: retry: Resource t ...
- java function retry wrapper
import java.util.concurrent.Callable; /** * Created by huahui.yang on 1/29/16. */ public class Retry ...
- Spring retry基本使用
Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...
- freebsd启动报错:My unqualified host name unkown...Sleeping for retry.
原文 http://blog.163.com/sujoe_2006/blog/static/335315120111158576591/ 病状:启动报"My unqualified host ...
- ebs R12.2启动报错"failed to start a managed process after the maximum retry limit"
启动日志: Error --> Process (index=1,uid=1739599208,pid=4479) failed to start a managed process after ...
- Windows Azure Storage 之 Retry Policy (用来处理短暂性错误-Transient Fault)
在使用Windows Azure Storage Service 的时候, 通常会遇到各种各样的问题. 例如网络连接不稳定,导致请求没有发出去.删除一个Blob Container 之后又立刻创建同名 ...
- 启动受管服务器出现:unable to get file lock, will retry...
启动受管服务器出现:unable to get file lock, will retry... 解决方法:一.删掉Domain下的*.lok文件1. 删除edit.lok进入到domain_home ...
- spring retry 使用
1. 场景 系统方法调用时无状态的,同时因为网络原因,或者系统暂时故障,进行的重试 2. maven 依赖 <project xmlns="http://maven.apa ...
- C# Retry重试操作解决方案(附源码)
一.前言 (1)对于Thread的Abort方法,如果线程当前正在执行的是一段非托管代码,那么CLR就不会抛出ThreadAbortException,只有当代码继续回到CLR中时,才会引发Threa ...
随机推荐
- Python Django框架 补充
Django REST framework ORM框架整理 Django框架 app间互借models字段的操作 ORM数据库操作补充:models中的一对一操作.过滤.事务 Django model ...
- VS2008 C++ 项目怎样添加“依赖”、“库目录”和“包含目录”
随笔 - 79, 文章 - 0, 评论 - 7, 引用 - 0 1. 添加编译所需要(依赖)的 lib 文件 [解决方案资源管理器]“项目->属性->配置属性->连接器->输入 ...
- Solr5.5.3的研究之路 ---1、从Mysql导入数据并创建索引
公司需要用到全文检索,故使用Solr,也是新人一枚,本人查看的前提是Solr已经安装部署成功,我用的服务器是自带的Jetty 1.创建Collection [root@whoami bin]# ./s ...
- setAttribute这个方法
setAttribute这个方法,在JSP内置对象session和request都有这个方法,这个方法作用就是保存数据,然后还可以用getAttribute方法来取出.比如现在又个User对象,Use ...
- C++下基本类型所占位数和取值范围
原文:http://hi.baidu.com/magicdemon/blog/item/821b2e22d7df494cad34debd.html C++下基本类型所占位数和取值范围: 符号属性 ...
- oracle --(二)分区(extent)
基本关系:数据库---表空间---数据段---分区---数据块 一.分区(extent)分区extent是比数据块大一级的存储结构,是几个逻辑上相邻的data block的组合.我们知道,物理存储通常 ...
- 【总结整理】行内标签span设置position:absolute/float属性可以设置宽度与高度
postion:absolute 跳出文本流,不是行内元素,设置宽高有效,我的理解. 引用下曹刘阳写的<编写高质量代码-web前端开发修炼之道>一书中看到的一句话:position:abs ...
- 孕妇的孕周和体重对胎儿游离DNA在母体cfDNA占比的影响
有一篇文件,研究了孕妇的孕周和体重对 胎儿游离DNA在母体cfDNA中占比的影响 文章名称:Gestational age and maternal weight effects on fetal c ...
- Jtabbedpane设置透明、Jpanel设置透明
摘自 https://zhidao.baidu.com/question/983204331427010139.html java中如何设置Jtabbedpane为透明 20 在Jtabbedpane ...
- Linux网络配置常用命令
计算机需要网络连接才能和其他计算机通信,这是通过操作系统识别接口卡(如以太网卡.猫等等),并配置该接口以连接到网络上来实现的.网络配置命令可以用来配置以下类型的网络接口:以太网.ISDN(综合业务数字 ...