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 ...
随机推荐
- appium-unittest框架中的断言
1.首先unittest本身是一个python的测试框架,他有他自己的使用规则: 2.如果用其中的方法,需要引入,方法: import unittest class Login(unittest.Te ...
- [Apache]如何查看apache服务器的error log(错误日志)
在进行网页和服务器的测试时, 有时会提醒 500 Internal Server Error: The server encountered an internal error or misconfi ...
- java.lang.OutOfMemoryError: Java heap space异常
最近使用Tomcat跑项目时,其他项目可以正常运行,但有一个项目报java.lang.OutOfMemoryError: Java heap space异常,查了资料后,找到一个处理我所遇见异常的解决 ...
- CSS3 高级属性
尽管现代浏览器已经支持了众多的CSS3属性,但是大部分设计师和开发人员貌似依然在关注于一些很“主流”的属性,如border-radius.box-shadow或者transform等.它们有良好的文档 ...
- java多线程环境单例模式实现详解
Abstract 在开发中,如果某个实例的创建需要消耗很多系统资源,那么我们通常会使用惰性加载机制,也就是说只有当使用到这个实例的时候才会创建这个实例,这个好处在单例模式中得到了广泛应用.这个机制在s ...
- centos系统查看本机IP地址
centos系统查看本机IP地址,输入 ifconfig -a查看 centos查询上网公网IP输入 curl ifconfig.me 命令即可查看 centos查询上网网关IP,tracepath ...
- TortoiseSVN客户端安装遇到的问题汇总
在windows server 2003版本上安装32位SVN客户,提示以下错误 1:无法通过windows installer服务安装此安装程序包” 这时需要安装更新的windows install ...
- 在Mac OS里安装和升级Git
在此记录,给自己看,也给别人参考. 进入终端,查看当前Git版本,输入指令:git --version 输入which git回车,可以查看当前git在什么位置 经查,版本:2.10.0,版本较低,为 ...
- ZBar在Windows上的使用 -- ImageMagick and OpenCV
博客转载自:https://blog.csdn.net/sunflower_boy/article/details/49095265 1. 下载ZBar v0.10 http://zbar.sourc ...
- 算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器
1. package algorithms.util; import algorithms.ADT.Stack; /****************************************** ...