RuoYi преди 6 години
родител
ревизия
d5a89621a9

+ 3 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/config/GenConfig.java

@@ -14,10 +14,13 @@ public class GenConfig
 {
     /** 作者 */
     public static String author;
+
     /** 生成包路径 */
     public static String packageName;
+
     /** 自动去除表前缀,默认是true */
     public static String autoRemovePre;
+
     /** 表前缀(类名不会包含表前缀) */
     public static String tablePrefix;
 

+ 53 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java

@@ -0,0 +1,53 @@
+package com.ruoyi.framework.config;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+/**
+ * 线程池配置
+ *
+ * @author ruoyi
+ **/
+@Configuration
+public class ThreadPoolConfig
+{
+    // 核心线程池大小
+    private int corePoolSize = 50;
+
+    // 最大可创建的线程数
+    private int maxPoolSize = 200;
+
+    // 队列最大长度
+    private int queueCapacity = 1000;
+
+    // 线程池维护线程所允许的空闲时间
+    private int keepAliveSeconds = 300;
+
+    @Bean(name = "threadPoolTaskExecutor")
+    public ThreadPoolTaskExecutor threadPoolTaskExecutor()
+    {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setMaxPoolSize(maxPoolSize);
+        executor.setCorePoolSize(corePoolSize);
+        executor.setQueueCapacity(queueCapacity);
+        executor.setKeepAliveSeconds(keepAliveSeconds);
+        // 线程池对拒绝任务(无线程可用)的处理策略
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        return executor;
+    }
+
+    /**
+     * 执行周期性或定时任务
+     */
+    @Bean(name = "scheduledExecutorService")
+    protected ScheduledExecutorService scheduledExecutorService()
+    {
+        return new ScheduledThreadPoolExecutor(corePoolSize,
+                new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build());
+    }
+}

+ 3 - 2
ruoyi-framework/src/main/java/com/ruoyi/framework/manager/AsyncManager.java

@@ -1,9 +1,10 @@
 package com.ruoyi.framework.manager;
 
 import java.util.TimerTask;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import com.ruoyi.common.utils.Threads;
+import com.ruoyi.framework.util.SpringUtils;
 
 /**
  * 异步任务管理器
@@ -20,7 +21,7 @@ public class AsyncManager
     /**
      * 异步操作任务调度线程池
      */
-    private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
+    private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
 
     /**
      * 单例模式

+ 5 - 2
ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/web/session/SpringSessionValidationScheduler.java

@@ -1,6 +1,5 @@
 package com.ruoyi.framework.shiro.web.session;
 
-import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import org.apache.shiro.session.mgt.DefaultSessionManager;
@@ -8,6 +7,8 @@ import org.apache.shiro.session.mgt.SessionValidationScheduler;
 import org.apache.shiro.session.mgt.ValidatingSessionManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import com.ruoyi.common.utils.Threads;
 
 /**
@@ -24,7 +25,9 @@ public class SpringSessionValidationScheduler implements SessionValidationSchedu
     /**
      * 定时器,用于处理超时的挂起请求,也用于连接断开时的重连。
      */
-    private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+    @Autowired
+    @Qualifier("scheduledExecutorService")
+    private ScheduledExecutorService executorService;
 
     private volatile boolean enabled = false;
 

+ 4 - 6
ruoyi-generator/src/main/java/com/ruoyi/generator/util/GenUtils.java

@@ -105,13 +105,11 @@ public class GenUtils
      */
     public static String tableToJava(String tableName)
     {
-        if (Constants.AUTO_REOMVE_PRE.equals(Global.getAutoRemovePre()))
+        String autoRemovePre = Global.getAutoRemovePre();
+        String tablePrefix = Global.getTablePrefix();
+        if (Constants.AUTO_REOMVE_PRE.equals(autoRemovePre) && StringUtils.isNotEmpty(tablePrefix))
         {
-            tableName = tableName.substring(tableName.indexOf("_") + 1);
-        }
-        if (StringUtils.isNotEmpty(Global.getTablePrefix()))
-        {
-            tableName = tableName.replace(Global.getTablePrefix(), "");
+            tableName = tableName.replaceFirst(tablePrefix, "");
         }
         return StringUtils.convertToCamelCase(tableName);
     }

+ 0 - 1
ruoyi-quartz/src/main/java/com/ruoyi/quartz/mapper/SysJobLogMapper.java

@@ -1,7 +1,6 @@
 package com.ruoyi.quartz.mapper;
 
 import com.ruoyi.quartz.domain.SysJobLog;
-
 import java.util.List;
 
 /**

+ 0 - 1
ruoyi-quartz/src/main/java/com/ruoyi/quartz/mapper/SysJobMapper.java

@@ -1,7 +1,6 @@
 package com.ruoyi.quartz.mapper;
 
 import com.ruoyi.quartz.domain.SysJob;
-
 import java.util.List;
 
 /**

+ 3 - 4
ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/ScheduleJob.java

@@ -1,14 +1,13 @@
 package com.ruoyi.quartz.util;
 
 import java.util.Date;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import org.quartz.DisallowConcurrentExecution;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.scheduling.quartz.QuartzJobBean;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.constant.ScheduleConstants;
@@ -29,7 +28,7 @@ public class ScheduleJob extends QuartzJobBean
 {
     private static final Logger log = LoggerFactory.getLogger(ScheduleJob.class);
 
-    private ExecutorService service = Executors.newSingleThreadExecutor();
+    private ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) SpringContextUtil.getBean("publicThreadPool");
 
     private final static ISysJobLogService jobLogService = (ISysJobLogService) SpringContextUtil.getBean("sysJobLogServiceImpl");
 
@@ -53,7 +52,7 @@ public class ScheduleJob extends QuartzJobBean
             // 执行任务
             log.info("任务开始执行 - 名称:{} 方法:{}", job.getJobName(), job.getMethodName());
             ScheduleRunnable task = new ScheduleRunnable(job.getJobName(), job.getMethodName(), job.getMethodParams());
-            Future<?> future = service.submit(task);
+            Future<?> future = executor.submit(task);
             future.get();
             long times = System.currentTimeMillis() - startTime;
             // 任务状态 0:成功 1:失败