Browse Source

增加JVM退出钩子处理.

nieqiurong 3 months ago
parent
commit
62dd02e555

+ 15 - 0
mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-4.9/src/main/java/com/baomidou/mybatisplus/extension/parser/JsqlParserGlobal.java

@@ -21,6 +21,8 @@ import net.sf.jsqlparser.JSQLParserException;
 import net.sf.jsqlparser.parser.CCJSqlParserUtil;
 import net.sf.jsqlparser.statement.Statement;
 import net.sf.jsqlparser.statement.Statements;
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -33,6 +35,8 @@ import java.util.concurrent.TimeUnit;
  */
 public class JsqlParserGlobal {
 
+    private static final Log LOG = LogFactory.getLog(JsqlParserGlobal.class);
+
     /**
      * 默认线程数大小
      *
@@ -54,6 +58,17 @@ public class JsqlParserGlobal {
         return thread;
     });
 
+    static {
+        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+            if (!executorService.isShutdown()) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("close jsqlParser thread pool ...");
+                }
+                executorService.shutdown();
+            }
+        }, "mybatis-plus-jsqlParser-shutdown-hook"));
+    }
+
     @Setter
     private static JsqlParserFunction<String, Statement> parserSingleFunc = sql -> CCJSqlParserUtil.parse(sql, executorService, null);
 

+ 15 - 0
mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser-5.0/src/main/java/com/baomidou/mybatisplus/extension/parser/JsqlParserGlobal.java

@@ -21,6 +21,8 @@ import net.sf.jsqlparser.JSQLParserException;
 import net.sf.jsqlparser.parser.CCJSqlParserUtil;
 import net.sf.jsqlparser.statement.Statement;
 import net.sf.jsqlparser.statement.Statements;
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -33,6 +35,8 @@ import java.util.concurrent.TimeUnit;
  */
 public class JsqlParserGlobal {
 
+    private static final Log LOG = LogFactory.getLog(JsqlParserGlobal.class);
+
     /**
      * 默认线程数大小
      *
@@ -54,6 +58,17 @@ public class JsqlParserGlobal {
         return thread;
     });
 
+    static {
+        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+            if (!executorService.isShutdown()) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("close jsqlParser thread pool ...");
+                }
+                executorService.shutdown();
+            }
+        }, "mybatis-plus-jsqlParser-shutdown-hook"));
+    }
+
     @Setter
     private static JsqlParserFunction<String, Statement> parserSingleFunc = sql -> CCJSqlParserUtil.parse(sql, executorService, null);
 

+ 15 - 0
mybatis-plus-jsqlparser-support/mybatis-plus-jsqlparser/src/main/java/com/baomidou/mybatisplus/extension/parser/JsqlParserGlobal.java

@@ -21,6 +21,8 @@ import net.sf.jsqlparser.JSQLParserException;
 import net.sf.jsqlparser.parser.CCJSqlParserUtil;
 import net.sf.jsqlparser.statement.Statement;
 import net.sf.jsqlparser.statement.Statements;
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -33,6 +35,8 @@ import java.util.concurrent.TimeUnit;
  */
 public class JsqlParserGlobal {
 
+    private static final Log LOG = LogFactory.getLog(JsqlParserGlobal.class);
+
     /**
      * 默认线程数大小
      *
@@ -54,6 +58,17 @@ public class JsqlParserGlobal {
         return thread;
     });
 
+    static {
+        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+            if (!executorService.isShutdown()) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("close jsqlParser thread pool ...");
+                }
+                executorService.shutdown();
+            }
+        }, "mybatis-plus-jsqlParser-shutdown-hook"));
+    }
+
     @Setter
     private static JsqlParserFunction<String, Statement> parserSingleFunc = sql -> CCJSqlParserUtil.parse(sql, executorService, null);