基于Spring Task的定时任务调度器实现
在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便。
只要跟需要定时执行的方法加上类似 @Scheduled(cron = "0 1 * * * *") 的注解就可以实现方法的定时执行。
cron 是一种周期的表达式,六位从右至左分别对应的是年、月、日、时、分、秒,数字配合各种通配符可以表达种类丰富的定时执行周期。
/**
* Cron Example patterns:
* <li>"0 0 * * * *" = the top of every hour of every day.</li>
* <li>"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.</li>
* <li>"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.</li>
* <li>"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays</li>
* <li>"0 0 0 25 12 ?" = every Christmas Day at midnight</li>
*/
基于注解的使用案列:
import org.springframework.stereotype.Component; @Component(“task”)
public class Task {
@Scheduled(cron = "0 1 * * * *") // 每分钟执行一次
public void job1() {
System.out.println(“任务进行中。。。”);
}
}
基于注解方式的定时任务,启动会依赖于系统的启动。如果需要通过代码或前台操作触发定时任务,就需要进行包装了。
下面是一个可以直接提供业务代码调用的定时任务调度器。调用 schedule(Runnable task, String cron) 传入要执行的任务
task和定时周期cron就可以了。注:基于注解方式需要在注解扫描范围内。
package com.louis.merak.schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component; @Component
public class MerakTaskScheduler { @Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler; @Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
return new ThreadPoolTaskScheduler();
} /**
* Cron Example patterns:
* <li>"0 0 * * * *" = the top of every hour of every day.</li>
* <li>"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.</li>
* <li>"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.</li>
* <li>"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays</li>
* <li>"0 0 0 25 12 ?" = every Christmas Day at midnight</li>
*/
public void schedule(Runnable task, String cron){
if(cron == null || "".equals(cron)) {
cron = "0 * * * * *";
}
threadPoolTaskScheduler.schedule(task, new CronTrigger(cron));
} /**
* shutdown and init
* @param task
* @param cron
*/
public void reset(){
threadPoolTaskScheduler.shutdown();
threadPoolTaskScheduler.initialize();
} /**
* shutdown before a new schedule operation
* @param task
* @param cron
*/
public void resetSchedule(Runnable task, String cron){
shutdown();
threadPoolTaskScheduler.initialize();
schedule(task, cron);
} /**
* shutdown
*/
public void shutdown(){
threadPoolTaskScheduler.shutdown();
}
}
如果是需要通过前台操作调用RESTful执行定时任务的调度,使用以下Controller即可。
package com.louis.merak.common.schedule; import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class MerakTaskSchedulerController { @Autowired
MerakTaskScheduler taskScheduler; @RequestMapping("/schedule")
public String schedule(@RequestParam String cron) {
if(cron == null) {
cron = "0/5 * * * * *";
}
Runnable runnable = new Runnable() {
public void run() {
String time = new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date());
System.out.println("Test GETaskScheduler Success at " + time);
}
};
taskScheduler.schedule(runnable, cron);
return "Test TaskScheduler Interface.";
}
}
作者:朝雨忆轻尘
出处:https://www.cnblogs.com/xifengxiaoma/
版权所有,欢迎转载,转载请注明原文作者及出处。
基于Spring Task的定时任务调度器实现的更多相关文章
- SpringBoot2 task scheduler 定时任务调度器四种方式
github:https://github.com/chenyingjun/springboot2-task 使用@EnableScheduling方式 @Component @Configurabl ...
- 2. SOFAJRaft源码分析—JRaft的定时任务调度器是怎么做的?
看完这个实现之后,感觉还是要多看源码,多研究.其实JRaft的定时任务调度器是基于Netty的时间轮来做的,如果没有看过Netty的源码,很可能并不知道时间轮算法,也就很难想到要去使用这么优秀的定时调 ...
- 记录对定时任务调度器的小小改进 - API调度计划
之前记录过一篇 [开源一个定时任务调度器 webscheduler],这是一个看似简单的小工具,昨天部署到服务器上开始试用下,听听反馈. 项目经理看过后,立马反馈说这个使用 Cron表达式 的计划太难 ...
- spring任务执行器与任务调度器(TaskExecutor And TaskScheduler)
对于多线程及周期性调度相关的操作,spring框架提供了TaskExecutor和TaskScheduler接口为异步执行和任务调度.并提供了相关实现类给开发者使用.(只记录采用注解的使用形式,对于X ...
- 基于spring的quartz定时框架,实现简单的定时任务功能
在项目中,经常会用到定时任务,这就需要使用quartz框架去进行操作. 今天就把我最近做的个人主页项目里面的定时刷新功能分享一下,很简单. 首先需要配置一个配置文件,因为我是基于spring框架的,所 ...
- 开源一个定时任务调度器 webscheduler
在企业应用中定时任务调度的需求是必不可少的,比如定时同步数据,定时结转数据,定时检测异常等等.公司之前是在使用一款采用.net 开发的windows服务形式的定时程序,基本能满足需求,在一段时间的时候 ...
- 基于Redis实现分布式定时任务调度
项目开发过程中,难免会有许多定时任务的需求进来.如果项目中还没有引入quarzt框架的情况下,我们通常会使用Spring的@Schedule(cron="* * * * *")注解 ...
- spring task 实现定时执行(补充:解决定时任务执行2次问题)
首先在spring-mvc.xml配置头文件引入: xmlns:task="http://www.springframework.org/schema/task" 其次引入task ...
- spring中的定时任务调度用例
在application-quartz.xml配置文件中添加如下配置信息: <!-- Quartz --> <bean id="getSendEmailObject ...
随机推荐
- Amazon成本和产出的衡量方式
Amazon用一种T-Shirt Size 估计的方式来做项目. 产品经理会对每一条需求评估上业务影响力的尺寸,如:XXXL 影响一千万人以上或是可以占到上亿美金的市场,XXL,影响百万用户或是占了千 ...
- 循环读取list 的几种方法?
1.最常用的方法.循环找出该位子的list元素for(int i = 0;i < list.size(); i ++){System.out.println(list.get(i));}2.利用 ...
- Android-JVM中的多线程&垃圾回收
Java语言是为数不多支持多线程技术的编程语言,而这多线程就不得不提到JVM虚拟机 先看代码案例:(JVM收垃圾) package android.java.thread; class Demo { ...
- SMINT:单页网站的免費jQuery插件
最近为了做一个静态网页版的数据报告,不希望花很多时间去设计网页,或者花时间去调整布局,于是找到了一个名为Smint的免費jQuery插件.几乎不需要写什么代码就可以完成一个一页式网站.这非常适合用来制 ...
- 用C#开发的双色球走势图(原创)值得园友拥有
首先声明,个人纯粹无聊之作,不作商业用途. 我相信每个人都拥有一个梦想那就是有朝一日能中500W,这个也一直是我的梦想,并默默每一期双色球或多或少要贡献自己一点点力量,本人并不属于那种铁杆的彩票迷,每 ...
- Nodejs-- web服务器
第一篇关于nodejs的东西,代码在此作为备份. 该代码目前未完成,是一个阻塞式的代码. 1.index.js ar server=require('./server'); var route=req ...
- 申请Let's Encrypt通配符HTTPS证书
./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d "*.xxx.com" --ma ...
- Django:form.save()方法
参考:https://blog.csdn.net/it_yuan/article/details/53580756 背景: 之前的博客是不支持上传文章缩略图的,后来新增了此功能,但是发现修改老的文章时 ...
- c#中在函数后紧跟=>,几个意思,差点懵逼到没有朋友!
以下是一段新建.net core web中的代码: namespace TempCoreApp { public class Program { public static void Main(str ...
- Django思维导图