I have a Task in my project , to complete that i have call(REST) multiple external systems. If my call fails at some level , i have to rollback all my previous calls(making call with undo action) .
Looking for effective way of implementing it in JAVA. Can Someone point me to any design pattern if exists?
public void myTask(){
call subtask1() ; //external sys
try{
call subtask2();
}
catch(Exception){
call undo_subtask1();
}
try{
call subtask3();
}
catch(Exception){
call undo_subtask1();
call undo_subtask2(); //calling in any order is fine.
}
.....
}
The above code is similar to database rollback. Tomorrow there can be subtask 4, 5,6 . I don't want to add try/catch again. Is there recommended way to achieve this?
When i googled i got command pattern. But i don't understand how that will help my case.
Thanks