1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
@Service public class ConditionalTaskOrchestrationService {
@Autowired private CompletableFutureTaskManager taskManager;
@Autowired private RedisTemplate<String, Object> redisTemplate;
private final String CONDITIONAL_TASK_CACHE_PREFIX = "conditional_task:";
public <T> CompletableFuture<T> executeConditionalTask( Supplier<Boolean> condition, Supplier<T> trueTask, Supplier<T> falseTask, String orchestrationId) { try { validateConditionalTask(condition, trueTask, falseTask, orchestrationId);
CompletableFuture<Boolean> conditionFuture = taskManager.createAsyncTask(condition, orchestrationId + "_condition");
CompletableFuture<T> result = conditionFuture.thenCompose(conditionResult -> { if (conditionResult) { return taskManager.createAsyncTask(trueTask, orchestrationId + "_true"); } else { return taskManager.createAsyncTask(falseTask, orchestrationId + "_false"); } });
recordOrchestrationInfo(orchestrationId, "CONDITIONAL", 3);
return result;
} catch (Exception e) { log.error("条件执行任务失败: {}", orchestrationId, e); return CompletableFuture.failedFuture(e); } }
private void validateConditionalTask(Object condition, Object trueTask, Object falseTask, String orchestrationId) { if (condition == null) { throw new IllegalArgumentException("条件不能为空"); }
if (trueTask == null) { throw new IllegalArgumentException("条件为真时的任务不能为空"); }
if (falseTask == null) { throw new IllegalArgumentException("条件为假时的任务不能为空"); }
if (orchestrationId == null || orchestrationId.trim().isEmpty()) { throw new IllegalArgumentException("编排ID不能为空"); } }
private void recordOrchestrationInfo(String orchestrationId, String type, int taskCount) { try { TaskOrchestrationInfo info = new TaskOrchestrationInfo(); info.setOrchestrationId(orchestrationId); info.setType(type); info.setTaskCount(taskCount); info.setCreateTime(LocalDateTime.now());
String cacheKey = CONDITIONAL_TASK_CACHE_PREFIX + orchestrationId; redisTemplate.opsForValue().set(cacheKey, info, Duration.ofHours(1));
} catch (Exception e) { log.error("记录编排信息失败", e); } }
public <T> CompletableFuture<T> executeMultiConditionalTask( List<ConditionalTask<T>> conditionalTasks, Supplier<T> defaultTask, String orchestrationId) { try { validateMultiConditionalTask(conditionalTasks, defaultTask, orchestrationId);
CompletableFuture<T> result = CompletableFuture.completedFuture(null);
for (int i = 0; i < conditionalTasks.size(); i++) { final int taskIndex = i; final ConditionalTask<T> conditionalTask = conditionalTasks.get(taskIndex); final String taskId = orchestrationId + "_" + taskIndex;
result = result.thenCompose(previousResult -> { if (previousResult != null) { return CompletableFuture.completedFuture(previousResult); }
return taskManager.createAsyncTask(conditionalTask.getCondition(), taskId + "_condition") .thenCompose(conditionResult -> { if (conditionResult) { return taskManager.createAsyncTask(conditionalTask.getTask(), taskId + "_task"); } else { return CompletableFuture.completedFuture(null); } }); }); }
result = result.thenCompose(finalResult -> { if (finalResult == null && defaultTask != null) { return taskManager.createAsyncTask(defaultTask, orchestrationId + "_default"); } else { return CompletableFuture.completedFuture(finalResult); } });
recordOrchestrationInfo(orchestrationId, "MULTI_CONDITIONAL", conditionalTasks.size() + 1);
return result;
} catch (Exception e) { log.error("多条件执行任务失败: {}", orchestrationId, e); return CompletableFuture.failedFuture(e); } }
private void validateMultiConditionalTask(List<?> conditionalTasks, Object defaultTask, String orchestrationId) { if (conditionalTasks == null || conditionalTasks.isEmpty()) { throw new IllegalArgumentException("条件任务列表不能为空"); }
if (orchestrationId == null || orchestrationId.trim().isEmpty()) { throw new IllegalArgumentException("编排ID不能为空"); } } }
|