线程池代码优化
This commit is contained in:
@ -3,6 +3,7 @@ package com.ruoyi.framework.manager;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
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);
|
||||
}
|
||||
|
||||
public void shutdown(long timeout, TimeUnit unit) throws Exception
|
||||
/**
|
||||
* 停止任务线程池
|
||||
*/
|
||||
public void shutdown()
|
||||
{
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(timeout, unit);
|
||||
Threads.shutdownAndAwaitTermination(executor);
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,11 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* ShutdownManager 类
|
||||
* 确保应用退出时能关闭后台线程
|
||||
*
|
||||
* @Auther: cj
|
||||
* @Date: 2018/12/28
|
||||
* @author cj
|
||||
*/
|
||||
@Component
|
||||
public class ShutdownManager
|
||||
@ -29,13 +27,16 @@ public class ShutdownManager
|
||||
shutdownAsyncManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止Seesion会话检查
|
||||
*/
|
||||
private void shutdownSpringSessionValidationScheduler()
|
||||
{
|
||||
if (springSessionValidationScheduler != null && springSessionValidationScheduler.isEnabled())
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("关闭会话验证任务");
|
||||
logger.info("====关闭会话验证任务====");
|
||||
springSessionValidationScheduler.disableSessionValidation();
|
||||
}
|
||||
catch (Exception e)
|
||||
@ -45,12 +46,15 @@ public class ShutdownManager
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止异步执行任务
|
||||
*/
|
||||
private void shutdownAsyncManager()
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.info("关闭后台任务线程池");
|
||||
AsyncManager.me().shutdown(10, TimeUnit.SECONDS);
|
||||
logger.info("====关闭后台任务任务线程池====");
|
||||
AsyncManager.me().shutdown();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -8,6 +8,7 @@ import org.apache.shiro.session.mgt.SessionValidationScheduler;
|
||||
import org.apache.shiro.session.mgt.ValidatingSessionManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.ruoyi.common.utils.Threads;
|
||||
|
||||
/**
|
||||
* 自定义任务调度器完成
|
||||
@ -136,15 +137,7 @@ public class SpringSessionValidationScheduler implements SessionValidationSchedu
|
||||
|
||||
if (this.enabled)
|
||||
{
|
||||
executorService.shutdown();
|
||||
try
|
||||
{
|
||||
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
Threads.shutdownAndAwaitTermination(executorService);
|
||||
}
|
||||
this.enabled = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user