diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceExecuteAutoRun.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceExecuteAutoRun.java index 88e66cf34..bb3f19930 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceExecuteAutoRun.java +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/DeviceExecuteAutoRun.java @@ -13,6 +13,7 @@ import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.lang.reflect.Field; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.*; @@ -128,7 +129,38 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable { this.runs.put(deviceDriver.getDeviceCode(), runnable); } runnable.setIndex(this.runs); - this.executorService.execute(runnable); +// this.executorService.execute(runnable); + Future future = this.executorService.submit(runnable); + /* try { + // 设置超时时间为 30 秒,如果任务超时,抛出 TimeoutException + future.get(5, TimeUnit.SECONDS); + } catch (TimeoutException e) { + System.out.println("任务超时,取消任务!"); + // 超时后取消任务,并设置为 true 以中断执行中的线程 + future.cancel(true); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + }*/ + + + //plc断连后强制终止线程 + try { + // 设置超时时间为 15 秒,如果任务超时,抛出 TimeoutException + future.get(15, TimeUnit.SECONDS); + } catch (TimeoutException e) { + System.out.println("任务超时,取消任务!"); + // 使用反射获取 FutureTask 中的线程,并使用 Thread.stop() 强制终止 + Field runnerField = future.getClass().getDeclaredField("runner"); + runnerField.setAccessible(true); + Thread runnerThread = (Thread) runnerField.get(future); + if (runnerThread != null) { + runnerThread.stop(); // 强制终止线程(不推荐) + } + + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } } diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcUtl.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcUtl.java index d92564514..23d63e1ee 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcUtl.java +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/opc/OpcUtl.java @@ -1,5 +1,7 @@ package org.nl.acs.opc; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.date.TimeInterval; import cn.hutool.core.util.NumberUtil; import lombok.extern.slf4j.Slf4j; import org.jinterop.dcom.common.JIException;