Commit 8ca9bebfd1c8cdac3f64842df1d7c640ce68fa1c
1 parent
1a3c6756
【代码优化】IoT: 更新 HttpPlugin,重构线程池初始化逻辑以确保线程池活跃
Showing
1 changed file
with
9 additions
and
4 deletions
yudao-module-iot/yudao-module-iot-plugin/yudao-module-iot-http-plugin/src/main/java/cn/iocoder/yudao/module/iot/plugin/HttpPlugin.java
| @@ -19,12 +19,12 @@ public class HttpPlugin extends Plugin { | @@ -19,12 +19,12 @@ public class HttpPlugin extends Plugin { | ||
| 19 | 19 | ||
| 20 | private static final int PORT = 8092; | 20 | private static final int PORT = 8092; |
| 21 | 21 | ||
| 22 | - private final ExecutorService executorService; | 22 | + private ExecutorService executorService; |
| 23 | private DeviceDataApi deviceDataApi; | 23 | private DeviceDataApi deviceDataApi; |
| 24 | 24 | ||
| 25 | public HttpPlugin(PluginWrapper wrapper) { | 25 | public HttpPlugin(PluginWrapper wrapper) { |
| 26 | super(wrapper); | 26 | super(wrapper); |
| 27 | - // 创建单线程池 | 27 | + // 初始化线程池 |
| 28 | this.executorService = Executors.newSingleThreadExecutor(); | 28 | this.executorService = Executors.newSingleThreadExecutor(); |
| 29 | } | 29 | } |
| 30 | 30 | ||
| @@ -32,6 +32,11 @@ public class HttpPlugin extends Plugin { | @@ -32,6 +32,11 @@ public class HttpPlugin extends Plugin { | ||
| 32 | public void start() { | 32 | public void start() { |
| 33 | log.info("HttpPlugin.start()"); | 33 | log.info("HttpPlugin.start()"); |
| 34 | 34 | ||
| 35 | + // 重新初始化线程池,确保它是活跃的 | ||
| 36 | + if (executorService.isShutdown() || executorService.isTerminated()) { | ||
| 37 | + executorService = Executors.newSingleThreadExecutor(); | ||
| 38 | + } | ||
| 39 | + | ||
| 35 | // 从 ServiceRegistry 中获取主程序暴露的 DeviceDataApi 接口实例 | 40 | // 从 ServiceRegistry 中获取主程序暴露的 DeviceDataApi 接口实例 |
| 36 | deviceDataApi = ServiceRegistry.getService(DeviceDataApi.class); | 41 | deviceDataApi = ServiceRegistry.getService(DeviceDataApi.class); |
| 37 | if (deviceDataApi == null) { | 42 | if (deviceDataApi == null) { |
| @@ -79,11 +84,11 @@ public class HttpPlugin extends Plugin { | @@ -79,11 +84,11 @@ public class HttpPlugin extends Plugin { | ||
| 79 | future.channel().closeFuture().sync(); | 84 | future.channel().closeFuture().sync(); |
| 80 | } catch (InterruptedException e) { | 85 | } catch (InterruptedException e) { |
| 81 | Thread.currentThread().interrupt(); | 86 | Thread.currentThread().interrupt(); |
| 82 | - log.error("HTTP 服务启动中断", e); | 87 | + log.warn("HTTP 服务启动被中断", e); |
| 83 | } finally { | 88 | } finally { |
| 84 | bossGroup.shutdownGracefully(); | 89 | bossGroup.shutdownGracefully(); |
| 85 | workerGroup.shutdownGracefully(); | 90 | workerGroup.shutdownGracefully(); |
| 86 | } | 91 | } |
| 87 | } | 92 | } |
| 88 | 93 | ||
| 89 | -} | 94 | -} |
| 95 | +} | ||
| 90 | \ No newline at end of file | 96 | \ No newline at end of file |