From c8e93afd420fc9362e535fc898917faa96e7fa48 Mon Sep 17 00:00:00 2001 From: lishuai <1793460677@qq.com> Date: Fri, 16 Aug 2024 14:50:41 +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=863?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/nl/acs/agv/QueryAGVStatus.java | 2 + .../org/nl/acs/opc/DeviceExecuteAutoRun.java | 96 ++----------------- .../src/main/java/org/nl/acs/opc/OpcUtl.java | 1 + .../config/thread/ThreadPoolExecutorUtil.java | 2 +- .../service/quartz/task/AutoCreateInst.java | 1 + .../service/quartz/task/CreateDDJInst.java | 1 + .../quartz/task/NdcAutoReconnection.java | 2 + .../src/main/resources/config/application.yml | 22 ++++- 8 files changed, 37 insertions(+), 90 deletions(-) diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/agv/QueryAGVStatus.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/agv/QueryAGVStatus.java index 269e7a94b..ed4278064 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/agv/QueryAGVStatus.java +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/acs/agv/QueryAGVStatus.java @@ -1,6 +1,7 @@ package org.nl.acs.agv; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; @@ -41,6 +42,7 @@ public class QueryAGVStatus { private DeviceAppService deviceAppService; public void run() { + log.info("定时查询AGV状态"+ DateUtil.now()); InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class); TaskService taskService = SpringContextHolder.getBean(TaskService.class); ISysParamService paramService = SpringContextHolder.getBean(ISysParamService.class); 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 cbc53fe61..de91a67a3 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 @@ -2,22 +2,18 @@ package org.nl.acs.opc; import lombok.extern.slf4j.Slf4j; import org.dromara.dynamictp.core.DtpRegistry; +import org.dromara.dynamictp.core.executor.DtpExecutor; +import org.dromara.dynamictp.core.support.ExecutorWrapper; import org.nl.acs.auto.run.AbstractAutoRunnable; import org.nl.acs.auto.run.AutoRunService; import org.nl.acs.device_driver.driver.ExecutableDeviceDriver; -import org.nl.acs.instruction.service.InstructionService; import org.nl.acs.udw.UnifiedDataAccessor; import org.nl.acs.udw.UnifiedDataAccessorFactory; -import org.nl.config.SpringContextHolder; -import org.nl.config.thread.TheadFactoryName; import org.nl.config.thread.ThreadPoolExecutorUtil; import org.springframework.beans.factory.annotation.Autowired; -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.*; @@ -38,10 +34,7 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable { int loop_time_millions = 100; - @Resource - private ThreadPoolExecutor executorService; - ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(30); Map runs; @@ -121,33 +114,8 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable { BlockedRunable runnable = new BlockedRunable() { @Override public void subRun() throws Exception { +// Thread.sleep(6000); deviceDriver.executeAuto(); - /* ThreadPoolExecutor executor= DtpRegistry.getDtpExecutor("dtpExecutor1"); - - Future submit = executor.submit(() -> { - try { - deviceDriver.executeAuto(); - } catch (Exception e) { - throw new RuntimeException(e); - } - }); - try { - // 设置超时时间为 15 秒,如果任务超时,抛出 TimeoutException - submit.get(10, TimeUnit.SECONDS); - System.out.println(); - } catch (TimeoutException e) { - System.out.println("任务超时,取消任务!"); - // 使用反射获取 FutureTask 中的线程,并使用 Thread.stop() 强制终止 - Field runnerField = submit.getClass().getDeclaredField("runner"); - runnerField.setAccessible(true); - Thread runnerThread = (Thread) runnerField.get(submit); - if (runnerThread != null) { - runnerThread.stop(); // 强制终止线程(不推荐) - } - - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - }*/ } @Override @@ -159,55 +127,16 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable { this.runs.put(deviceDriver.getDeviceCode(), runnable); } runnable.setIndex(this.runs); -// 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(); - }*/ + + ExecutorService executorService = (ExecutorService) ThreadPoolExecutorUtil.getPoll(); + Future future = executorService.submit(runnable); - //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(); // 强制终止线程(不推荐) - } + ScheduledExecutorService scheduler= (ScheduledExecutorService) DtpRegistry.getExecutor("scheduled_pool"); - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - }*/ - - // 使用调度器在超时后取消任务 + // 使用调度器在超时后取消任务 scheduler.schedule(() -> { if (!future.isDone()) { - -// future.cancel(true); // 尝试中断任务 -// Field runnerField = null; -// try { -// runnerField = future.getClass().getDeclaredField("runner"); -// runnerField.setAccessible(true); -// Thread runnerThread = (Thread) runnerField.get(future); -// if (runnerThread != null) { -// runnerThread.stop(); // 强制终止线程(不推荐) -// } -// } catch (Exception e) { -// throw new RuntimeException(e); -// } - try { // 设置超时时间为 30 秒,如果任务超时,抛出 TimeoutException future.get(5, TimeUnit.SECONDS); @@ -218,15 +147,8 @@ public class DeviceExecuteAutoRun extends AbstractAutoRunnable { } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } - - } - }, 5, TimeUnit.SECONDS); // 设置超时时间为10秒 - - - - - + }, 1, TimeUnit.MILLISECONDS); // 设置超时时间为10秒 } } } 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 d5a8dec41..edcb1a84c 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 @@ -52,6 +52,7 @@ public class OpcUtl { e1.printStackTrace(); } + boolean is_success = true; StringBuilder message = new StringBuilder(); Iterator arg4 = e.keySet().iterator(); diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java index ab73632f1..5e3c0e648 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java @@ -35,7 +35,7 @@ public class ThreadPoolExecutorUtil { public static Executor getPoll(){ - return DtpRegistry.getExecutor("dtpExecutor1"); + return DtpRegistry.getExecutor("cool_pool"); /* AsyncTaskProperties properties = SpringContextHolder.getBean(AsyncTaskProperties.class); return new ThreadPoolExecutor( diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java index 62183ceb6..7a4555c38 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java @@ -46,6 +46,7 @@ public class AutoCreateInst { * 创建指令前需要判断是否条件具备:起始位置是否有货、目标位置是否有货 */ public void run() throws Exception { + log.info("自动生成指令"+DateUtil.now()); TaskService taskserver = SpringContextHolder.getBean(TaskService.class); InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class); RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class); diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/CreateDDJInst.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/CreateDDJInst.java index c3d4868fc..3590b3615 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/CreateDDJInst.java +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/CreateDDJInst.java @@ -43,6 +43,7 @@ public class CreateDDJInst { * 定时查询堆垛机任务 */ public void run() { + log.info("定时查询堆垛机任务"+DateUtil.now()); TaskService taskserver = SpringContextHolder.getBean(TaskService.class); InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class); RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class); diff --git a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/NdcAutoReconnection.java b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/NdcAutoReconnection.java index c1b1d54ce..f5bd3a1c6 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/NdcAutoReconnection.java +++ b/acs2/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/NdcAutoReconnection.java @@ -1,5 +1,6 @@ package org.nl.system.service.quartz.task; +import cn.hutool.core.date.DateUtil; import lombok.extern.slf4j.Slf4j; import org.nl.acs.auto.run.AutoRunService; import org.nl.system.service.param.ISysParamService; @@ -20,6 +21,7 @@ public class NdcAutoReconnection { AutoRunService autoRunService; public void run(String threadCode) throws Exception { + log.info("定时NDC自动重连"+ DateUtil.now()); String[] threadCodes = threadCode.split(","); for (String code : threadCodes) { if (!autoRunService.getThreadByCode(code).isAlive()) { diff --git a/acs2/nladmin-system/nlsso-server/src/main/resources/config/application.yml b/acs2/nladmin-system/nlsso-server/src/main/resources/config/application.yml index a038b7eb6..42d9bf76e 100644 --- a/acs2/nladmin-system/nlsso-server/src/main/resources/config/application.yml +++ b/acs2/nladmin-system/nlsso-server/src/main/resources/config/application.yml @@ -57,7 +57,7 @@ spring: runTimeout: 10000 queueTimeout: 100 executors: # 动态线程池配置,都有默认值,采用默认值的可以不配置该项,减少配置量 - - threadPoolName: dtpExecutor1 # 线程池名称,必填 + - threadPoolName: cool_pool # 线程池名称,必填dtpExecutor1 threadPoolAliasName: core_thread # 线程池别名,可选 executorType: common # 线程池类型 common、eager、ordered、scheduled、priority,默认 common corePoolSize: 30 # 核心线程数,默认1 @@ -67,6 +67,24 @@ spring: rejectedHandlerType: CallerRunsPolicy # 拒绝策略,查看RejectedTypeEnum枚举类,默认AbortPolicy keepAliveTime: 30 # 空闲线程等待超时时间,默认60 threadNamePrefix: core_thread # 线程名前缀,默认dtp + tryInterrupt: true + allowCoreThreadTimeOut: true # 是否允许核心线程池超时,默认false + waitForTasksToCompleteOnShutdown: true # 参考spring线程池设计,优雅关闭线程池,默认true + awaitTerminationSeconds: 5 # 优雅关闭线程池时,阻塞等待线程池中任务执行时间,默认3,单位(s) + preStartAllCoreThreads: false # 是否预热所有核心线程,默认false + runTimeout: 2000 # 任务执行超时阈值,单位(ms),默认0(不统计) + queueTimeout: 1000 # 任务在队列等待超时阈值,单位(ms),默认0(不统计) + - threadPoolName: scheduled_pool # 线程池名称,必填 + threadPoolAliasName: scheduled_thread # 线程池别名,可选 + executorType: scheduled # 线程池类型 common、eager、ordered、scheduled、priority,默认 common + corePoolSize: 30 # 核心线程数,默认1 + maximumPoolSize: 50 # 最大线程数,默认cpu核数 + queueCapacity: 1024 # 队列容量,默认1024 + queueType: VariableLinkedBlockingQueue # 任务队列,查看源码QueueTypeEnum枚举类,默认VariableLinkedBlockingQueue + rejectedHandlerType: CallerRunsPolicy # 拒绝策略,查看RejectedTypeEnum枚举类,默认AbortPolicy + keepAliveTime: 30 # 空闲线程等待超时时间,默认60 + threadNamePrefix: scheduled_thread # 线程名前缀,默认dtp + tryInterrupt: true allowCoreThreadTimeOut: true # 是否允许核心线程池超时,默认false waitForTasksToCompleteOnShutdown: true # 参考spring线程池设计,优雅关闭线程池,默认true awaitTerminationSeconds: 5 # 优雅关闭线程池时,阻塞等待线程池中任务执行时间,默认3,单位(s) @@ -142,4 +160,4 @@ lucene: path: C:\acs\lucene\indexl tlog: pattern: "[$preApp][$preIp][$spanId][$traceId]" - enable-invoke-time-print: true \ No newline at end of file + enable-invoke-time-print: true