// Fault Tolerence Aspect. // aspect Auto-retry. This aspects automaticall catches calls to // the void run() method of any class, and if it throws a RuntimeException, // automatically retries the call three times before reporting failure. // Clearly this only works for stateless functions, which do not require // any side effects to be rolled back, which is why stateless functional // programming has an advantage in distributing computing environments // where faults are more common. aspect FaultTolerance // only use on stateless functions { private static int max = 3; // not visible outside aspect public static void setmax(int m) {max=m;} // regular function // an around advice replaces a method call: the advice need to // specify a return type even if it's void. To specify an // advice we need to write a 'pointcut' expression to identify // a 'join point' in the program with the 'cross-cutting concern' // of fault-torlence should be applied, in this case, the // calling of void run() on any Object c. Inside the advice, // we can 'proceed' with executing the original program from // the join point by calling proceed() void around(Object c): call(void *.run()) && target(c) { int cx = max; // allow 3 attempts to run while (cx-- >0) { try { proceed(c); // proceed with run() on target c cx = 0; // stops while loop } catch (RuntimeException re) { System.out.println(re); if (cx==0) System.out.println("run failed after "+max+" tries"); } }//while }//around advice }