线程池代码优化

pull/60/MERGE
RuoYi 2018-12-29 15:05:29 +08:00
parent 383d082194
commit f04c1fed50
5 changed files with 82 additions and 22 deletions

View File

@ -8,9 +8,7 @@
> RuoYi从3.0开始,进行模块拆分,将原先的单应用转变为多模块,如需单应用,请移步 [RuoYi-fast](https://gitee.com/y_project/RuoYi-fast) > RuoYi从3.0开始,进行模块拆分,将原先的单应用转变为多模块,如需单应用,请移步 [RuoYi-fast](https://gitee.com/y_project/RuoYi-fast)
> 推荐使用阿里云部署,通用云产品代金券 [点我领取](https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=brki8iof) > 推荐使用阿里云部署,通用云产品代金券 [点我领取](https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=brki8iof)
> 阿里云双12活动低至2折 [点我购买](https://m.aliyun.com/act/team1212?params=N.DTB4ZxQhX0#/)
## 内置功能 ## 内置功能

View File

@ -0,0 +1,62 @@
package com.ruoyi.common.utils;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 线.
*
* @author ruoyi
*/
public class Threads
{
private static final Logger logger = LoggerFactory.getLogger("sys-user");
/**
* sleep,
*/
public static void sleep(long milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e)
{
return;
}
}
/**
* 线
* 使shutdown, .
* , shutdownNow, workQueuePending,.
* 退.
* shutdown线.
*/
public static void shutdownAndAwaitTermination(ExecutorService pool)
{
if (pool != null && !pool.isShutdown())
{
pool.shutdown();
try
{
if (!pool.awaitTermination(120, TimeUnit.SECONDS))
{
pool.shutdownNow();
if (!pool.awaitTermination(120, TimeUnit.SECONDS))
{
logger.info("Pool did not terminate");
}
}
}
catch (InterruptedException ie)
{
pool.shutdownNow();
Thread.currentThread().interrupt();
}
}
}
}

View File

@ -3,6 +3,7 @@ package com.ruoyi.framework.manager;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.ruoyi.common.utils.Threads;
/** /**
* *
@ -41,9 +42,11 @@ public class AsyncManager
executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS); executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
} }
public void shutdown(long timeout, TimeUnit unit) throws Exception /**
* 线
*/
public void shutdown()
{ {
executor.shutdown(); Threads.shutdownAndAwaitTermination(executor);
executor.awaitTermination(timeout, unit);
} }
} }

View File

@ -6,13 +6,11 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import java.util.concurrent.TimeUnit;
/** /**
* ShutdownManager * 退线
* *
* @Auther: cj * @author cj
* @Date: 2018/12/28
*/ */
@Component @Component
public class ShutdownManager public class ShutdownManager
@ -29,13 +27,16 @@ public class ShutdownManager
shutdownAsyncManager(); shutdownAsyncManager();
} }
/**
* Seesion
*/
private void shutdownSpringSessionValidationScheduler() private void shutdownSpringSessionValidationScheduler()
{ {
if (springSessionValidationScheduler != null && springSessionValidationScheduler.isEnabled()) if (springSessionValidationScheduler != null && springSessionValidationScheduler.isEnabled())
{ {
try try
{ {
logger.info("关闭会话验证任务"); logger.info("====关闭会话验证任务====");
springSessionValidationScheduler.disableSessionValidation(); springSessionValidationScheduler.disableSessionValidation();
} }
catch (Exception e) catch (Exception e)
@ -45,12 +46,15 @@ public class ShutdownManager
} }
} }
/**
*
*/
private void shutdownAsyncManager() private void shutdownAsyncManager()
{ {
try try
{ {
logger.info("关闭后台任务线程池"); logger.info("====关闭后台任务任务线程池====");
AsyncManager.me().shutdown(10, TimeUnit.SECONDS); AsyncManager.me().shutdown();
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -8,6 +8,7 @@ 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 com.ruoyi.common.utils.Threads;
/** /**
* *
@ -136,15 +137,7 @@ public class SpringSessionValidationScheduler implements SessionValidationSchedu
if (this.enabled) if (this.enabled)
{ {
executorService.shutdown(); Threads.shutdownAndAwaitTermination(executorService);
try
{
executorService.awaitTermination(10, TimeUnit.SECONDS);
}
catch (InterruptedException e)
{
log.error(e.getMessage(), e);
}
} }
this.enabled = false; this.enabled = false;
} }