是否可以计算不应维护任何有状态变量的服务中的迭代次数?
@Service //stateless spring service singleton
class MyService {
//this is clearly not stateless
int iterations = 0;
ResultVO run() {
interations++;
do();
some();
subroutines();
if (validationFails() && iterations <= 3) {
run(); //retry the iteration steps only 3 times
} else {
throw TooManyIterationsException();
}
return resultVO;
}
}
你可以做这样的事情
@Service //stateless spring service singleton
class MyService {
ResultVO theRun(int iterations) { // actual execution method
do();
some();
subroutines();
if (validationFails() && iterations <= 3) {
theRun(iterations+1); //retry the iteration steps only 3 times
} else {
throw TooManyIterationsException();
}
doSomethingWithIterations(iterations);
return resultVO;
}
ResultVO run() { // same name stub as before
return theRun(0);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句