springboot定时任务线程池配置

今天药忘吃喽~ 2022-11-18 10:51 333阅读 0赞

首先定义的是异步线程池,需要再启动类添加注解

@EnableScheduling
@EnableAsync

  1. package com.git.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.scheduling.annotation.EnableAsync;
  5. import org.springframework.scheduling.annotation.EnableScheduling;
  6. @SpringBootApplication
  7. @EnableScheduling
  8. @EnableAsync
  9. public class GitDemoApplication{
  10. public static void main(String[] args) {
  11. SpringApplication.run(GitDemoApplication.class, args);
  12. }
  13. }

定义异步任务类

  1. package com.git.demo.task;
  2. import org.springframework.scheduling.annotation.Async;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public class ScheduledTask {
  7. @Scheduled(cron = "0/1 * * * * ?")
  8. @Async
  9. public void scheduledTask1() throws Exception{
  10. int a=1/0;
  11. System.out.println(Thread.currentThread().getName()+"---scheduledTask1---"+System.currentTimeMillis());
  12. Thread.sleep(4000);
  13. }
  14. @Scheduled(cron = "0/1 * * * * ?")
  15. @Async("asyncTaskExecutor")
  16. public void scheduledTask2(){
  17. System.out.println(Thread.currentThread().getName()+"---scheduledTask2---"+System.currentTimeMillis());
  18. }
  19. }

局部线程池

  1. package com.git.demo.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.core.task.AsyncTaskExecutor;
  4. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  5. import org.springframework.stereotype.Component;
  6. import java.util.concurrent.ThreadPoolExecutor;
  7. /**
  8. * 自定义局部线程池 使用时需要指定名称为asyncTaskExecutor
  9. */
  10. @Component
  11. public class AsyncTaskExecutorConfig {
  12. @Bean("asyncTaskExecutor")
  13. public AsyncTaskExecutor getAsyncTaskExecutor() {
  14. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  15. //自定义线程池前缀
  16. executor.setThreadNamePrefix("defineAsyncTask-");
  17. //核心线程数
  18. executor.setCorePoolSize(2);
  19. //最大线程数
  20. executor.setMaxPoolSize(5);
  21. //队列大小
  22. executor.setQueueCapacity(50);
  23. //淘汰策略:CallerRunsPolicy:当线程池没有能力处理时直接在执行方法的调用线程中运行被拒绝的任务
  24. // AbortPolicy:处理程序遭到拒绝时将抛出 RejectedExecutionException
  25. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  26. //等待所有任务调度完成在关闭线程池,保证所有的任务被正确处理
  27. executor.setWaitForTasksToCompleteOnShutdown(true);
  28. //线程池关闭时等待其他任务的时间,不能无限等待,确保应用最后能被关闭。而不是无限期阻塞
  29. executor.setAwaitTerminationSeconds(60);
  30. //初始化线程池
  31. executor.initialize();
  32. return executor;
  33. }
  34. }

全量线程池

  1. package com.git.demo.config;
  2. import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
  5. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  6. import org.springframework.stereotype.Component;
  7. import java.util.concurrent.Executor;
  8. import java.util.concurrent.ThreadPoolExecutor;
  9. /**
  10. * 定义全局线程池
  11. */
  12. @Component
  13. public class AsyncGlobalConfig extends AsyncConfigurerSupport {
  14. @Value("${define.global.async.prefix}")
  15. private String threadName;
  16. @Override
  17. public Executor getAsyncExecutor() {
  18. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  19. executor.setThreadNamePrefix(threadName);
  20. executor.setCorePoolSize(2);
  21. executor.setMaxPoolSize(10);
  22. executor.setQueueCapacity(100);
  23. executor.setKeepAliveSeconds(20);
  24. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  25. executor.setWaitForTasksToCompleteOnShutdown(true);
  26. executor.setAwaitTerminationSeconds(60);
  27. executor.initialize();
  28. return executor;
  29. }
  30. /**
  31. * 重写线程异常处理方法
  32. * @return
  33. */
  34. @Override
  35. public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
  36. return new CustomAsyncExceptionHandler();
  37. }
  38. }

线程异常处理类

  1. package com.git.demo.config;
  2. import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
  3. import java.lang.reflect.Method;
  4. /**
  5. * 处理线程异常
  6. */
  7. public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
  8. @Override
  9. public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
  10. System.out.println("线程异常了");
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,333人围观)

还没有评论,来说两句吧...

相关阅读