栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

上篇文章简单介绍了怎么在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;


@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 cityList = cityService.list();
        log.info("城市数量: {}", cityList.size());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "${listAllCityGdp.cron}")
    public void listAllCityGdp() {
        List cityGdpList = cityGdpService.list();
        log.info("城市GDP数量: {}", cityGdpList.size());
    }

    @Scheduled(cron = "${listAllCityRail.cron}")
    public void listAllCityRail() {
        List cityRailList = cityRailService.list();
        log.info("城市轨道交通数量: {}", cityRailList.size());
    }
}

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

 因为在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;


@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;
    }
}

实际执行效果如下:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677615.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号