ScheduleTaskConfiguration.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.shkpr.service.alambizplugin.configuration;
  2. import com.global.base.log.LogLevelFlag;
  3. import com.global.base.log.LogPrintMgr;
  4. import com.shkpr.service.alambizplugin.commtools.TimeTool;
  5. import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
  6. import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
  7. import org.springframework.boot.task.TaskExecutorBuilder;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.scheduling.TaskScheduler;
  11. import org.springframework.scheduling.annotation.AsyncConfigurer;
  12. import org.springframework.scheduling.annotation.EnableAsync;
  13. import org.springframework.scheduling.annotation.EnableScheduling;
  14. import org.springframework.scheduling.annotation.SchedulingConfigurer;
  15. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  16. import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
  17. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
  18. import javax.annotation.PostConstruct;
  19. import java.lang.reflect.Method;
  20. import java.time.Duration;
  21. import java.util.Timer;
  22. import java.util.TimerTask;
  23. import java.util.concurrent.Executor;
  24. @Configuration
  25. @EnableScheduling
  26. @EnableAsync
  27. public class ScheduleTaskConfiguration implements SchedulingConfigurer, AsyncConfigurer {
  28. private final String mStrClassName;
  29. public ScheduleTaskConfiguration() {
  30. mStrClassName = this.getClass().getSimpleName();
  31. }
  32. @Override
  33. public Executor getAsyncExecutor() {
  34. return taskExecutor();
  35. }
  36. @Override
  37. public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
  38. return new AsyncUncaughtExceptionHandler() {
  39. @Override
  40. public void handleUncaughtException(Throwable ex, Method method, Object... params) {
  41. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, LogFlagBusiType.BUSI_INIT.toStrValue(), mStrClassName
  42. , String.format("AsyncUncaughtException(method=" + method.getName() + ", msg=" + ex.getMessage() + ")..."));
  43. }
  44. };
  45. }
  46. @Override
  47. public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
  48. TaskScheduler taskScheduler = taskScheduler();
  49. scheduledTaskRegistrar.setTaskScheduler(taskScheduler);
  50. }
  51. /**
  52. * 定时任务多线程处理
  53. */
  54. @Bean(destroyMethod = "shutdown")
  55. public ThreadPoolTaskScheduler taskScheduler() {
  56. ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
  57. scheduler.setPoolSize(2);
  58. scheduler.setThreadNamePrefix("Time-Task-");
  59. scheduler.setAwaitTerminationSeconds(5);
  60. scheduler.setWaitForTasksToCompleteOnShutdown(true);
  61. return scheduler;
  62. }
  63. /**
  64. * 异步任务多线程处理
  65. *
  66. * @return 线程任务执行
  67. */
  68. @Bean(destroyMethod = "shutdown", name = "asyncThreadPoolTaskExecutor")
  69. public ThreadPoolTaskExecutor taskExecutor() {
  70. TaskExecutorBuilder builder = new TaskExecutorBuilder();
  71. builder = builder.queueCapacity(Integer.MAX_VALUE);
  72. builder = builder.corePoolSize(8);
  73. builder = builder.maxPoolSize(Integer.MAX_VALUE);
  74. builder = builder.allowCoreThreadTimeOut(true); // 允许核心线程超时回收:减少空闲资源占用[1](@ref)
  75. builder = builder.keepAlive(Duration.ofSeconds(30)); // 非核心线程空闲存活时间:建议30-60秒[1,4](@ref)
  76. builder = builder.threadNamePrefix("AsyncTask-"); // 线程名前缀:便于监控和日志追踪[1,4](@ref)
  77. return builder.build();
  78. }
  79. @PostConstruct
  80. public void init() {
  81. new Timer().schedule(new TimerTask() {
  82. @Override
  83. public void run() {
  84. TimeTool.refreshUTCTimeRes();
  85. }
  86. }, 10000);
  87. }
  88. }