Today see curator source code when it found its request zookeeper cluster when the internal encapsulation of the retry mechanism, the codes are as follows:
Stat Resultstat = Retryloop.callwithretry ( client.getzookeeperclient (), new callable<stat> () { @Override public Stat call () throws Exception { return Client.getzookeeper (). SetData (Path, data, version); } );
Public static<t> T Callwithretry (curatorzookeeperclient client, callable<t> proc) throws Exception { T result = null; Retryloop Retryloop = Client.newretryloop (); while (Retryloop.shouldcontinue ()) { try { client.internalblockuntilconnectedortimedout (); Call the callable method with the return value result = Proc.call (); Retryloop.markcomplete (); } catch (Exception e) { retryloop.takeexception (e); } } return result; }
use the Callable<t> in Java, a thread with a return value of T, to pass the code snippet that failed to retry to be called in the Retry function.
Because I want to develop the corresponding version of C #, I also need to pass a piece of code (request zookeeper code) to such a function with failed retry function, because I am not very familiar with C #, so I can think of only by declaring a proxy, Define an operation zookeeper function, assign the function to the proxy, and pass the proxy to the function with the retry function.
But this approach is bound to define a proxy, but also explicitly declare a method assigned to the agent, the code is large and not very elegant, so I began to understand how C # This aspect of implementation, and then found the anonymous method introduced by c#2.0, and c#3.0 introduced to replace the anonymous method of the lambda expression, is the first choice to write inline code. Here is a test code I wrote to complete my requirements before I explain what anonymous methods and lambda expressions are.
CustomDelegate.cs:
Using system;using system.collections.generic;using system.linq;namespace delegateandlamda{ Delegate string Customdelegate (string material);}
PizzaMaker.cs:
Namespace delegateandlamda{ class Pizzamaker {public static void Makepizza (string material, Customdelegate Bake) { //front have a bunch of fixed processes, but the next bake method is different string result = bake (material); Console.WriteLine (Result);}}}
Program.cs
Namespace delegateandlamda{ Class program {public static void Main (string[] args) { //C # before 2.0 Version, the only way to declare a delegate is to use the named Method Customdelegate Bake = new Customdelegate (Bakepizza); Pizzamaker.makepizza ("Zhang San Pizza", bake); C # 2.0 introduces the anonymous method Pizzamaker.makepizza ("John Doe Pizza", delegate (string material) { return String.Format ("will { 0} Bake into bread ", material); } ); In C # 3.0 and later, LAMBDA expressions replace anonymous methods as the preferred way to write inline code Pizzamaker.makepizza ("Jiyichin pizza", material + = { Return String.Format ("bake {0} to Italian pizza", material) ;} public static string Bakepizza (string material) { return String.Format ("bake {0} to watermelon", material);} }
Reference URL:
Commissioned by: http://msdn.microsoft.com/zh-cn/library/900fyy8e.aspx
Anonymous method: http://msdn.microsoft.com/zh-cn/library/0yw3tz5k.aspx
Lambda expression: http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
Expression tree: http://msdn.microsoft.com/zh-cn/library/bb397951.aspx
LINQ query: http://msdn.microsoft.com/zh-cn/library/bb397906.aspx
Delegates and lambda expressions in C #