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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| package com.executor.service;
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; import java.util.stream.Collectors;
@Slf4j @Service public class TaskManagementService { @Autowired @Qualifier("coreExecutor") private Executor coreExecutor;
public List<String> executeBatchTasks(List<String> taskNames) { log.info("开始批量执行任务,任务数量: {}", taskNames.size()); List<CompletableFuture<String>> futures = taskNames.stream() .map(taskName -> CompletableFuture.supplyAsync(() -> { try { log.info("执行任务: {}", taskName); Thread.sleep(1000); return "任务完成: " + taskName; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("任务被中断: " + taskName, e); } }, coreExecutor)) .collect(Collectors.toList()); CompletableFuture<Void> allTasks = CompletableFuture.allOf( futures.toArray(new CompletableFuture[0]) ); try { allTasks.get(30, TimeUnit.SECONDS); List<String> results = futures.stream() .map(CompletableFuture::join) .collect(Collectors.toList()); log.info("批量任务执行完成,成功数量: {}", results.size()); return results; } catch (TimeoutException e) { log.error("批量任务执行超时", e); throw new RuntimeException("批量任务执行超时", e); } catch (Exception e) { log.error("批量任务执行失败", e); throw new RuntimeException("批量任务执行失败", e); } }
public String executeTaskWithResult(String taskName) { log.info("开始执行任务并获取结果: {}", taskName); CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { try { log.info("执行任务: {}", taskName); Thread.sleep(2000); return "任务结果: " + taskName; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("任务被中断: " + taskName, e); } }, coreExecutor); try { String result = future.get(10, TimeUnit.SECONDS); log.info("任务执行完成,结果: {}", result); return result; } catch (TimeoutException e) { log.error("任务执行超时: {}", taskName, e); throw new RuntimeException("任务执行超时: " + taskName, e); } catch (Exception e) { log.error("任务执行失败: {}", taskName, e); throw new RuntimeException("任务执行失败: " + taskName, e); } }
public String executeTaskWithExceptionHandling(String taskName, boolean shouldFail) { log.info("开始执行任务并处理异常: {}", taskName); CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { try { log.info("执行任务: {}", taskName); Thread.sleep(1000); if (shouldFail) { throw new RuntimeException("模拟任务失败"); } return "任务成功: " + taskName; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("任务被中断: " + taskName, e); } }, coreExecutor); CompletableFuture<String> handledFuture = future.handle((result, throwable) -> { if (throwable != null) { log.error("任务执行异常: {}", taskName, throwable); return "任务失败: " + taskName + ", 原因: " + throwable.getMessage(); } return result; }); try { String result = handledFuture.get(10, TimeUnit.SECONDS); log.info("任务处理完成: {}", result); return result; } catch (Exception e) { log.error("任务处理失败: {}", taskName, e); throw new RuntimeException("任务处理失败: " + taskName, e); } }
public String executeTaskChain(String initialTask) { log.info("开始执行任务链: {}", initialTask); CompletableFuture<String> future = CompletableFuture .supplyAsync(() -> { log.info("执行第一个任务: {}", initialTask); try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } return "第一步完成: " + initialTask; }, coreExecutor) .thenApplyAsync(result -> { log.info("执行第二个任务,基于: {}", result); try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } return result + " -> 第二步完成"; }, coreExecutor) .thenApplyAsync(result -> { log.info("执行第三个任务,基于: {}", result); try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } return result + " -> 第三步完成"; }, coreExecutor); try { String result = future.get(30, TimeUnit.SECONDS); log.info("任务链执行完成: {}", result); return result; } catch (Exception e) { log.error("任务链执行失败: {}", initialTask, e); throw new RuntimeException("任务链执行失败: " + initialTask, e); } }
public List<String> executeParallelTasks(List<String> taskNames) { log.info("开始执行并行任务,任务数量: {}", taskNames.size()); List<CompletableFuture<String>> futures = taskNames.stream() .map(taskName -> CompletableFuture.supplyAsync(() -> { try { log.info("并行执行任务: {}", taskName); Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 3000)); return "并行任务完成: " + taskName; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("任务被中断: " + taskName, e); } }, coreExecutor)) .collect(Collectors.toList()); CompletableFuture<Void> allTasks = CompletableFuture.allOf( futures.toArray(new CompletableFuture[0]) ); try { allTasks.get(60, TimeUnit.SECONDS); List<String> results = futures.stream() .map(CompletableFuture::join) .collect(Collectors.toList()); log.info("并行任务执行完成,成功数量: {}", results.size()); return results; } catch (Exception e) { log.error("并行任务执行失败", e); throw new RuntimeException("并行任务执行失败", e); } } }
|