zoukankan      html  css  js  c++  java
  • c# 中Exception 的重试机制

    有时候我们在项目中碰到,比如这样那样的原因(网络或者是其他的原因)导致方法调用失败,大部分的方法是在触发一次方法调用,其实我们可以通过自定义Exception,然后提供调用的方法名称和参数等等,提供一种自动的重试多次的机制。

    其实方法是很简单的,我们先自定义一个Exception

    代码如下

    

     public class RetryException : Exception
        {
            private int RetryTimes;
            private string MethodName;
            private object[] Values;
            private Type ClassType;
            public RetryException(int retryTimes, Type classType, string method, params object[] values)
            {
                RetryTimes = retryTimes;
                MethodName = method;
                Values = values;
                ClassType = classType;
                RetryMethod();
            }
            public RetryException(string message, int retryTimes, Type classType, string method, params object[] values)
                : base(message)
            {
                RetryTimes = retryTimes;
                MethodName = method;
                ClassType = classType;
                Values = values;
                RetryMethod();
            }
            public RetryException(string message, Exception innerException, int retryTimes, Type classType, string method, params object[] values)
                : base(message, innerException)
            {
                RetryTimes = retryTimes;
                MethodName = method;
                ClassType = classType;
                Values = values;
                RetryMethod();
            }
            private void RetryMethod()
            {
                if (string.IsNullOrEmpty(MethodName))
                    throw new Exception("method 为空");
                if (RetryTimes == 0)
                    throw new Exception("重试次数为空");
                if (ClassType == null)
                    throw new Exception("ClassType为空");
                object instance = Activator.CreateInstance(ClassType);
                MethodInfo Method = ClassType.GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
                do
                {
                    try
                    {   
                        Method.Invoke(instance, Values);
                    }
                    catch (Exception ex)
                    {
                        if (RetryTimes > 1)
                        {
                            System.Threading.Thread.Sleep(1000);
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                    RetryTimes--;
                } while (RetryTimes > 0);
            }
        }
    

    使用方法 我们先定义一个简单的方法如下

     public class service
        {
            public void GetName(string message,object obj)
            {
                if (message == "error")
                    throw new Exception(" message");
                   
            }
        }
    

    调用的方法是

      service sv = new service();
                object obj = new object();
                try
                {
                    sv.GetName("error",obj);
                }
                catch (Exception ex)
                {
                   new RetryException(5, typeof(service), "GetName", new object[] {"error",obj});
                }
    

  • 相关阅读:
    log4j 使用笔记整理中
    执行bat文件
    excel让每个单元格的宽度随着字体自动变动的两种方式(有更好方法的大神,请忽略,求评论下)
    XML中CDATA及其字符实体的使用
    Java文件读写操作指定编码方式。。。。。
    尾数为0零BigDecimal不能装成正常数
    jquery 自动补全控件(支持IE6)待整理
    $.ajax提交,后台接受到的值总是乱码?明天再总结
    js定义变量需赋予初始值
    存储过程的优缺点
  • 原文地址:https://www.cnblogs.com/jfliuyun/p/2054181.html
Copyright © 2011-2022 走看看