mirror of https://gitee.com/y_project/RuoYi.git
线程池统一管理
parent
c6bd84818a
commit
d5a89621a9
|
@ -14,10 +14,13 @@ public class GenConfig
|
||||||
{
|
{
|
||||||
/** 作者 */
|
/** 作者 */
|
||||||
public static String author;
|
public static String author;
|
||||||
|
|
||||||
/** 生成包路径 */
|
/** 生成包路径 */
|
||||||
public static String packageName;
|
public static String packageName;
|
||||||
|
|
||||||
/** 自动去除表前缀,默认是true */
|
/** 自动去除表前缀,默认是true */
|
||||||
public static String autoRemovePre;
|
public static String autoRemovePre;
|
||||||
|
|
||||||
/** 表前缀(类名不会包含表前缀) */
|
/** 表前缀(类名不会包含表前缀) */
|
||||||
public static String tablePrefix;
|
public static String tablePrefix;
|
||||||
|
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
package com.ruoyi.framework.manager;
|
package com.ruoyi.framework.manager;
|
||||||
|
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import com.ruoyi.common.utils.Threads;
|
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");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单例模式
|
* 单例模式
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package com.ruoyi.framework.shiro.web.session;
|
package com.ruoyi.framework.shiro.web.session;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import org.apache.shiro.session.mgt.DefaultSessionManager;
|
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.apache.shiro.session.mgt.ValidatingSessionManager;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import com.ruoyi.common.utils.Threads;
|
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;
|
private volatile boolean enabled = false;
|
||||||
|
|
||||||
|
|
|
@ -105,13 +105,11 @@ public class GenUtils
|
||||||
*/
|
*/
|
||||||
public static String tableToJava(String tableName)
|
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);
|
tableName = tableName.replaceFirst(tablePrefix, "");
|
||||||
}
|
|
||||||
if (StringUtils.isNotEmpty(Global.getTablePrefix()))
|
|
||||||
{
|
|
||||||
tableName = tableName.replace(Global.getTablePrefix(), "");
|
|
||||||
}
|
}
|
||||||
return StringUtils.convertToCamelCase(tableName);
|
return StringUtils.convertToCamelCase(tableName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package com.ruoyi.quartz.mapper;
|
package com.ruoyi.quartz.mapper;
|
||||||
|
|
||||||
import com.ruoyi.quartz.domain.SysJobLog;
|
import com.ruoyi.quartz.domain.SysJobLog;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package com.ruoyi.quartz.mapper;
|
package com.ruoyi.quartz.mapper;
|
||||||
|
|
||||||
import com.ruoyi.quartz.domain.SysJob;
|
import com.ruoyi.quartz.domain.SysJob;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
package com.ruoyi.quartz.util;
|
package com.ruoyi.quartz.util;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import org.quartz.DisallowConcurrentExecution;
|
import org.quartz.DisallowConcurrentExecution;
|
||||||
import org.quartz.JobExecutionContext;
|
import org.quartz.JobExecutionContext;
|
||||||
import org.quartz.JobExecutionException;
|
import org.quartz.JobExecutionException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||||
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.constant.ScheduleConstants;
|
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 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");
|
private final static ISysJobLogService jobLogService = (ISysJobLogService) SpringContextUtil.getBean("sysJobLogServiceImpl");
|
||||||
|
|
||||||
|
@ -53,7 +52,7 @@ public class ScheduleJob extends QuartzJobBean
|
||||||
// 执行任务
|
// 执行任务
|
||||||
log.info("任务开始执行 - 名称:{} 方法:{}", job.getJobName(), job.getMethodName());
|
log.info("任务开始执行 - 名称:{} 方法:{}", job.getJobName(), job.getMethodName());
|
||||||
ScheduleRunnable task = new ScheduleRunnable(job.getJobName(), job.getMethodName(), job.getMethodParams());
|
ScheduleRunnable task = new ScheduleRunnable(job.getJobName(), job.getMethodName(), job.getMethodParams());
|
||||||
Future<?> future = service.submit(task);
|
Future<?> future = executor.submit(task);
|
||||||
future.get();
|
future.get();
|
||||||
long times = System.currentTimeMillis() - startTime;
|
long times = System.currentTimeMillis() - startTime;
|
||||||
// 任务状态 0:成功 1:失败
|
// 任务状态 0:成功 1:失败
|
||||||
|
|
Loading…
Reference in New Issue