在SpringBoot中配置定时任务
前言
之前在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中配置定时任务的更多相关文章
- springBoot中的定时任务
springBoot中的定时任务 1:在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 2:新建ScheduledTasks任务类 : package c ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
- springboot中配置主从redis
测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...
- SpringBoot中执行定时任务
一:在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. @SpringBootApplication @EnableSchedu ...
- SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理
SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...
- springBoot中使用定时任务
简单示例 导入依赖 springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务. <parent> <groupId>org.springfram ...
- SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10659045.html,否则将追究法律责任!!! 一.在JAVA开发领域,目前可以通过以下 ...
- springboot中配置过滤器以及可能出现的问题
在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...
- SpringBoot中的定时任务与Quartz的整合
SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...
随机推荐
- linux根据端口号查询来源程序
1.根据端口号查询进程 netstat -tunlp|grep port 2.根据进程查询来源程序 ps aux | grep pid 上图看出所属进程为2281 上图看出占用8083端口的程序为n ...
- LIUNX-Centos 7 编译GDAL
一.准备工作 安装编译环境 sudo yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-dev ...
- MongoDB Driver 简单的CURD
c#中我们可以使用MongoDB.Driver驱动进行对MongoDB数据库的增删改查. 首先需要在NuGet中安装驱动 安装完毕后会发现会有三个引用 其中 MongoDB.Driver和MongoD ...
- 关于metaclass,我原以为我是懂的
关于Python2.x中metaclass这一黑科技,我原以为我是懂的,只有当被打脸的时候,我才认识到自己too young too simple sometimes native. 为什么之前我认为 ...
- CentOS7安装MySQL的方法之通用二进制格式
CentOS7安装MySQL的方法之通用二进制格式
- JDBC学习笔记(四)
减少各个Dao类间的重复代码,有以下几种方式: 写一个DBConnectionManager,将公共的查询逻辑做成方法,将sql语句作为参数传递给方法. public class DBConnecti ...
- BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 [高斯消元 概率DP]
1778: [Usaco2010 Hol]Dotp 驱逐猪猡 题意:一个炸弹从1出发p/q的概率爆炸,否则等概率走向相邻的点.求在每个点爆炸的概率 高斯消元求不爆炸到达每个点的概率,然后在一个点爆炸就 ...
- iOS学习——布局利器Masonry框架源码深度剖析
iOS开发过程中很大一部分内容就是界面布局和跳转,iOS的布局方式也经历了 显式坐标定位方式 --> autoresizingMask --> iOS 6.0推出的自动布局(Auto La ...
- H5弹性盒布局的使用(父容器属性)
为父容器添加display:flex/inline-flex 父容器可以使用的属性有: 1.flex-direction:决定主轴的方向 有四个属性值: row(默认值):主轴为水平方向,起点在左端. ...
- 定时跳转的两种方式(html + javaweb)
html方式 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...