SpringBoot定时任务

SpringBoot定时任务介绍

SpringBoot定时任务依赖于Spring中的spring-context包,所以只要容器包含spring-context包,就可以使用定时任务,如spring-boot-starter-web。直接导入依赖即可

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

SpringBoot定时任务基础使用

使用SpringBoot定时任务之前,在主类上线开启定时任务,需要在主类上面加上@EnableScheduling
如下:

@EnableScheduling
@SpringBootApplication
public class TestScheduleApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(TestScheduleApplication.class, args);
        System.out.println("End=====================");
    }
}

接下来实现一个定时任务,5s打印一次日志

@Configuration
@Slf4j
public class TestSchedule {
    
    // 5s执行一次
    @Scheduled(cron = "*/5 * * * * *")
    public void testSchedule(){
        log.info("测试SpringBoot定时任务");
    }
}

测试结果如下:
在这里插入图片描述
我们会发现每次执行的都是scheduling-1这个任务调度线程在执行。而没有使用到SpringBoot自动配置的ThreadPoolTaskExecutor 线程池,所以此处需要使用注解开启异步执行操作才会使用线程池。
操作如下,在主类上添加@EnableAsync注解,在方法上添加@Async注解
在这里插入图片描述
在这里插入图片描述
测试结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/b512ee69b58d440b9c347f8fd6a5d632.png
现在可以看出每次执行的线程不一样,自动配置的任务执行线程池默认是 8 个核心线程。
从源码可以看出
在这里插入图片描述
我们可以不用默认的线程池,可以自定义线程池,一种方式通过yml文件实现,但是yml文件也有缺点,如我们的线程不可能无限大,所以线程池有拒绝策略的,yml文件无法实现这个,
yml配置如下:

spring:
  task:
    execution:
      pool:
        coreSize: 6  #核心线程数6
        maxSize: 20  #最大线程数20
        queueCapacity: 20  #队列大小20
        keepAlive: 60s    #线程存活时间60s
        allowCoreThreadTimeout: true  #是否允许核心线程超时:是

第二种自定义线程Bean实现,实现如下:
这里面也有两种,一种是自定义ThreadPoolTaskExecutor ,另外一种是自定义ThreadPoolTaskScheduler
定义ThreadPoolTaskExecutor

@Configuration
public class TaskExecutorConfig {
    
    @Bean
    public ThreadPoolTaskExecutor taskExecutor(TaskExecutionProperties taskExecutionProperties) {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();

        PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
        TaskExecutionProperties.Pool pool = taskExecutionProperties.getPool();
        map.from(20).to(taskExecutor::setQueueCapacity);
        map.from(6).to(taskExecutor::setCorePoolSize);
        map.from(10).to(taskExecutor::setMaxPoolSize);
        map.from(60).to(taskExecutor::setKeepAliveSeconds);
        map.from(true).to(taskExecutor::setAllowCoreThreadTimeOut);
        map.from("myTask-").whenHasText().to(taskExecutor::setThreadNamePrefix);

        return taskExecutor;
    }
    
}

定义ThreadPoolTaskScheduler

@Configuration
public class TaskSchedulerConfig {
    
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    
        // 配置 ThreadPoolTaskScheduler 的属性
        scheduler.setPoolSize(3); // 设置线程池大小
        scheduler.setThreadNamePrefix("myScheduler-"); // 设置线程名称前缀
    
        scheduler.setAwaitTerminationSeconds(60);         // 等待时长
        scheduler.setWaitForTasksToCompleteOnShutdown(true);  // 调度器shutdown被调用时等待当前被调度的任务完成
        return scheduler;
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值