fix:主备切换测试

This commit is contained in:
waner
2025-05-30 16:21:39 +08:00
parent 085d71feb9
commit fbcbe79388
11 changed files with 543 additions and 157 deletions
@@ -1,11 +1,7 @@
package com.sunyard.task;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class AsyncTaskExecutor {
@@ -20,24 +16,7 @@ public class AsyncTaskExecutor {
// 默认配置构造函数
public AsyncTaskExecutor() {
this(4, 2);
}
public List<TaskOutcome<String>> execTasks(List<TaskContext<String>> taskContexts) {
List<CompletableFuture<String>> futures =
taskContexts.stream().map(this::retryWithDelay).collect(Collectors.toList());
List<TaskOutcome<String>> taskOutcomes = new ArrayList<>();
taskContexts.forEach(task ->
retryWithDelay(task).whenCompleteAsync((result, ex) -> {
if (ex != null) {
// 提取原始异常
Throwable rootCause = ex instanceof CompletionException ? ex.getCause() : ex;
taskOutcomes.add(new TaskOutcome<>(task, rootCause));
} else {
taskOutcomes.add(new TaskOutcome<>(task, result));
}
}));
return taskOutcomes;
this(2, 2);
}
public List<TaskOutcome<String>> execBatchTasks(List<TaskContext<String>> taskContexts) {
@@ -94,12 +73,12 @@ public class AsyncTaskExecutor {
private <T> CompletableFuture<T> handleRetry(TaskContext<T> context, Throwable ex) {
if (context.canRetry()) {
context.decreaseRetry();
System.out.printf("[重试调度] ID: %s| IP:%s | 延迟: %ss | 剩余重试: %d | 错误: %s%n",
context.getTaskId(),
context.getAddress(),
context.getRetryDelay().getSeconds(),
context.getRetriesLeft(),
ex.getCause().getMessage());
// System.out.printf("[重试调度] ID: %s| IP:%s | 延迟: %ss | 剩余重试: %d | 错误: %s%n",
// context.getTaskId(),
// context.getAddress(),
// context.getRetryDelay().getSeconds(),
// context.getRetriesLeft(),
// ex.getCause().getMessage());
// 创建延迟重试的Future
CompletableFuture<T> delayedRetry = new CompletableFuture<>();