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 ...
随机推荐
- 下拉列表---demo---bai
select.jsp <%@ page language="java" import="java.util.*" pageEncoding="U ...
- _tprintf(), printf(),wprintf() 与控制字符 %s 和 %S(Unicoe与GB2312))
_tprintf() 是 printf() 和 wprintf() 的通用类型:如果定义了 _unicode,那么 _tprintf() 就会转换为 wprintf(),否则为 printf() . ...
- 类型:Oracle;问题:oracle 时间加减;结果:ORACLE 日期加减操作
ORACLE 日期加减操作 无论是DATE还是timestamp都可以进行加减操作. 可以对当前日期加年.月.日.时.分.秒,操作不同的时间类型,有三种方法: 1 使用内置函数numtodsinter ...
- leetcode482
这道题主要使用了C++的几个API,大小写转化,字符串替换.其余的逻辑都比较简单.而且经查资料,string类字符串拼接的速度使用+=的速度是很快的.以下代码,也是用的+=来拼接字符串. string ...
- ios配合iTuns提取应用Documents下的文件到本地
出处:http://blog.csdn.net/jianandjan/article/details/50442988 有一些App需要通过使用iTunes让用户上传和下载文档.要让iOS程序支持iT ...
- eclipse 中文版 变成 英文版 方法
找到目录运行命令 “eclipse.exe -nl en”
- 复选框操作checked选中为true,反之为False,也可以赋值为true,false
- Angular问题01 创建组件时报错、HammerJS找不到
1 利用ng创建组件时出现错误 1.1 ng g c test/testHome 1.2 问题描述 当angular应用中有多个module.ts文件时,创建组件时会出现冲突,因为有多个module. ...
- 290. Word Pattern 单词匹配模式
[抄题]: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...
- Gearman 分布式的异步任务分发框架
What is Gearman? Gearman provides a generic application framework to farm out work to other machines ...