parent
b3541e9758
commit
8349e631d2
@ -1,55 +0,0 @@
|
|||||||
package com.ruoyi.framework.manager;
|
|
||||||
|
|
||||||
import java.util.TimerTask;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import com.ruoyi.common.utils.Threads;
|
|
||||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步任务管理器
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
public class AsyncManager
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* 操作延迟10毫秒
|
|
||||||
*/
|
|
||||||
private final int OPERATE_DELAY_TIME = 10;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步操作任务调度线程池
|
|
||||||
*/
|
|
||||||
private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单例模式
|
|
||||||
*/
|
|
||||||
private AsyncManager(){}
|
|
||||||
|
|
||||||
private static AsyncManager me = new AsyncManager();
|
|
||||||
|
|
||||||
public static AsyncManager me()
|
|
||||||
{
|
|
||||||
return me;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行任务
|
|
||||||
*
|
|
||||||
* @param task 任务
|
|
||||||
*/
|
|
||||||
public void execute(TimerTask task)
|
|
||||||
{
|
|
||||||
executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 停止任务线程池
|
|
||||||
*/
|
|
||||||
public void shutdown()
|
|
||||||
{
|
|
||||||
Threads.shutdownAndAwaitTermination(executor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,39 +1,41 @@
|
|||||||
package com.ruoyi.framework.manager;
|
package com.ruoyi.framework.manager;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import com.ruoyi.common.utils.Threads;
|
||||||
import org.slf4j.LoggerFactory;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
import javax.annotation.PreDestroy;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 确保应用退出时能关闭后台线程
|
* 确保应用退出时能关闭后台线程
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Slf4j(topic = "sys-user")
|
||||||
@Component
|
@Component
|
||||||
public class ShutdownManager
|
public class ShutdownManager {
|
||||||
{
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger("sys-user");
|
@Autowired
|
||||||
|
@Qualifier("scheduledExecutorService")
|
||||||
|
private ScheduledExecutorService scheduledExecutorService;
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
public void destroy()
|
public void destroy() {
|
||||||
{
|
shutdownAsyncManager();
|
||||||
shutdownAsyncManager();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 停止异步执行任务
|
* 停止异步执行任务
|
||||||
*/
|
*/
|
||||||
private void shutdownAsyncManager()
|
private void shutdownAsyncManager() {
|
||||||
{
|
try {
|
||||||
try
|
log.info("====关闭后台任务任务线程池====");
|
||||||
{
|
Threads.shutdownAndAwaitTermination(scheduledExecutorService);
|
||||||
logger.info("====关闭后台任务任务线程池====");
|
} catch (Exception e) {
|
||||||
AsyncManager.me().shutdown();
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
}
|
||||||
{
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue