![]() |
Desktop Development »Miscellaneous »General IntermediateLicense:The Code Project Open License (CPOL)A Class for Retry ManagementByRotem SapirAllows the developer to use a built-in retry machanism, instead of having to develop it. | C#, .NET, Dev
|
Advanced Search |
|
On numerous occasions, it becomes necessary for a developer to perform retries. For example, when an application tries to access the DB to read data, or read from a file. We'll need our application to be able to retry, based on our own parameters, until it is successful, or until we want it to stop.
The library is a DLL which can be used in any application. It allows to specify a retry schedule. For example:
Such a retry schedule will enable the application to retry until it succeeds. Building a schedule this way allows the developer to make sure that the network/CPU is not overloaded by retries for a long time. It is possible to specify multiple schedules. Each will follow the other when the previous one completes.
The retry manager accepts a delegate to a method (Thanks toRamon for the tip). This delegate will call a function in the calling application. If that delegate returnsfalse
or throws an exception, the retry manager will retry based on the schedule defined.
Each retry action also raises an event to the calling application, with the retry data. This allows the developer to log the retry events, notify the user, and manage them.
The attached console application tries to perform an activity with two retry schedules. As shown, it will first try to do something. After failure, it will retry three times every second. If it fails, it will try every 2 seconds, indefinitely.
static RetryTimer.RetryManager manager =new RetryTimer.RetryManager();staticvoid Main(string[] args){ Console.WriteLine("Trying...");if (!TrySomething()) { Console.WriteLine("Failed, Retrying");//The action we tried did not succeed//Add an event handler to the retry mechanism manager.DoRetry +=new RetryTimer.RetryHandler(manager_DoRetry);//add 2 schedules//Try 5 times every 1 second manager.AddSchedule(RetryTimer.MeasurementUnit.Seconds,3,1);//If still unsuccessful - try indefinitely every 10 seconds manager.AddSchedule(RetryTimer.MeasurementUnit.Seconds, -1,2);//Start Retry RetryTimer.RetryCallback handler = TrySomething; manager.Start(handler); Console.ReadLine(); }else {//The actions succeeded Console.WriteLine("Success!"); }}
The retry event handler handles retrying and reports the results of the retry to the retry manager.
staticvoid manager_DoRetry(object sender, RetryTimer.DoRetryEventArgs args){ Result = (string.Format("Retrying {0} of {1} every {2} {3} at {4}",newobject[] {args.CurrentRetry, args.CurrentSchedule.RetryCount, args.CurrentSchedule.UnitCount, args.CurrentSchedule.UnitOfMeasure.ToString(), DateTime.Now.ToString()})); Console.WriteLine(Result);}
The retry manager will call the delegate based on the schedule, until stopped or until the schedules are completed.
publicvoid Start(RetryCallback callback) { StopRequested =false;if (mScheduleList.Count ==0) {thrownew Exception("No Schedules defined. Cannot Start"); }bool successTry =false;//loop through all scheduleswhile (currentScheduleIndex <= (mScheduleList.Count -1) && !successTry && !StopRequested) {while ((ElapsedCount <= mScheduleList[currentScheduleIndex].RetryCount || mScheduleList[currentScheduleIndex].RetryCount<0) && !successTry && !StopRequested) {try {if (callback()) { successTry =true; ; } }catch (Exception ex) {//throw; }if (DoRetry !=null) { DoRetry(this,new DoRetryEventArgs(mScheduleList[currentScheduleIndex], ElapsedCount)); }if (!successTry) {for (int numberOfUnits =0; numberOfUnits < mScheduleList[currentScheduleIndex].UnitCount * Convert.ToUInt32( mScheduleList[currentScheduleIndex].UnitOfMeasure) /1000; numberOfUnits++) { System.Threading.Thread.Sleep(1000); } } ElapsedCount++; } currentScheduleIndex++; ElapsedCount =1; } }
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
General
News
Question
Answer
Joke
Rant
Admin
PermaLink |Privacy |Terms of Use Last Updated: 6 Nov 2008 Editor:Deeksha Shenoy | Copyright 2008 by Rotem Sapir Everything elseCopyright ©CodeProject, 1999-2009 Web10 |Advertise on the Code Project |