From 1216649c4562c775bfad7fe5507264a984341d58 Mon Sep 17 00:00:00 2001 From: limliu <123654xuni@zhenergy.com.cn> Date: Sat, 4 Feb 2023 18:29:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81=E5=A2=9E=E5=8A=A0transmittable-t?= =?UTF-8?q?hread-local=20jar=E5=8C=85=202=E3=80=81=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E5=B9=B6=E8=A1=8C=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trunk/sqltoy-orm/pom.xml | 6 ++++++ .../main/java/org/sagacity/sqltoy/SqlExecuteStat.java | 4 ++-- .../main/java/org/sagacity/sqltoy/SqlToyContext.java | 3 ++- .../org/sagacity/sqltoy/dialect/DialectFactory.java | 2 +- .../sqltoy/configure/SqltoyAutoConfiguration.java | 10 +++++++++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/trunk/sqltoy-orm/pom.xml b/trunk/sqltoy-orm/pom.xml index 578f634a8..93c3b3dc0 100644 --- a/trunk/sqltoy-orm/pom.xml +++ b/trunk/sqltoy-orm/pom.xml @@ -27,6 +27,7 @@ 1.18.24 3.0.4 1.2.15 + 2.14.2 @@ -275,6 +276,11 @@ provided true + + com.alibaba + transmittable-thread-local + ${transmittable-thread-local} + diff --git a/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlExecuteStat.java b/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlExecuteStat.java index d9e59c830..606bdf604 100644 --- a/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlExecuteStat.java +++ b/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlExecuteStat.java @@ -2,6 +2,7 @@ package org.sagacity.sqltoy; import static java.lang.System.out; +import com.alibaba.ttl.TransmittableThreadLocal; import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalDateTime; @@ -11,7 +12,6 @@ import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; - import org.sagacity.sqltoy.config.model.SqlExecuteLog; import org.sagacity.sqltoy.config.model.SqlExecuteTrace; import org.sagacity.sqltoy.model.OverTimeSql; @@ -50,7 +50,7 @@ public class SqlExecuteStat { private final static Pattern ARG_PATTERN = Pattern.compile("\\W\\?\\W"); // 通过ThreadLocal 来保存线程数据 - private static ThreadLocal threadLocal = new InheritableThreadLocal(); + private static ThreadLocal threadLocal = new TransmittableThreadLocal(); // sql执行超时处理器 public static OverTimeSqlHandler overTimeSqlHandler; diff --git a/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlToyContext.java b/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlToyContext.java index 4c50e7efc..2ab13f99c 100644 --- a/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlToyContext.java +++ b/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/SqlToyContext.java @@ -1,5 +1,6 @@ package org.sagacity.sqltoy; +import com.alibaba.ttl.threadpool.TtlExecutors; import java.sql.Connection; import java.util.Arrays; import java.util.HashMap; @@ -354,7 +355,7 @@ public class SqlToyContext { /** * sqltoy的线程池 */ - private Executor taskExecutor = ForkJoinPool.commonPool(); + private Executor taskExecutor = TtlExecutors.getTtlExecutor(ForkJoinPool.commonPool()); /** * @todo 初始化 diff --git a/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/dialect/DialectFactory.java b/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/dialect/DialectFactory.java index 219fac741..a7ce1ab35 100644 --- a/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/dialect/DialectFactory.java +++ b/trunk/sqltoy-orm/src/main/java/org/sagacity/sqltoy/dialect/DialectFactory.java @@ -929,7 +929,7 @@ public class DialectFactory { queryResult.setPageSize(pageSize); Executor taskExecutor = sqlToyContext.getTaskExecutor(); try { - SqlExecuteStat.debug("开始并行查询count总记录数和单页记录数据!", null); + //SqlExecuteStat.debug("开始并行查询count总记录数和单页记录数据!", null); // 查询总记录数量 CompletableFuture countCompletableFuture = CompletableFuture.runAsync(() -> { try { diff --git a/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java b/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java index e65716fb0..2ee30365d 100644 --- a/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java +++ b/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java @@ -2,9 +2,11 @@ package org.sagacity.sqltoy.configure; import static java.lang.System.err; +import com.alibaba.ttl.threadpool.TtlExecutors; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executor; import org.sagacity.sqltoy.SqlToyContext; import org.sagacity.sqltoy.config.model.ElasticEndpoint; import org.sagacity.sqltoy.dao.SqlToyLazyDao; @@ -26,6 +28,7 @@ import org.sagacity.sqltoy.service.impl.SqlToyCRUDServiceImpl; import org.sagacity.sqltoy.translate.cache.TranslateCacheManager; import org.sagacity.sqltoy.utils.StringUtil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -82,11 +85,16 @@ public class SqltoyAutoConfiguration { return pool; } + @Bean(name = "ttlSqltoyOrmTaskExecutor") + public Executor ttlSqltoyOrmTaskExecutor(@Qualifier("sqltoyOrmTaskExecutor") ThreadPoolTaskExecutor taskExecutor) { + return TtlExecutors.getTtlExecutor(taskExecutor); + } + // 构建sqltoy上下文,并指定初始化方法和销毁方法 @Bean(name = "sqlToyContext", initMethod = "initialize", destroyMethod = "destroy") @ConditionalOnMissingBean SqlToyContext sqlToyContext( - @Value("${spring.sqltoy.taskExecutor.targetPoolName:sqltoyOrmTaskExecutor}") String taskExecutorName) + @Value("${spring.sqltoy.taskExecutor.targetPoolName:ttlSqltoyOrmTaskExecutor}") String taskExecutorName) throws Exception { // 用辅助配置来校验是否配置错误 if (StringUtil.isBlank(properties.getSqlResourcesDir()) && StringUtil.isNotBlank(sqlResourcesDir)) { -- Gitee From 65951cdcc57d4cfeeb7eb0da03ec7692e9cc100b Mon Sep 17 00:00:00 2001 From: limliu <123654xuni@zhenergy.com.cn> Date: Sat, 4 Feb 2023 18:37:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=E3=80=81=E5=A2=9E=E5=8A=A0transmittable-t?= =?UTF-8?q?hread-local=20jar=E5=8C=85=202=E3=80=81=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E5=B9=B6=E8=A1=8C=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java | 1 + 1 file changed, 1 insertion(+) diff --git a/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java b/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java index 2ee30365d..f173c7e39 100644 --- a/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java +++ b/trunk/sqltoy-spring-boot-starter/src/main/java/org/sagacity/sqltoy/configure/SqltoyAutoConfiguration.java @@ -85,6 +85,7 @@ public class SqltoyAutoConfiguration { return pool; } + @ConditionalOnExpression("#{''.equals(environment.getProperty('spring.sqltoy.taskExecutor.targetPoolName', ''))}") @Bean(name = "ttlSqltoyOrmTaskExecutor") public Executor ttlSqltoyOrmTaskExecutor(@Qualifier("sqltoyOrmTaskExecutor") ThreadPoolTaskExecutor taskExecutor) { return TtlExecutors.getTtlExecutor(taskExecutor); -- Gitee