前言

之前在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. JavaScript数据结构 (手打代码)

    array: 数组创建: ); //创建一个长度为6的数组 ,,,,,); 数组方法: var str="I love javascript"; var single=str.sp ...

  2. DNS服务器解析域名的过程

    最近在读许令波老师的<深入分析Java Web技术内幕>,算是对DNS服务器域名解析有个大体的理解,以下的内容来自个人对书中内容的整理 1.什么是域名解析? 当我们在浏览器的地址栏输入一个 ...

  3. JavaSE基础篇—MySQL三大范式—数据库设计规范

    1.概   念     范式是一种符合设计要求的总结,要想设计一个结构合理的关系型数据库,必须满足一定的范式.各个范式是以此嵌套包含的,范式越高,设计等级越高,在现实设计中也越难实现,一般数据库只要打 ...

  4. 浅谈WPF依赖项属性

    浅谈WPF依赖项属性 0. 引言 依赖项属性虽然在使用上和CLR属性一样,但是它是WPF特有的,不同于CLR属性.只是封装为我们常用CLR的属性,在语法使用上和CLR属性一样.WPF中一些功能:动画, ...

  5. Single-Pass Stereo Rendering for HoloLens——HoloLens的单程立体渲染

    原文网站:https://docs.unity3d.com/Manual/SinglePassStereoRenderingHoloLens.html Single-Pass Stereo Rende ...

  6. wpf阻止键盘快捷键alt+space,alt+F4

    /// <summary>        /// 阻止 alt+f4和alt+space 按键        /// </summary>        /// <par ...

  7. 安装redis 2.6.4

    下载redis-2.6.4下载链接:http://pan.baidu.com/s/1eQ9Z8NS make MALLOC=jemalloc/server/redis2/src/redis-serve ...

  8. BZOJ 3744: Gty的妹子序列 [分块]

    传送门 题意:询问区间内逆序对数 感觉这种题都成套路题了 两个预处理$f[i][j]$块i到j的逆序对数,$s[i][j]$前i块$\le j$的有多少个 f我直接处理成到元素j,方便一点 用个树状数 ...

  9. BZOJ 1180: [CROATIAN2009]OTOCI [LCT]

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 961  Solved: 594[Submit][S ...

  10. xftp上传失败之解决办法

    修改/usr/local 文件夹权限 rwx 为不可读不可写第三方不可访问 报错 传输状态 恢复文件夹/usr/local 读写第三方访问权限 成功上传