码迷,mamicode.com
首页 > 编程语言 > 详细

在SpringBoot中配置定时任务

时间:2018-02-09 17:22:48      阅读:879      评论:0      收藏:0      [点我收藏+]

标签:并行   context   sys   睡眠   完成   bsp   task   text   org   

前言

之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单。

已经加入我的github模版中:https://github.com/LinkinStars/springBootTemplate

 

定时任务的分类

所谓定时任务,就是在项目启动之后,定时的去执行一个任务,从而满足业务的需要。

定时任务分为下面几种,串行,并行,同步,异步

串行,并行:当配置了多个定时任务的时候,串行方式只会有一个线程去执行所有的任务,并且前一个任务执行完成,后一个任务才会开始执行;而并行是指会启动多个线程去执行所有的任务,各个任务同时进行执行

同步,异步:这是对于一个定时任务来说,同步是指,这个定时任务本身自己完成之后才能再次执行自己;异步是指任务本身即使没有完成,当定时到达的时候也会再次执行

 

举例来说(定时任务都设置为每两秒执行一次,但是执行中睡眠3秒)

[2018-02-08 09:45:14.005] [pool-1-thread-1] [INFO ] my_info - 任务1:执行
[2018-02-08 09:45:17.007] [pool-1-thread-1] [INFO ] my_info - 任务1:执行完成
[2018-02-08 09:45:17.009] [pool-1-thread-1] [INFO ] my_info - 任务2:执行
[2018-02-08 09:45:20.012] [pool-1-thread-1] [INFO ] my_info - 任务2:执行完成
[2018-02-08 09:45:20.013] [pool-1-thread-1] [INFO ] my_info - 任务3:执行
[2018-02-08 09:45:23.016] [pool-1-thread-1] [INFO ] my_info - 任务3:执行完成
[2018-02-08 09:45:23.017] [pool-1-thread-1] [INFO ] my_info - 任务4:执行
[2018-02-08 09:45:26.021] [pool-1-thread-1] [INFO ] my_info - 任务4:执行完成
[2018-02-08 09:45:26.022] [pool-1-thread-1] [INFO ] my_info - 任务1:执行
[2018-02-08 09:45:29.026] [pool-1-thread-1] [INFO ] my_info - 任务1:执行完成

可以看到这四个任务是同一个线程执行的,并且 只有当前一个任务完成的时候,下一个任务才会开始,所以当前是串行同步的

[2018-02-08 10:02:42.004] [pool-1-thread-1] [INFO ] my_info - 任务3:执行
[2018-02-08 10:02:42.004] [pool-1-thread-3] [INFO ] my_info - 任务4:执行
[2018-02-08 10:02:42.004] [pool-1-thread-4] [INFO ] my_info - 任务1:执行
[2018-02-08 10:02:42.004] [pool-1-thread-2] [INFO ] my_info - 任务2:执行
[2018-02-08 10:02:45.011] [pool-1-thread-4] [INFO ] my_info - 任务1:执行完成
[2018-02-08 10:02:45.011] [pool-1-thread-2] [INFO ] my_info - 任务2:执行完成
[2018-02-08 10:02:45.011] [pool-1-thread-3] [INFO ] my_info - 任务4:执行完成
[2018-02-08 10:02:45.011] [pool-1-thread-1] [INFO ] my_info - 任务3:执行完成
[2018-02-08 10:02:46.005] [pool-1-thread-2] [INFO ] my_info - 任务2:执行
[2018-02-08 10:02:46.005] [pool-1-thread-4] [INFO ] my_info - 任务1:执行
[2018-02-08 10:02:46.005] [pool-1-thread-5] [INFO ] my_info - 任务4:执行
[2018-02-08 10:02:46.005] [pool-1-thread-8] [INFO ] my_info - 任务3:执行
[2018-02-08 10:02:49.011] [pool-1-thread-2] [INFO ] my_info - 任务2:执行完成
[2018-02-08 10:02:49.011] [pool-1-thread-4] [INFO ] my_info - 任务1:执行完成
[2018-02-08 10:02:49.011] [pool-1-thread-8] [INFO ] my_info - 任务3:执行完成
[2018-02-08 10:02:49.011] [pool-1-thread-5] [INFO ] my_info - 任务4:执行完成

可以看到这四个任务是不同线程执行的,并且当前也是只有在前一个任务完成的情况下才会执行下一个任务,所以当前是并行同步的

我们项目中喜欢使用的是第二种方式

 

配置方式

如果使用串行方式如下

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 定时任务配置
 * @author LinkinStar
 */
@Configuration
@EnableScheduling
public class TimeTaskConfig {

    @Scheduled(cron = "0/5 * * * * ?")
    public void test1(){
        System.out.println("任务1:执行");
    }
}

如果使用并行方式如下

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executors;

/**
 * 定时任务配置
 * @author LinkinStar
 */
@Configuration
@EnableScheduling
public class TimeTaskConfig implements SchedulingConfigurer {

    /**
     * 配置定时任务线程池大小
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }
}
import com.linkinstars.springBootTemplate.util.LogUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 定时任务配置
 * @author LinkinStar
 */
@Configuration
public class TimeTask {

    @Scheduled(cron = "0/2 * * * * ?")
    public void test1(){
        LogUtil.printLog("任务1:执行");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LogUtil.printLog("任务1:执行完成");
    }

}

 

参考

https://www.jianshu.com/p/ef18af5a9c1d 

https://www.cnblogs.com/slimer/p/6222485.html

在SpringBoot中配置定时任务

标签:并行   context   sys   睡眠   完成   bsp   task   text   org   

原文地址:https://www.cnblogs.com/linkstar/p/8435958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!