xiari 10 місяців тому
батько
коміт
8cc8ea94d2

+ 9 - 2
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/check/ParallelTaskExecutor.java

@@ -79,7 +79,8 @@ public class ParallelTaskExecutor {
                 String taskName = futureToName.get(future);
 
                 try {
-                    T result = future.get();
+                    // 避免长时间阻塞,添加超时时间
+                    T result = future.get(5L, TimeUnit.SECONDS);
                     if (result instanceof R && ((R<?>) result).getCode()!=200) {
                         @SuppressWarnings("unchecked")
                         R<ErrorInfo> errorResult = (R<ErrorInfo>) result;
@@ -93,6 +94,12 @@ public class ParallelTaskExecutor {
                     }
                 } catch (CancellationException e) {
                     // 忽略已取消的任务
+                    log.info("忽略已取消的任务");
+                } catch (TimeoutException e) {
+                    // 获取单个任务执行5s超时,立即结束
+                    firstError.compareAndSet(null, CheckError.createTimeoutError());
+                    cancelPendingFutures(allFutures);
+                    break;
                 }
             }
         } catch (InterruptedException e) {
@@ -115,7 +122,7 @@ public class ParallelTaskExecutor {
     private <T> Callable<T> createLoggedTask(String taskName, IValidationTask<T> task, AtomicBoolean hasError) {
         return () -> {
             if (hasError.get()) {
-                throw new CancellationException("任务已取消");
+                throw new CancellationException("已有其他任务返回false,取消执行任务");
             }
 
             if (Thread.currentThread().isInterrupted()) {