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的更多相关文章

  1. 第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 ...

  2. -bash: fork: retry: Resource temporarily unavailable

    登陆不了服务器The server refused to start a shell. 登陆服务器后执行ls命令报错:   1 2 $ls -bash: fork: retry: Resource t ...

  3. java function retry wrapper

    import java.util.concurrent.Callable; /** * Created by huahui.yang on 1/29/16. */ public class Retry ...

  4. Spring retry基本使用

    Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...

  5. freebsd启动报错:My unqualified host name unkown...Sleeping for retry.

    原文 http://blog.163.com/sujoe_2006/blog/static/335315120111158576591/ 病状:启动报"My unqualified host ...

  6. 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 ...

  7. Windows Azure Storage 之 Retry Policy (用来处理短暂性错误-Transient Fault)

    在使用Windows Azure Storage Service 的时候, 通常会遇到各种各样的问题. 例如网络连接不稳定,导致请求没有发出去.删除一个Blob Container 之后又立刻创建同名 ...

  8. 启动受管服务器出现:unable to get file lock, will retry...

    启动受管服务器出现:unable to get file lock, will retry... 解决方法:一.删掉Domain下的*.lok文件1. 删除edit.lok进入到domain_home ...

  9. spring retry 使用

    1.  场景      系统方法调用时无状态的,同时因为网络原因,或者系统暂时故障,进行的重试 2. maven 依赖 <project xmlns="http://maven.apa ...

  10. C# Retry重试操作解决方案(附源码)

    一.前言 (1)对于Thread的Abort方法,如果线程当前正在执行的是一段非托管代码,那么CLR就不会抛出ThreadAbortException,只有当代码继续回到CLR中时,才会引发Threa ...

随机推荐

  1. Python函数(四)-递归函数

    递归函数就是函数在自己内部调用自己 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" def Digui(n): print(n ...

  2. debian7 安装VMware Tools

    前提:需要安装gcc.kernel 1. apt-get install gcc 2. apt-get install kernel 问题描述: 安装时如果提示更换介质 :请把标有 " de ...

  3. 图解JVM和Tomcat类加载机制

    说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的web项目整合后无法直接断点调试.后来同 ...

  4. 具有避障和寻线功能的Arduino小车

    标签:  Arduino  乐高  机器人 创客对于成年人来说,多半是科技娱乐,或者是一种是一种向往科技的人生态度,总是希望自己不仅可以看到或者听到科技的资讯,还希望能够亲身制作科技玩意,从而更好地体 ...

  5. spring各版本下载地址

    发现从spring.io里面找下载连接不好找了,但是机智的我还是找到了,这里做下分享 一.纯spring http://repo.spring.io/release/org/springframewo ...

  6. 【259】ucpole.dat update

    2017年2月21日 57871 +0.020896 0.007232 +0.414732 0.009212 +0.418044 0.007533 p 57872 +0.022055 0.007284 ...

  7. 关于uboot的一些优化

    转载于:http://blog.163.com/solylee@126/blog/static/1718231572010101910485331/ 本人的开发环境是u-boot-1.1.6版本,fe ...

  8. ARC102E Stop. Otherwise...

    传送门 题目大意 现在有n个k面的骰子,问在i=2~2*k的情况下,任意两个骰子向上那一面的和不等于i的方案数是多少. 分析 这道题具体做法见这个博客. 至于k2的值为啥是那个自己画画图就明白了. 代 ...

  9. 229B Planets

    传送门 题目大意 有编号1~n的星球,在不用的星球间共有m个传送门,任意两个星球间传送门不超过1个,每个传送门需要花费一定的时间,但是在某时刻会在某星球有旅客到达,这时要一定等到没有旅客到达的时候才能 ...

  10. C++面试笔记--排序

    这里我们开始复习排序的一些面试题. 首先我们来看一下各个排序方法的时间复杂度和稳定性的比较,见下面表格: 排序法 平均时间 最差情形 稳定度 额外空间 备注 冒泡 O(n2)     O(n2) 稳定 ...