SpringBoot自定义线程池,执行定时任务方式
作者:Hello!joy先生
SpringBoot自定义线程池,执行定时任务
在我们开发过程中有很多场景需要定时执行,接下来我们在SpringBoot中实现定时任务的功能。
一、 几个必要的注解
1、@EnableScheduling:在启动类上添加此注解,帮助我们开启定时任务。
2、@Scheduled:在实现接口上添加此注解,表示此接口需要定时调用执行,这个注解的参数有不少,在文章最后展示,可以自己了解下。
3、@Component:在需要执行定时任务的实现类上添加此注解。
4、@Configuration:自定义多线程使用
5、@EnableAsync:在自定义多线程配置类上添加
6、@Async:在执行定时任务的方法上添加,表示使用自定义线程
二、 单线程实现定时任务
单线程场景在实际开发中并不多见,我们只用来测试
直接上代码
1、启动类添加@EnableScheduling注解
@SpringBootApplication
@EnableScheduling
public class LogaopApplication {
    public static void main(String[] args) {
        SpringApplication.run(LogaopApplication.class, args);
    }
}
2、在执行定时任务的实现类添加@Component注解,在执行定时任务的方法上添加@Scheduled注解
@Service
@Slf4j
@Component
public class ScheduTestServiceImpl implements ScheduTestService {
    @Autowired
    private ScheduJobDao scheduJobDao;
    @Scheduled(cron = "*/1 * * * * ?") // 一秒执行一次
    @Override
    public void taskTest() throws InterruptedException {
        System.out.println("测试定时任务------");
    }
执行结果
2023-09-16 11:17:17.978 INFO 4636 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'asyncServiceExecutor'
2023-09-16 11:17:18.183 INFO 4636 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2023-09-16 11:17:18.214 INFO 4636 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-16 11:17:18.230 INFO 4636 --- [ main] com.qty.logaop.LogaopApplication : Started LogaopApplication in 1.98 seconds (JVM running for 2.555)
测试定时任务------
测试定时任务------
测试定时任务------
测试定时任务------
测试定时任务------
测试定时任务------
测试定时任务------
以上就是单线程执行定时任务,接下来测试多线程定时任务。
三、自定义多线程执行定时任务
因为springboot默认是提供单线程供我们使用,一旦有多个定时任务,当某个任务被阻塞,那么其他定时任务也不会被执行,所以,一般我们会自定义线程池来解决这个问题
多个定时任务场景,某个阻塞情况演示
@Service
@Slf4j
@Component
public class ScheduTestServiceImpl implements ScheduTestService {
    @Autowired
    private ScheduJobDao scheduJobDao;
    @Scheduled(cron = "*/1 * * * * ?")
    @Override
    public void taskTest() throws InterruptedException {
        long timeStamp1 = System.currentTimeMillis();
        Thread.sleep(1000*10);
        System.out.println("测试定时任务1------");
        long timeStamp2 = System.currentTimeMillis();
        log.info("阻塞市时长:{}",timeStamp2-timeStamp1);
    }
    @Scheduled(cron = "*/1 * * * * ?")
    @Override
    public void taskTest2() throws InterruptedException {
        System.out.println("测试定时任务2------");
    }
演示结果
2023-09-16 11:28:07.906 INFO 19692 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'asyncServiceExecutor'
2023-09-16 11:28:08.157 INFO 19692 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2023-09-16 11:28:08.189 INFO 19692 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-16 11:28:08.204 INFO 19692 --- [ main] com.qty.logaop.LogaopApplication : Started LogaopApplication in 1.903 seconds (JVM running for 2.457)
测试定时任务1------
2023-09-16 11:28:19.035 INFO 19692 --- [ scheduling-1] c.q.l.s.impl.ScheduTestServiceImpl : 阻塞市时长:10015
测试定时任务2------
测试定时任务1------
2023-09-16 11:28:30.010 INFO 19692 --- [ scheduling-1] c.q.l.s.impl.ScheduTestServiceImpl : 阻塞市时长:10001
测试定时任务2------
这样就会造成很大的问题,必须执行完某个定时任务才会执行下一个,所以我们需要自定义多线程解决这个问题
1、线程池配置类
package com.qty.logaop.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ScheduThreadPool {
    @Value("${async.executor.thread.core_pool_size}")//从配置文件中获取配置项
    private int corePoolSize;
    @Value("${async.executor.thread.max_pool_size}")
    private int maxPoolSize;
    @Value("${async.executor.thread.queue_capacity}")
    private int queueCapacity;
    @Value("${async.executor.thread.name.prefix}")
    private String namePrefix;
    /**
     * 自定义线程池配置类。
     * 不要命名为 taskScheduler,与spring框架的bean重名。
     * @return
     */
    @Bean(name = "asyncServiceExecutor")
    public Executor asyncServiceExecutor() {
        //阿里巴巴编程规范:线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。
        //SpringBoot项目,可使用Spring提供的对 ThreadPoolExecutor 封装的线程池 ThreadPoolTaskExecutor:
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        ThreadPoolTaskExecutor executor = new MyThreadPoolTaskExecutor();//自定义ThreadPoolTaskExecutor,会打印线程池情况
        //配置核心线程数
        executor.setCorePoolSize(corePoolSize);
        //配置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        //配置队列大小
        executor.setQueueCapacity(queueCapacity);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix(namePrefix);
        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        //     1、CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行。
        //        "该策略既不会抛弃任务,也不会抛出异常,而是将任务回推到调用者。"顾名思义,在饱和的情况下,调用者会执行该任务(而不是由多线程执行)
        //     2、AbortPolicy:拒绝策略,直接拒绝抛出异常
        //     3、。。。
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }
}
我们在方法上添加@Async注解后再看结果
    @Scheduled(cron = "*/10 * * * * ?")
    @Async("asyncServiceExecutor")
    @Override
    public void taskTest() throws InterruptedException {
        long timeStamp1 = System.currentTimeMillis();
        Thread.sleep(1000*100);
        System.out.println("测试定时任务1------");
        long timeStamp2 = System.currentTimeMillis();
        log.info("阻塞市时长:{}",timeStamp2-timeStamp1);
    }
    @Scheduled(cron = "*/10 * * * * ?")
    @Async("asyncServiceExecutor")
    @Override
    public void taskTest2() throws InterruptedException {
        System.out.println("测试定时任务2------");
    }
演示结果
2023-09-16 11:43:25.065 INFO 11604 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-09-16 11:43:25.065 INFO 11604 --- [ main] com.qty.logaop.LogaopApplication : Started LogaopApplication in 1.909 seconds (JVM running for 2.561)
测试定时任务2------
测试定时任务2------
测试定时任务2------
即使定时任务1被阻塞,定时任务2会获取到其他线程,继续执行的,这样就可以解决多个定时任务的问题了。
@Scheduled参数:
- 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点每天上午10点,下午2点,4点
 - 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
 - 0 0 12 ? * WED 表示每个星期三中午12点
 - 0 0 12 * * ? 每天中午12点触发
 - 0 15 10 ? * * 每天上午10:15触发
 - 0 15 10 * * ? 每天上午10:15触发
 - 0 15 10 * * ? * 每天上午10:15触发
 - 0 15 10 * * ? 2019 2019年的每天上午10:15触发
 - 0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
 - 0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
 - 0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
 - 0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
 - 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
 - 0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
 - 0 15 10 15 * ? 每月15日上午10:15触发
 - 0 15 10 L * ? 每月最后一日的上午10:15触发
 - 0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
 - 0 15 10 ? * 6L 2018-2019 2018年至2019年的每月的最后一个星期五上午10:15触发
 - 0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
 
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
