import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class ServiceA {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
List<Callable<Boolean>> serviceCalls = new ArrayList<>();
List<Callable<Boolean>> rollbackCalls = new ArrayList<>();
List<Future<Boolean>> results = new ArrayList<>();
boolean previousCallSuccess = true;
serviceCalls.add(() -> callServiceB());
rollbackCalls.add(() -> rollbackServiceB());
serviceCalls.add(() -> callServiceC());
rollbackCalls.add(() -> rollbackServiceC());
serviceCalls.add(() -> callServiceD());
rollbackCalls.add(() -> rollbackServiceD());
try {
for (int i = 0; i < serviceCalls.size(); i++) {
Callable<Boolean> call = serviceCalls.get(i);
Callable<Boolean> rollbackCall = rollbackCalls.get(i);
if (!previousCallSuccess) {
System.out.println("Previous call failed, skipping current service call");
break;
}
Future<Boolean> result = executor.submit(call);
results.add(result);
boolean currentCallSuccess = result.get();
if (!currentCallSuccess) {
System.out.println("Current call failed, rolling back...");
for (int j = i; j >= 0; j--) {
executor.submit(rollbackCalls.get(j)).get();
}
previousCallSuccess = false;
} else {
previousCallSuccess = true;
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
executor.shutdown();
}
private static boolean callServiceB() {
System.out.println("Calling service B...");
return true;
}
private static boolean rollbackServiceB() {
System.out.println("Rolling back service B...");
return true;
}
private static boolean callServiceC() {
System.out.println("Calling service C...");
return true;
}
private static boolean rollbackServiceC() {
System.out.println("Rolling back service C...");
return true;
}
private static boolean callServiceD() {
System.out.println("Calling service D...");
throw new RuntimeException("Error occurred while calling service D");
}
private static boolean rollbackServiceD() {
System.out.println("Rolling back service D...");
return true;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class ServiceRollbackUtil {
private static ExecutorService executor = Executors.newCachedThreadPool();
public static void main(String[] args) {
List<String> services = new ArrayList<>();
services.add("B");
services.add("C");
services.add("D");
try {
boolean success = callServices(services);
if (!success) {
System.out.println("One or more services failed, performing rollback...");
} else {
System.out.println("All services executed successfully.");
}
} finally {
executor.shutdown();
}
}
public static boolean callServices(List<String> services) {
List<Callable<Boolean>> serviceCalls = new ArrayList<>();
List<Callable<Boolean>> rollbackCalls = new ArrayList<>();
List<Future<Boolean>> results = new ArrayList<>();
boolean previousCallSuccess = true;
for (String service : services) {
serviceCalls.add(() -> callService(service));
rollbackCalls.add(() -> rollbackService(service));
}
try {
for (int i = 0; i < serviceCalls.size(); i++) {
Callable<Boolean> call = serviceCalls.get(i);
Callable<Boolean> rollbackCall = rollbackCalls.get(i);
if (!previousCallSuccess) {
System.out.println("Previous call failed, skipping current service call");
break;
}
Future<Boolean> result = executor.submit(call);
results.add(result);
boolean currentCallSuccess = result.get();
if (!currentCallSuccess) {
System.out.println("Current call failed, rolling back...");
rollbackServices(rollbackCalls, i);
previousCallSuccess = false;
} else {
previousCallSuccess = true;
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return previousCallSuccess;
}
private static void rollbackServices(List<Callable<Boolean>> rollbackCalls, int index) throws InterruptedException, ExecutionException {
for (int j = index; j >= 0; j--) {
executor.submit(rollbackCalls.get(j)).get();
}
}
private static boolean callService(String serviceName) {
System.out.println("Calling service " + serviceName + "...");
return true;
}
private static boolean rollbackService(String serviceName) {
System.out.println("Rolling back service " + serviceName + "...");
return true;
}
}