Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restart a method from its last failure point

Tags:

java

I have a method like this:

    public void runMethod()
    {
    method1();
    method2();
    method3();
    }

I want to call this runMethod multiple times based on an id. However, if say method2() fails for some reason, then when I call the runMethod, it should execute method3() and not try to execute method1() again (which has already run successfully for this id).

What would be the best way to achieve this?

Thanks a lot for the help

like image 790
Sai Avatar asked Oct 18 '12 15:10

Sai


2 Answers

You could record in a map whether a method has executed successfully or not.

private Map<String, Boolean> executes = new HashMap<String, Boolean>();

public void method1() {
    Boolean hasExecuted = executes.get("method1");
    if (hasExecuted != null && hasExecuted) {
        return;
    }
    executes.put("method1", true);
    ...
}

// and so forth, with method2, method3, etc
like image 145
FThompson Avatar answered Oct 23 '22 09:10

FThompson


You are looking for some kind of state machine. Keep state of the method execution in a data structure(E.g map).

At the beginning of method, you need to check whether execution of method1 for given id is executed successfully or not.

public void runMethod()
{
  method1();
  method2()
  method3();
}
private Set<Integer> method1Executed = new HashSet<Integer>();
private Set<Integer> method2Executed = new HashSet<Integer>();

private void method1(Integer id)
{
    if (method1Executed.contains(id)) {
        return;
    }
    // Processing. 
    method1Executed.add(id)
}

 // Similar code for method2.
like image 35
Htaras Avatar answered Oct 23 '22 10:10

Htaras