详解SpringBoot定时任务说明
1. 定时任务实现方式
定时任务实现方式:
- java自带的java.util.timer类,这个类允许你调度一个java.util.timertask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少,这篇文章将不做详细介绍。
- 使用quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,有空介绍。
- springboot自带的scheduled,可以将它看成一个轻量级的quartz,而且使用起来比quartz简单许多,本文主要介绍。
定时任务执行方式:
- 单线程(串行)
- 多线程(并行)
2. 创建定时任务
package com.autonavi.task.test; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.scheduling.annotation.scheduled; import org.springframework.stereotype.component; import com.autonavi.task.scheduledtasks; @component public class scheduledtest { private static final logger logger = loggerfactory.getlogger(scheduledtasks.class); @scheduled(cron="0 0/2 8-20 * * ?") public void executefiledownloadtask() { // 间隔2分钟,执行工单上传任务 thread current = thread.currentthread(); system.out.println("定时任务1:"+current.getid()); logger.info("scheduledtest.executefiledownloadtask 定时任务1:"+current.getid()+ ",name:"+current.getname()); } @scheduled(cron="0 0/1 8-20 * * ?") public void executeuploadtask() { // 间隔1分钟,执行工单上传任务 thread current = thread.currentthread(); system.out.println("定时任务2:"+current.getid()); logger.info("scheduledtest.executeuploadtask 定时任务2:"+current.getid() + ",name:"+current.getname()); } @scheduled(cron="0 0/3 5-23 * * ?") public void executeuploadbacktask() { // 间隔3分钟,执行工单上传任务 thread current = thread.currentthread(); system.out.println("定时任务3:"+current.getid()); logger.info("scheduledtest.executeuploadbacktask 定时任务3:"+current.getid()+ ",name:"+current.getname()); } }
@scheduled 注解用于标注这个方法是一个定时任务的方法,使用@scheduled(cron=”…”) 表达式来设置定时任务。
// 每天早八点到晚八点,间隔2分钟执行任务 @scheduled(cron="0 0/2 8-20 * * ?") // 每天早八点到晚八点,间隔3分钟执行任务 @scheduled(cron="0 0/3 8-20 * * ?") // 每天早八点到晚八点,间隔1分钟执行任务 @scheduled(cron="0 0/1 8-20 * * ?")
3. 启动定时任务
@componentscan @enableautoconfiguration @enablescheduling @configuration public class app { private static final logger logger = loggerfactory.getlogger(app.class); public static void main(string[] args) { springapplication.run(app.class, args); logger.info("oops"); } }
其中 @enablescheduling 注解的作用是发现注解@scheduled的任务并后台执行。
4. 执行结果
2016-02-14-14-51 [pool-2-thread-1] [com.autonavi.task.scheduledtasks] [info] - scheduledtest.executeuploadbacktask 定时任务3:15,name:pool-2-thread-1 定时任务2:15 2016-02-14-14-51 [pool-2-thread-1] [com.autonavi.task.scheduledtasks] [info] - scheduledtest.executeuploadtask 定时任务2:15,name:pool-2-thread-1 定时任务1:15 2016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.scheduledtasks] [info] - scheduledtest.executefiledownloadtask 定时任务1:15,name:pool-2-thread-1 定时任务2:15 2016-02-14-14-52 [pool-2-thread-1] [com.autonavi.task.scheduledtasks] [info] - scheduledtest.executeuploadtask 定时任务2:15,name:pool-2-thread-1 定时任务2:15 2016-02-14-14-53 [pool-2-thread-1] [com.autonavi.task.scheduledtasks] [info] - scheduledtest.executeuploadtask 定时任务2:15,name:pool-2-thread-1
5. 串行任务
上述方法可以实现定时任务,方式也比较简单,不用配置什么文件啥的,但你会发现一个问题,就是不论定时任务被安排在多少个class类中,其依然是单线程执行定时任务(串行任务):
2016-02-14-15-05 [pool-2-thread-1] [com.autonavi.task.scheduledtasks] [info] - scheduledtasks.executeuploadtask 定时任务1:15,name:pool-2-thread-1 定时任务2:15 2016-02-14-15-06 [pool-2-thread-1] [com.autonavi.task.scheduledtasks] [info] - scheduledtest.executeuploadtask 定时任务2:15,name:pool-2-thread-1
上述执行结果中scheduledtest和scheduledtasks是两个独立类,都有各自定时任务,但运行时起thread name都是一样的pool-2-thread-1,因此每个定时任务若要新启一个线程,需要自行编写实现或者配置文件。
springboot定时任务默认单线程,多线程需要自行实现或配置文件
6. 并行任务
有时候会碰到不同业务的定时任务,这时候利用并行任务处理要妥当,采用多线程任务。只需要配置springboot的配置文件:applicationcontext.xml,添加如下内容:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- enables the spring task @scheduled programming model --> <task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven executor="executor" scheduler="scheduler" /> </beans>
添加红框中的内容
同时注意补充title中遗漏的网址。
效果如下,每个调度处理一个任务,每个调度也是一个子线程:
有关executor、scheduler参数的介绍见文中的34.5 the task namespace节。
7. 基于springboot的定时任务工程样例
8. 动态定时任务说明
有时候需要实现动态定时任务,即工程启动后,可以实现启动和关闭任务,同时也可以设置定时计划。这就需要利用到quartz,spring官方对于这个包下面各类的介绍,后续抽空配置下这类业务的实现:
。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java 对10个数进行排序的实现代码
推荐阅读