如何计算无状态服务中的递归次数?

成员之声

是否可以计算不应维护任何有状态变量的服务中的迭代次数?

@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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章