package com.shkpr.service.alambizplugin.configuration; import com.global.base.log.LogLevelFlag; import com.global.base.log.LogPrintMgr; import com.shkpr.service.alambizplugin.commtools.TimeTool; import com.shkpr.service.alambizplugin.constants.LogFlagBusiType; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.boot.task.TaskExecutorBuilder; 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.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import javax.annotation.PostConstruct; import java.lang.reflect.Method; import java.time.Duration; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Executor; @Configuration @EnableScheduling @EnableAsync public class ScheduleTaskConfiguration implements SchedulingConfigurer, AsyncConfigurer { private final String mStrClassName; public ScheduleTaskConfiguration() { mStrClassName = this.getClass().getSimpleName(); } @Override public Executor getAsyncExecutor() { return taskExecutor(); } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new AsyncUncaughtExceptionHandler() { @Override public void handleUncaughtException(Throwable ex, Method method, Object... params) { LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, LogFlagBusiType.BUSI_INIT.toStrValue(), mStrClassName , 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(2); scheduler.setThreadNamePrefix("Time-Task-"); scheduler.setAwaitTerminationSeconds(5); scheduler.setWaitForTasksToCompleteOnShutdown(true); return scheduler; } /** * 异步任务多线程处理 * * @return 线程任务执行 */ @Bean(destroyMethod = "shutdown", name = "asyncThreadPoolTaskExecutor") public ThreadPoolTaskExecutor taskExecutor() { TaskExecutorBuilder builder = new TaskExecutorBuilder(); builder = builder.queueCapacity(Integer.MAX_VALUE); builder = builder.corePoolSize(8); builder = builder.maxPoolSize(Integer.MAX_VALUE); builder = builder.allowCoreThreadTimeOut(true); // 允许核心线程超时回收:减少空闲资源占用[1](@ref) builder = builder.keepAlive(Duration.ofSeconds(30)); // 非核心线程空闲存活时间:建议30-60秒[1,4](@ref) builder = builder.threadNamePrefix("AsyncTask-"); // 线程名前缀:便于监控和日志追踪[1,4](@ref) return builder.build(); } @PostConstruct public void init() { new Timer().schedule(new TimerTask() { @Override public void run() { TimeTool.refreshUTCTimeRes(); } }, 10000); } }