| package com.dy.rtuMw; | 
|   | 
| import com.dy.common.mw.protocol.Command; | 
| import com.dy.common.mw.protocol.CommandType; | 
| import com.dy.rtuMw.server.local.CommandInnerDeaLer; | 
| import com.dy.rtuMw.server.local.localProtocol.CodeLocal; | 
| import lombok.extern.slf4j.Slf4j; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2024/8/6 9:51 | 
|  * @Description 程序(控制台)关闭处理钩子类 | 
|  */ | 
| @Slf4j | 
| public class ServerShutDownHook { | 
|     public static void OnShutDown(){ | 
|         Runtime.getRuntime().addShutdownHook(new Thread(){ | 
|             @Override | 
|             public void run(){ | 
|             try{ | 
|                 // 确保这段代码尽可能快速执行,避免影响JVM的关闭 | 
|                 log.info("程序(控制台)关闭钩子类执行"); | 
|                 Command com1 = new Command() ; | 
|                 com1.id = Command.defaultId ; | 
|                 com1.code = CodeLocal.stopTcpSv ; | 
|                 com1.type = CommandType.innerCommand ; | 
|                 new CommandInnerDeaLer().deal(com1) ; | 
|                 //Thread.sleep(100L);//实测不执行 | 
|                 log.info("关闭程序前,关闭了TCP服务"); | 
|                 Command com2 = new Command() ; | 
|                 com2.id = Command.defaultId ; | 
|                 com2.code = CodeLocal.stopMqttSv ; | 
|                 com2.type = CommandType.innerCommand ; | 
|                 new CommandInnerDeaLer().deal(com2) ; | 
|                 log.info("关闭程序前,关闭了MQTT服务"); | 
|             }catch (Exception e){ | 
|                 log.error("程序(控制台)关闭钩子发生异常", e); | 
|             } | 
|             } | 
|         }); | 
|     } | 
| } |