|
@@ -0,0 +1,81 @@
|
|
|
+package com.ruoyi.framework.config.license;
|
|
|
+
|
|
|
+import com.global.base.log.LogLevelFlag;
|
|
|
+import com.global.base.log.LogPrintMgr;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.scheduling.TaskScheduler;
|
|
|
+import org.springframework.scheduling.annotation.AsyncConfigurer;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
+import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Timer;
|
|
|
+import java.util.TimerTask;
|
|
|
+import java.util.concurrent.Executor;
|
|
|
+
|
|
|
+import static com.ruoyi.framework.config.license.ScheduleTaskMgr.refreshUTCTimeRes;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableScheduling
|
|
|
+public class ScheduleTaskConfiguration implements SchedulingConfigurer, AsyncConfigurer {
|
|
|
+ private String mStrClassName;
|
|
|
+ public ScheduleTaskConfiguration() {
|
|
|
+ mStrClassName = this.getClass().getSimpleName();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger("sys-user");
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Executor getAsyncExecutor() {
|
|
|
+ Executor executor = taskScheduler();
|
|
|
+ return executor;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
|
|
+ return new AsyncUncaughtExceptionHandler() {
|
|
|
+ @Override
|
|
|
+ public void handleUncaughtException(Throwable ex, Method method, Object... params) {
|
|
|
+ logger.info(String.format("AsyncUncaughtException(method="+method.getName()+", msg="+ex.getMessage()+")..."));
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
|
|
|
+ TaskScheduler taskScheduler = taskScheduler();
|
|
|
+ scheduledTaskRegistrar.setTaskScheduler(taskScheduler);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 定时任务多线程处理 */
|
|
|
+ @Bean(destroyMethod = "shutdown")
|
|
|
+ public ThreadPoolTaskScheduler taskScheduler(){
|
|
|
+ ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
|
|
+ scheduler.setPoolSize(5);
|
|
|
+ scheduler.setThreadNamePrefix("Time-Task-");
|
|
|
+ //设置线程池中任务的等待时间,如果超过这个时间还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住。
|
|
|
+ //如:kill [PID]后,最大等待时长为5秒钟
|
|
|
+ scheduler.setAwaitTerminationSeconds(5);
|
|
|
+ //设置线程池关闭的时候等待所有任务都完成后,再继续销毁其他的Bean,这样这些异步任务的销毁就会先于数据库连接池对象的销毁
|
|
|
+ scheduler.setWaitForTasksToCompleteOnShutdown(true);
|
|
|
+ return scheduler;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init(){
|
|
|
+ new Timer().schedule(new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ ScheduleTaskMgr.refreshUTCTimeRes();
|
|
|
+ GlobalData.getInstance().reInitLicense();
|
|
|
+ }
|
|
|
+ }, 10000);//延时10秒执行
|
|
|
+ }
|
|
|
+}
|