From 0c5a58771fcf86ad0b562829fbfa440da9392703 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期三, 17 十二月 2025 09:08:16 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/impl/ZyRgvThread.java | 71 +++++--
/dev/null | 124 ------------
src/main/java/com/zy/core/network/ZyRgvConnectDriver.java | 51 +++--
src/main/java/com/zy/core/network/ZyCrnConnectDriver.java | 51 +++--
src/main/java/com/zy/core/network/ZyStationConnectDriver.java | 52 +++--
src/main/java/com/zy/core/thread/impl/ZyStationThread.java | 79 ++++---
src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java | 70 ++++--
src/main/java/com/zy/core/MainProcess.java | 2
src/main/resources/application.yml | 12 +
src/main/java/com/zy/core/ServerBootstrap.java | 46 +---
10 files changed, 261 insertions(+), 297 deletions(-)
diff --git a/src/main/java/com/zy/common/model/Shelves.java b/src/main/java/com/zy/common/model/Shelves.java
deleted file mode 100644
index de2aa46..0000000
--- a/src/main/java/com/zy/common/model/Shelves.java
+++ /dev/null
@@ -1,124 +0,0 @@
-package com.zy.common.model;
-
-import com.alibaba.fastjson.JSON;
-import com.core.exception.CoolException;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * <strong>绔嬪簱璐ф灦瀹炰綋绫�</strong>
- * Created by vincent on 2020/6/11
- */
-public class Shelves {
-
- // 璐ф灦鎺掓暟閲�
- public final int size;
-
- // 璐ф灦缁勬暟閲�
- public final int group;
-
- // 鍋忕Щ閲廩default:0]
- public final int offset;
-
- // 璐ф灦瀹炰緥鑺傜偣闆嗗悎
- public List<List<Integer>> nodes;
-
- public Shelves(int size, int group) {
- this(size, group, 0);
- }
-
- /**
- * @param size 璐ф灦鍗曟帓鎬绘暟
- * @param group 璐ф灦缁勬暟閲�
- * @param offset 搴忓垪鍙峰亸绉婚噺
- */
- public Shelves(int size, int group, int offset) {
- this.size = size;
- this.group = group;
- this.offset = offset;
- init();
- }
-
- /**
- * 鍒濆鍖栨柟娉曘�愮鏈夈��
- */
- private void init(){
- if (group == 0 || size%group != 0) {
- throw new RuntimeException("shelves init fail!");
- }
- nodes = new ArrayList<>();
- for (int g = 1; g <= this.group; g++){
- int unit = size/group;
- List<Integer> node = new ArrayList<>();
- for (int i = (g-1)*unit+1+offset ; i <= g*unit+offset; i++){
- node.add(i);
- }
- nodes.add(node);
- }
- }
-
- /**
- * 寮�濮嬭绠� =======>>>
- *
- * 璐ф灦鍛戒腑瑙勫垯濡備笅锛�
- * 瀹夎浣嶇疆锛� [1] [2] | [3] [4] -------- [5] [6] | [7] [8]
- * 鍛戒腑椤哄簭锛� 1 -> 5 -> 4 -> 8 -> 2 -> 6 -> 3 -> 7 -> 1 ...
- *
- * 1.璇ヨ鍒欓�備笉闄愬埗璐ф灦鏁伴噺锛屾�绘暟涓庣粍鍒湪鏋勯�犲櫒涓缃�
- * 2.濡傛湁搴忓垪鍙疯捣濮嬮棶棰橈紝鐢ㄥ亸绉婚噺瑙勯伩鍗冲彲
- *
- * @param curSeq 褰撳墠璐ф灦鍙�
- * @return 瑙勫垯鍛戒腑璐ф灦鍙�
- */
- public int start(int curSeq){
- Iterator<List<Integer>> iterator = nodes.iterator();
- while (iterator.hasNext()){
- List<Integer> node = iterator.next();
- if (node.contains(curSeq)) {
- int idx = node.indexOf(curSeq);
- // 鏄惁涓烘湯灏捐揣鏋�
- if (iterator.hasNext()) {
- return iterator.next().get(idx);
- } else {
- List<Integer> first = nodes.get(0);
- int val = first.get(idx);
- int res = size / group + 1 + offset - val;
- // 鍙嶅悜鍛戒腑璐ф灦鏃朵笉鍐嶆槸瀵圭珛涓嬫爣锛堢浉瀵逛簬宸烽亾锛�
- if (res < val) {
- // 杞鎵�鏈夎揣鏋跺悗閲嶆柊寮�濮嬪畾浣�
- if (val - res - offset == 1) {
- return first.get(0);
- }
- res = res + 1;
- }
- return res + offset;
- }
- }
- }
- return -1;
- }
-
- public Integer get(Integer curRow) {
- for (List<Integer> node : nodes){
- if (node.contains(curRow)) {
- return nodes.indexOf(node) + 1;
- }
- }
- throw new CoolException("璐ф帓妫�绱㈢郴缁熸姤閿欙紝 node:" + JSON.toJSONString(nodes) + ", curRow:" + curRow);
- }
-
- public static void main(String[] args) throws InterruptedException {
- Shelves shelves = new Shelves(8,2);
- System.out.println(shelves.nodes.toString());
- int start = 1;
- while (true) {
- System.out.println(start);
- start = shelves.start(start);
- Thread.sleep(500L);
- }
- }
-
-}
-
diff --git a/src/main/java/com/zy/core/MainProcess.java b/src/main/java/com/zy/core/MainProcess.java
index ab04415..4b36c14 100644
--- a/src/main/java/com/zy/core/MainProcess.java
+++ b/src/main/java/com/zy/core/MainProcess.java
@@ -55,6 +55,8 @@
}
}
});
+ thread.setName("MainProcess");
+ thread.setDaemon(true);
thread.start();
}
diff --git a/src/main/java/com/zy/core/ServerBootstrap.java b/src/main/java/com/zy/core/ServerBootstrap.java
index dcf52e6..549e663 100644
--- a/src/main/java/com/zy/core/ServerBootstrap.java
+++ b/src/main/java/com/zy/core/ServerBootstrap.java
@@ -17,7 +17,6 @@
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
-import javax.annotation.PreDestroy;
import java.util.List;
/**
@@ -92,7 +91,10 @@
throw new CoolException("鏈煡鐨勭嚎绋嬪疄鐜�");
}
- new Thread(thread).start();
+ Thread t = new Thread(thread);
+ t.setName("CrnThread-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ t.start();
SlaveConnection.put(SlaveType.Crn, deviceConfig.getDeviceNo(), thread);
}
@@ -107,7 +109,10 @@
throw new CoolException("鏈煡鐨勭嚎绋嬪疄鐜�");
}
- new Thread(thread).start();
+ Thread t = new Thread(thread);
+ t.setName("DevpThread-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ t.start();
SlaveConnection.put(SlaveType.Devp, deviceConfig.getDeviceNo(), thread);
}
@@ -122,40 +127,13 @@
throw new CoolException("鏈煡鐨勭嚎绋嬪疄鐜�");
}
- new Thread(thread).start();
+ Thread t = new Thread(thread);
+ t.setName("RgvThread-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ t.start();
SlaveConnection.put(SlaveType.Rgv, deviceConfig.getDeviceNo(), thread);
}
}
-
-
- @PreDestroy
- public void destroy() {
- try {
- List<DeviceConfig> crnList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
- .eq("device_type", String.valueOf(SlaveType.Crn)));
- for (DeviceConfig deviceConfig : crnList) {
- SlaveConnection.remove(SlaveType.Crn, deviceConfig.getDeviceNo());
- }
- } catch (Exception ignore) {}
- try {
- List<DeviceConfig> devpList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
- .eq("device_type", String.valueOf(SlaveType.Devp)));
- for (DeviceConfig deviceConfig : devpList) {
- SlaveConnection.remove(SlaveType.Devp, deviceConfig.getDeviceNo());
- }
- } catch (Exception ignore) {}
- try {
- List<DeviceConfig> rgvList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
- .eq("device_type", String.valueOf(SlaveType.Rgv)));
- for (DeviceConfig deviceConfig : rgvList) {
- SlaveConnection.remove(SlaveType.Rgv, deviceConfig.getDeviceNo());
- }
- } catch (Exception ignore) {}
- try {
- mainProcess.shutDown();
- } catch (Exception ignore) {}
- }
-
}
diff --git a/src/main/java/com/zy/core/network/ZyCrnConnectDriver.java b/src/main/java/com/zy/core/network/ZyCrnConnectDriver.java
index 22a1778..48a8a51 100644
--- a/src/main/java/com/zy/core/network/ZyCrnConnectDriver.java
+++ b/src/main/java/com/zy/core/network/ZyCrnConnectDriver.java
@@ -9,6 +9,10 @@
import com.zy.core.network.fake.ZyCrnFakeConnect;
import com.zy.core.network.real.ZyCrnRealConnect;
import lombok.extern.slf4j.Slf4j;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
/**
* 杩炴帴椹卞姩
@@ -20,29 +24,15 @@
private DeviceConfig deviceConfig;
private ZyCrnConnectApi zyCrnConnectApi;
private volatile boolean closed = false;
- private Thread selfThread;
+ private ScheduledExecutorService executor;
public ZyCrnConnectDriver(DeviceConfig deviceConfig) {
this.deviceConfig = deviceConfig;
}
@Override
- @SuppressWarnings("InfiniteLoopStatement")
public void run() {
- selfThread = Thread.currentThread();
- while (!closed && !Thread.currentThread().isInterrupted()) {
- try {
- if (!connected) {
- connect();
- }
- Thread.sleep(1000);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
+
}
@Override
@@ -61,9 +51,9 @@
@Override
public void close() {
closed = true;
- Thread t = selfThread;
- if (t != null) {
- try { t.interrupt(); } catch (Exception ignore) {}
+ ScheduledExecutorService ex = executor;
+ if (ex != null) {
+ try { ex.shutdownNow(); } catch (Exception ignore) {}
}
if (zyCrnConnectApi != null) {
zyCrnConnectApi.disconnect();
@@ -73,8 +63,27 @@
}
public void start() {
- Thread t = new Thread(this);
- t.start();
+ executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("CrnConnect-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ executor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
+ try {
+ if (!connected) {
+ connect();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }, 0, 1000, TimeUnit.MILLISECONDS);
}
public ZyCrnStatusEntity getStatus() {
diff --git a/src/main/java/com/zy/core/network/ZyRgvConnectDriver.java b/src/main/java/com/zy/core/network/ZyRgvConnectDriver.java
index 9b82eea..2e64f4a 100644
--- a/src/main/java/com/zy/core/network/ZyRgvConnectDriver.java
+++ b/src/main/java/com/zy/core/network/ZyRgvConnectDriver.java
@@ -9,6 +9,10 @@
import com.zy.core.network.fake.ZyRgvFakeConnect;
import com.zy.core.network.real.ZyRgvRealConnect;
import lombok.extern.slf4j.Slf4j;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
@Slf4j
public class ZyRgvConnectDriver implements ThreadHandler {
@@ -16,29 +20,15 @@
private DeviceConfig deviceConfig;
private ZyRgvConnectApi zyRgvConnectApi;
private volatile boolean closed = false;
- private Thread selfThread;
+ private ScheduledExecutorService executor;
public ZyRgvConnectDriver(DeviceConfig deviceConfig) {
this.deviceConfig = deviceConfig;
}
@Override
- @SuppressWarnings("InfiniteLoopStatement")
public void run() {
- selfThread = Thread.currentThread();
- while (!closed && !Thread.currentThread().isInterrupted()) {
- try {
- if (!connected) {
- connect();
- }
- Thread.sleep(1000);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
+
}
@Override
@@ -56,9 +46,9 @@
@Override
public void close() {
closed = true;
- Thread t = selfThread;
- if (t != null) {
- try { t.interrupt(); } catch (Exception ignore) {}
+ ScheduledExecutorService ex = executor;
+ if (ex != null) {
+ try { ex.shutdownNow(); } catch (Exception ignore) {}
}
if (zyRgvConnectApi != null) {
zyRgvConnectApi.disconnect();
@@ -68,8 +58,27 @@
}
public void start() {
- Thread t = new Thread(this);
- t.start();
+ executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("RgvConnect-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ executor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
+ try {
+ if (!connected) {
+ connect();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }, 0, 1000, TimeUnit.MILLISECONDS);
}
public ZyRgvStatusEntity getStatus() {
diff --git a/src/main/java/com/zy/core/network/ZyStationConnectDriver.java b/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
index 13e68d7..61edc2a 100644
--- a/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
+++ b/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
@@ -10,6 +10,10 @@
import com.zy.core.network.fake.ZyStationFakeConnect;
import com.zy.core.network.real.ZyStationRealConnect;
import lombok.extern.slf4j.Slf4j;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
/**
* 杈撻�佺珯杩炴帴椹卞姩
@@ -21,29 +25,15 @@
private DeviceConfig deviceConfig;
private ZyStationConnectApi zyStationConnectApi;
private volatile boolean closed = false;
- private Thread selfThread;
+ private ScheduledExecutorService executor;
public ZyStationConnectDriver(DeviceConfig deviceConfig) {
this.deviceConfig = deviceConfig;
}
@Override
- @SuppressWarnings("InfiniteLoopStatement")
public void run() {
- selfThread = Thread.currentThread();
- while (!closed && !Thread.currentThread().isInterrupted()) {
- try {
- if (!connected) {
- connect();
- }
- Thread.sleep(1000);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
+
}
@Override
@@ -62,9 +52,9 @@
@Override
public void close() {
closed = true;
- Thread t = selfThread;
- if (t != null) {
- try { t.interrupt(); } catch (Exception ignore) {}
+ ScheduledExecutorService ex = executor;
+ if (ex != null) {
+ try { ex.shutdownNow(); } catch (Exception ignore) {}
}
if (zyStationConnectApi != null) {
zyStationConnectApi.disconnect();
@@ -74,8 +64,28 @@
}
public void start() {
- Thread t = new Thread(this);
- t.start();
+ executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("DevpConnect-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+
+ executor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
+ try {
+ if (!connected) {
+ connect();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }, 0, 1000, TimeUnit.MILLISECONDS);
}
public List<ZyStationStatusEntity> getStatus() {
diff --git a/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java b/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java
index e5c2d77..295a818 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyRgvThread.java
@@ -30,6 +30,10 @@
import java.text.MessageFormat;
import java.util.Date;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
@Data
@Slf4j
@@ -41,7 +45,8 @@
private RgvProtocol rgvProtocol;
private int deviceLogCollectTime = 200;
private volatile boolean closed = false;
- private Thread mainThread;
+ private ScheduledExecutorService readExecutor;
+ private ScheduledExecutorService processExecutor;
public ZyRgvThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
this.deviceConfig = deviceConfig;
@@ -53,33 +58,53 @@
public void run() {
connect();
initRgv();
- mainThread = Thread.currentThread();
- while (!closed && !Thread.currentThread().isInterrupted()) {
+ readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("RgvReader-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ readExecutor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
try {
deviceLogCollectTime = Utils.getDeviceLogCollectTime();
+ readStatus();
+ } catch (Exception e) {
+ log.error("RgvThread Fail", e);
+ }
+ }, 0, 200, TimeUnit.MILLISECONDS);
+
+ processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("RgvWriter-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ processExecutor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
+ try {
int step = 1;
Task task = MessageQueue.poll(SlaveType.Rgv, deviceConfig.getDeviceNo());
if (task != null) {
step = task.getStep();
}
- switch (step) {
- case 1:
- readStatus();
- break;
- case 2:
- sendCommand((RgvCommand) task.getData());
- break;
- default:
- break;
+ if (step == 2 && task != null) {
+ sendCommand((RgvCommand) task.getData());
}
- Thread.sleep(200);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
} catch (Exception e) {
e.printStackTrace();
}
- }
+ }, 0, 200, TimeUnit.MILLISECONDS);
}
private void initRgv() {
@@ -158,13 +183,17 @@
@Override
public void close() {
closed = true;
- Thread t = mainThread;
- if (t != null) {
- try { t.interrupt(); } catch (Exception ignore) {}
- }
if (zyRgvConnectDriver != null) {
zyRgvConnectDriver.close();
}
+ ScheduledExecutorService ex = readExecutor;
+ if (ex != null) {
+ try { ex.shutdownNow(); } catch (Exception ignore) {}
+ }
+ ScheduledExecutorService px = processExecutor;
+ if (px != null) {
+ try { px.shutdownNow(); } catch (Exception ignore) {}
+ }
}
@Override
diff --git a/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java b/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
index d8decf5..a29d75a 100644
--- a/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZySiemensCrnThread.java
@@ -12,7 +12,6 @@
import com.zy.asrs.service.BasCrnpOptService;
import com.zy.asrs.utils.Utils;
import com.zy.common.utils.RedisUtil;
-import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.CrnTaskModeType;
@@ -31,6 +30,10 @@
import java.text.MessageFormat;
import java.util.Date;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
/**
* 鍫嗗灈鏈虹嚎绋�
@@ -46,7 +49,8 @@
private int deviceLogCollectTime = 200;
private boolean resetFlag = false;
private volatile boolean closed = false;
- private Thread mainThread;
+ private ScheduledExecutorService readExecutor;
+ private ScheduledExecutorService processExecutor;
public ZySiemensCrnThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
this.deviceConfig = deviceConfig;
@@ -58,33 +62,53 @@
public void run() {
this.connect();
this.initCrn();
- mainThread = Thread.currentThread();
- while (!closed && !Thread.currentThread().isInterrupted()) {
+ readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("CrnReader-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ readExecutor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
try {
deviceLogCollectTime = Utils.getDeviceLogCollectTime();
+ readStatus();
+ } catch (Exception e) {
+ log.error("CrnThread Fail", e);
+ }
+ }, 0, 200, TimeUnit.MILLISECONDS);
+
+ processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("CrnWriter-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ processExecutor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
+ try {
int step = 1;
Task task = MessageQueue.poll(SlaveType.Crn, deviceConfig.getDeviceNo());
if (task != null) {
step = task.getStep();
}
- switch (step) {
- case 1:
- readStatus();
- break;
- case 2:
- sendCommand((CrnCommand) task.getData());
- break;
- default:
- break;
+ if (step == 2 && task != null) {
+ sendCommand((CrnCommand) task.getData());
}
- Thread.sleep(200);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
} catch (Exception e) {
e.printStackTrace();
}
- }
+ }, 0, 200, TimeUnit.MILLISECONDS);
}
/**
@@ -217,9 +241,13 @@
@Override
public void close() {
closed = true;
- Thread t = mainThread;
- if (t != null) {
- try { t.interrupt(); } catch (Exception ignore) {}
+ ScheduledExecutorService ex = readExecutor;
+ if (ex != null) {
+ try { ex.shutdownNow(); } catch (Exception ignore) {}
+ }
+ ScheduledExecutorService px = processExecutor;
+ if (px != null) {
+ try { px.shutdownNow(); } catch (Exception ignore) {}
}
if (zyCrnConnectDriver != null) {
zyCrnConnectDriver.close();
diff --git a/src/main/java/com/zy/core/thread/impl/ZyStationThread.java b/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
index 6eb4b94..246f081 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationThread.java
@@ -28,6 +28,10 @@
import java.text.MessageFormat;
import java.util.*;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@@ -46,8 +50,8 @@
private int deviceLogCollectTime = 200;
private long deviceDataLogTime = System.currentTimeMillis();
private volatile boolean closed = false;
- private Thread mainThread;
- private Thread readThread;
+ private ScheduledExecutorService readExecutor;
+ private ScheduledExecutorService processExecutor;
public ZyStationThread(DeviceConfig deviceConfig, RedisUtil redisUtil) {
this.deviceConfig = deviceConfig;
@@ -59,45 +63,54 @@
public void run() {
this.connect();
deviceLogCollectTime = Utils.getDeviceLogCollectTime();
- mainThread = Thread.currentThread();
- readThread = new Thread(() -> {
- while (!closed && !Thread.currentThread().isInterrupted()) {
- try {
- readStatus();
- Thread.sleep(200);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
- } catch (Exception e) {
- log.error("StationThread Fail", e);
- }
+ readExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("DevpReader-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
}
});
- readThread.start();
+ readExecutor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
+ try {
+ deviceLogCollectTime = Utils.getDeviceLogCollectTime();
+ readStatus();
+ } catch (Exception e) {
+ log.error("StationThread Fail", e);
+ }
+ }, 0, 200, TimeUnit.MILLISECONDS);
- while (!closed && !Thread.currentThread().isInterrupted()) {
+ processExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread t = new Thread(r);
+ t.setName("DevpWriter-" + deviceConfig.getDeviceNo());
+ t.setDaemon(true);
+ return t;
+ }
+ });
+ processExecutor.scheduleAtFixedRate(() -> {
+ if (closed || Thread.currentThread().isInterrupted()) {
+ return;
+ }
try {
int step = 1;
Task task = MessageQueue.poll(SlaveType.Devp, deviceConfig.getDeviceNo());
if (task != null) {
step = task.getStep();
}
- switch (step) {
- case 2:
- sendCommand((StationCommand) task.getData());
- break;
- default:
- break;
+ if (step == 2 && task != null) {
+ sendCommand((StationCommand) task.getData());
}
- Thread.sleep(200);
- } catch (InterruptedException ie) {
- Thread.currentThread().interrupt();
- break;
} catch (Exception e) {
e.printStackTrace();
}
- }
+ }, 0, 200, TimeUnit.MILLISECONDS);
}
private void readStatus() {
@@ -177,13 +190,13 @@
@Override
public void close() {
closed = true;
- Thread t = mainThread;
- if (t != null) {
- try { t.interrupt(); } catch (Exception ignore) {}
+ ScheduledExecutorService ex = readExecutor;
+ if (ex != null) {
+ try { ex.shutdownNow(); } catch (Exception ignore) {}
}
- Thread rt = readThread;
- if (rt != null) {
- try { rt.interrupt(); } catch (Exception ignore) {}
+ ScheduledExecutorService px = processExecutor;
+ if (px != null) {
+ try { px.shutdownNow(); } catch (Exception ignore) {}
}
if (zyStationConnectDriver != null) {
zyStationConnectDriver.close();
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 6824914..75485dd 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -2,6 +2,7 @@
port: 9090
servlet:
context-path: /@pom.build.finalName@
+ shutdown: graceful
spring:
application:
@@ -25,6 +26,15 @@
scheduling:
pool:
size: 3
+ shutdown:
+ await-termination: true
+ await-termination-period: 30s
+ execution:
+ shutdown:
+ await-termination: true
+ await-termination-period: 30s
+ lifecycle:
+ timeout-per-shutdown-phase: 20s
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
@@ -76,4 +86,4 @@
model: deepseek-ai/DeepSeek-V3.2
# base-url: http://34.2.134.223:3000/v1
# api-key: sk-WabrmtOezCFwVo7XvVOrO3QkmfcKG7T7jy0BaVnmQTWm5GXh
-# model: gemini-3-pro-preview
\ No newline at end of file
+# model: gemini-3-pro-preview
--
Gitblit v1.9.1