SpringBoot项目给定时任务配置线程池

208 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

上篇文章简单介绍了怎么在SpringBoot项目中运用简单的定时(间隔)任务,并且举例用的是单个任务,如果是多个定时(间隔)任务,并且想同时执行,应该怎么办?

首先经过了解,@Scheduled是单线程的,如果有多个定时任务,势必需要前一个任务执行完才会执行后面的任务,先放代码:

package com.wl.standard.task;
 
import com.wl.standard.entity.City;
import com.wl.standard.entity.CityGdp;
import com.wl.standard.entity.CityRail;
import com.wl.standard.service.CityGdpService;
import com.wl.standard.service.CityRailService;
import com.wl.standard.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
 
import java.util.List;
 
/**
 * 间隔任务
 * @author wl
 * @date 2021/12/23
 */
@Configuration
@Slf4j
@PropertySource("classpath:/task-config.ini")
public class IntervalTask {
 
    private final CityService cityService;
    private final CityGdpService cityGdpService;
    private final CityRailService cityRailService;
 
    @Autowired
    public IntervalTask(CityService cityService, CityGdpService cityGdpService,
                        CityRailService cityRailService) {
        this.cityService = cityService;
        this.cityGdpService = cityGdpService;
        this.cityRailService = cityRailService;
    }
 
    @Scheduled(cron = "${listAllCity.cron}")
    public void listAllCity() {
        List<City> cityList = cityService.list();
        log.info("城市数量: {}", cityList.size());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    @Scheduled(cron = "${listAllCityGdp.cron}")
    public void listAllCityGdp() {
        List<CityGdp> cityGdpList = cityGdpService.list();
        log.info("城市GDP数量: {}", cityGdpList.size());
    }
 
    @Scheduled(cron = "${listAllCityRail.cron}")
    public void listAllCityRail() {
        List<CityRail> cityRailList = cityRailService.list();
        log.info("城市轨道交通数量: {}", cityRailList.size());
    }
}

现在设置的间隔时间为15秒,执行启动类看实际效果:

image.png 因为在listAllCity方法中写了一段睡眠5秒的代码,而@Scheduled又是单线程的,所以另外2个定时任务要在listAllCity执行5秒后才会开始执行。

如果需求是需要多个定时任务同时执行,这时就需要将定时任务变为多线程的,这里可以通过创建线程池的方法解决这个问题,代码如下:

package com.wl.standard.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
 
/**
 * 定时任务线程池配置
 * @author wl
 * @date 2021/11/23
 */
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskScheduler());
    }
 
    @Bean
    public TaskScheduler taskScheduler(){
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(4);
        taskScheduler.setThreadNamePrefix("scheduler-pool-");
        return taskScheduler;
    }
}

实际执行效果如下:

image.png

不仅时间上几乎是同时执行,通过线程名也可以看出3个定时任务分别由3个不同的线程来执行的,这样就达到了开始的目的。