线程池统一管理

master
RuoYi 6 years ago committed by Limy
parent 60ba44bf88
commit 683572d57f

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

@ -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;

@ -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);
}

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

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

@ -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失败

Loading…
Cancel
Save