Skip to content

Commit

Permalink
调整JsqlParser解析线程池.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Feb 21, 2025
1 parent 301879e commit 38f26a2
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,29 @@
package com.baomidou.mybatisplus.extension.parser;

import com.baomidou.mybatisplus.extension.parser.cache.JsqlParseCache;
import com.baomidou.mybatisplus.jsqlparser.JsqlParserThreadPool;
import lombok.Setter;
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;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @author miemie
* @since 2023-08-05
*/
public class JsqlParserGlobal {

private static final Log LOG = LogFactory.getLog(JsqlParserGlobal.class);

/**
* 默认线程数大小
*
* @since 3.5.6
* @deprecated {@link JsqlParserThreadPool#DEFAULT_THREAD_SIZE}
*/
@Deprecated
public static final int DEFAULT_THREAD_SIZE = (Runtime.getRuntime().availableProcessors() + 1) / 2;

/**
Expand All @@ -50,34 +47,54 @@ public class JsqlParserGlobal {
*
* @see java.util.concurrent.ThreadPoolExecutor
* @since 3.5.6
* @deprecated 3.5.10.2 后面不再公开此属性,请使用{@link #setExecutorService(ExecutorService)}} 或 {@link #setExecutorService(ExecutorService, boolean)}
*/
public static ExecutorService executorService = new ThreadPoolExecutor(DEFAULT_THREAD_SIZE, DEFAULT_THREAD_SIZE, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), r -> {
Thread thread = new Thread(r);
thread.setName("mybatis-plus-jsqlParser-" + thread.getId());
thread.setDaemon(true);
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"));
}
@Deprecated
public static ExecutorService executorService;

@Setter
private static JsqlParserFunction<String, Statement> parserSingleFunc = sql -> CCJSqlParserUtil.parse(sql, executorService, null);
private static JsqlParserFunction<String, Statement> parserSingleFunc = sql -> CCJSqlParserUtil.parse(sql, getExecutorService(), null);

@Setter
private static JsqlParserFunction<String, Statements> parserMultiFunc = sql -> CCJSqlParserUtil.parseStatements(sql, executorService, null);
private static JsqlParserFunction<String, Statements> parserMultiFunc = sql -> CCJSqlParserUtil.parseStatements(sql, getExecutorService(), null);

@Setter
private static JsqlParseCache jsqlParseCache;

/**
* 设置解析线程池
*
* @param executorService 线程池 (自行控制线程池关闭)
* @since 3.5.10.2
*/
public static void setExecutorService(ExecutorService executorService) {
JsqlParserGlobal.executorService = executorService;
}

/**
* 设置解析线程池
*
* @param executorService 线程池 (自行控制线程池关闭)
* @param addShutdownHook 是否注册退出关闭钩子
* @since 3.5.10.2
*/
public static void setExecutorService(ExecutorService executorService, boolean addShutdownHook) {
JsqlParserGlobal.executorService = executorService;
if (addShutdownHook) {
JsqlParserThreadPool.addShutdownHook(executorService);
}
}

/**
* 获取解析线程池(如果未自定义则返回默认的解析线程池)
*
* @return 解析线程池
* @since 3.5.10.2
*/
public static ExecutorService getExecutorService() {
return JsqlParserGlobal.executorService == null ? JsqlParserThreadPool.getDefaultThreadPoolExecutor() : JsqlParserGlobal.executorService;
}

public static Statement parse(String sql) throws JSQLParserException {
if (jsqlParseCache == null) {
return parserSingleFunc.apply(sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,29 @@
package com.baomidou.mybatisplus.extension.parser;

import com.baomidou.mybatisplus.extension.parser.cache.JsqlParseCache;
import com.baomidou.mybatisplus.jsqlparser.JsqlParserThreadPool;
import lombok.Setter;
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;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @author miemie
* @since 2023-08-05
*/
public class JsqlParserGlobal {

private static final Log LOG = LogFactory.getLog(JsqlParserGlobal.class);

/**
* 默认线程数大小
*
* @since 3.5.6
* @deprecated {@link JsqlParserThreadPool#DEFAULT_THREAD_SIZE}
*/
@Deprecated
public static final int DEFAULT_THREAD_SIZE = (Runtime.getRuntime().availableProcessors() + 1) / 2;

/**
Expand All @@ -50,34 +47,54 @@ public class JsqlParserGlobal {
*
* @see java.util.concurrent.ThreadPoolExecutor
* @since 3.5.6
* @deprecated 3.5.10.2 后面不再公开此属性,请使用{@link #setExecutorService(ExecutorService)}} 或 {@link #setExecutorService(ExecutorService, boolean)}
*/
public static ExecutorService executorService = new ThreadPoolExecutor(DEFAULT_THREAD_SIZE, DEFAULT_THREAD_SIZE, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), r -> {
Thread thread = new Thread(r);
thread.setName("mybatis-plus-jsqlParser-" + thread.getId());
thread.setDaemon(true);
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"));
}
@Deprecated
public static ExecutorService executorService;

@Setter
private static JsqlParserFunction<String, Statement> parserSingleFunc = sql -> CCJSqlParserUtil.parse(sql, executorService, null);
private static JsqlParserFunction<String, Statement> parserSingleFunc = sql -> CCJSqlParserUtil.parse(sql, getExecutorService(), null);

@Setter
private static JsqlParserFunction<String, Statements> parserMultiFunc = sql -> CCJSqlParserUtil.parseStatements(sql, executorService, null);
private static JsqlParserFunction<String, Statements> parserMultiFunc = sql -> CCJSqlParserUtil.parseStatements(sql, getExecutorService(), null);

@Setter
private static JsqlParseCache jsqlParseCache;

/**
* 设置解析线程池
*
* @param executorService 线程池 (自行控制线程池关闭)
* @since 3.5.10.2
*/
public static void setExecutorService(ExecutorService executorService) {
JsqlParserGlobal.executorService = executorService;
}

/**
* 设置解析线程池
*
* @param executorService 线程池 (自行控制线程池关闭)
* @param addShutdownHook 是否注册退出关闭钩子
* @since 3.5.10.2
*/
public static void setExecutorService(ExecutorService executorService, boolean addShutdownHook) {
JsqlParserGlobal.executorService = executorService;
if (addShutdownHook) {
JsqlParserThreadPool.addShutdownHook(executorService);
}
}

/**
* 获取解析线程池(如果未自定义则返回默认的解析线程池)
*
* @return 解析线程池
* @since 3.5.10.2
*/
public static ExecutorService getExecutorService() {
return JsqlParserGlobal.executorService == null ? JsqlParserThreadPool.getDefaultThreadPoolExecutor() : JsqlParserGlobal.executorService;
}

public static Statement parse(String sql) throws JSQLParserException {
if (jsqlParseCache == null) {
return parserSingleFunc.apply(sql);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2011-2025, baomidou ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.mybatisplus.jsqlparser;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* JsqlParser解析线程池
* <p>当没指定解析线程池时,默认使用一个固定长度的线程作为解析线程池.</p>
*
* @author nieqiurong
* @since 3.5.10.2
*/
public class JsqlParserThreadPool {

/**
* 默认线程数大小
*/
public static final int DEFAULT_THREAD_SIZE = (Runtime.getRuntime().availableProcessors() + 1) / 2;

/**
* 获取默认解析线程池(固定大小)
*
* @return 解析线程池
*/
public static ExecutorService getDefaultThreadPoolExecutor() {
return DefaultJsqlParserFixedThreadPool.INSTANCE.getDefaultThreadPoolExecutor();
}

/**
* 注册Jvm退出钩子
*
* @param executorService 线程池
*/
public static void addShutdownHook(ExecutorService executorService) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (!executorService.isShutdown()) {
executorService.shutdown();
}
}, "mybatis-plus-jsqlParser-shutdown-hook"));
}

private static class DefaultJsqlParserFixedThreadPool {

private static final DefaultJsqlParserFixedThreadPool INSTANCE = new DefaultJsqlParserFixedThreadPool();

public static ExecutorService executorService = new ThreadPoolExecutor(DEFAULT_THREAD_SIZE, DEFAULT_THREAD_SIZE, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), r -> {
Thread thread = new Thread(r);
thread.setName("mybatis-plus-jsqlParser-" + thread.getId());
thread.setDaemon(true);
return thread;
});

static {
addShutdownHook(executorService);
}

/**
* 默认解析线程池(固定大小,默认大小{@link #DEFAULT_THREAD_SIZE})
*
* @return 线程池
*/
public ExecutorService getDefaultThreadPoolExecutor() {
return executorService;
}

}

}
Loading

0 comments on commit 38f26a2

Please sign in to comment.