From 1c88c2267bebea4cb85eaf4decee3a424bbd1a8e Mon Sep 17 00:00:00 2001 From: lishuai <1793460677@qq.com> Date: Thu, 15 Aug 2024 13:46:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=BA=BF=E7=A8=8B=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/nl/acs/opc/DeviceExecuteAutoRun.java | 34 ++++++++++++++++++- .../src/main/java/org/nl/acs/opc/OpcUtl.java | 2 ++ 2 files changed, 35 insertions(+), 1 deletion(-) 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;