前言

之前在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中配置定时任务的更多相关文章

  1. springBoot中的定时任务

    springBoot中的定时任务 1:在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 2:新建ScheduledTasks任务类 : package c ...

  2. 在SpringBoot中配置aop

    前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...

  3. springboot中配置主从redis

    测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...

  4. SpringBoot中执行定时任务

    一:在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. @SpringBootApplication @EnableSchedu ...

  5. SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理

    SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...

  6. springBoot中使用定时任务

    简单示例 导入依赖 springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务. <parent> <groupId>org.springfram ...

  7. SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10659045.html,否则将追究法律责任!!! 一.在JAVA开发领域,目前可以通过以下 ...

  8. springboot中配置过滤器以及可能出现的问题

    在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...

  9. SpringBoot中的定时任务与Quartz的整合

    SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...

随机推荐

  1. Windows脚本相关

    1 获取IP地址 echo StartChangeIPFile echo 获取主机名 for /f %%i in ('hostname') do (set pcName=%%i) ::ping %pc ...

  2. 一个Android上的以滑动揭示的方式显示并切换图片的View

    SlideView是一个Android上的以滑动揭示的方式显示并切换图片的View,以视觉对比的方式把一套相似的图片展示出来. 示例 翻页图片揭示效果: 特性 设置一组(List<ImageIn ...

  3. Linux cp 移动的时候报错

    报错如下: cp: omitting directory `./nginx-1.12.1'   原因: 要移动的目录下还存在有目录   解决: cp -r 文件名 地址   注意: 这里的-r代表递归 ...

  4. windows 查看端口被哪个程序占用

    比如查看端口8080 1. 查看占用端口8080对应的PID,输入命令:netstat -aon|findstr "8080" (加入查到pid为111222) 2. 继续输入ta ...

  5. ios GCD将异步转换为同步

    在开发中有时需要等网络请求完成之后拿到数据做一些操作,而且有时是同时好几个网络请求同时发起.这时会有对异步操作进行更进一步控制的场景,不单网络请求,有时一些其他本地文件,多张图片处理等可能都会遇到这种 ...

  6. Halcon一日一练:创建三通道图像

    首先理解一个什么是三通道图像: 三通道图像就是彩色图像,我们之前黑白相机或黑白电视机都是彩用的灰阶图像,即单通道图像,一般是2的8次方个灰阶,即256个灰阶.彩色图像采用RGB,红绿蓝三个通道来合成彩 ...

  7. Java内存回收机制基础[转]

    原文链接:http://blog.jobbole.com/37273/ 在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方面工作都是由JVM自动完成的,降低了J ...

  8. BZOJ 3544: [ONTAK2010]Creative Accounting [set]

    给定一个长度为N的数组a和M,求一个区间[l,r],使得$(\sum_{i=l}^{r}{a_i}) mod M$的值最大,求出这个值,注意这里的mod是数学上的mod 这道题真好,题面连LaTeX都 ...

  9. js小知识点

    1.setTimeout(function(num){ alert(num);},1000,123); 第三个参数为实参. 2.拼接字符串: document.body.innerHTML = '&l ...

  10. HashMap----工作原理

    先来些简单的问题 "你用过HashMap吗?" "什么是HashMap?你为什么用到它?" 几乎每个人都会回答"是的",然后回答HashMa ...