如何动态添加修改删除定时任务?

如何动态添加修改删除定时任务?那么我们一起看看具体怎么实现,先看下本节大纲:
(1)思路说明;
(2)代码解析;
(3)修改定时任务执行周期特别说明;
接下来看看本节的大纲:(1)思路说明;
(a)首先这里我们需要重新认识一个类threadpooltaskscheduler:线程池任务调度类,能够开启线程池进行任务调度。
(b)threadpooltaskscheduler.schedule()方法会创建一个定时计划scheduledfuture,在这个方法需要添加两个参数,runnable(线程接口类)和crontrigger(定时任务触发器)
(c)在scheduledfuture中有一个cancel可以停止定时任务。
(2)代码解析;
根据以上的思路分析,我们很容易就知道如何进行编码了,先提供代码如下:package com.kfit.task;
import java.util.date;
**import **java.util.concurrent.scheduledfuture;
**import **org.springframework.beans.factory.annotation.autowired;
**import **org.springframework.context.annotation.bean;
importorg.springframework.scheduling.concurrent.threadpooltaskscheduler;
**import **org.springframework.scheduling.support.crontrigger;
**import **org.springframework.stereotype.component;
**import **org.springframework.web.bind.annotation.requestmapping;
**import **org.springframework.web.bind.annotation.restcontroller;
/**
@author angel --守护天使@version v.0.1@date 2017年4月6日*/
@restcontroller
@component
**public **class dynamictask {
@autowired**private** threadpooltaskschedulerthreadpooltaskscheduler;**private **scheduledfuture? future;@bean**public** threadpooltaskschedulerthreadpooltaskscheduler() { **return****new**threadpooltaskscheduler();}@requestmapping(/startcron)**public** string startcron() { future = threadpooltaskscheduler.schedule(**new** myrunnable(), **new** crontrigger(0/5 * * * * *)); system. ***out*** .println(dynamictask.startcron()); **return ** startcron;}@requestmapping(/stopcron)**public** string stopcron() { **if** (future != **null** ) { future.cancel( **true** ); } system. ***out*** .println(dynamictask.stopcron()); **return ** stopcron;}@requestmapping(/changecron10)**public** string startcron10() { stopcron();// 先停止,在开启. future = threadpooltaskscheduler.schedule(**new** myrunnable(), **new** crontrigger(*/10 * * * * *)); system. ***out*** .println(dynamictask.startcron10()); **return ** changecron10;}**private ****class** myrunnable **implements** runnable { @override **public ****void** run() { system. ***out*** .println(dynamictask.myrunnable.run(), + **new** date()); }}}
(a)我们首先了一个类dynamictask;
(b)定义了两个变量,threadpooltaskscheduler和future 其中future是treadpooltaskscheduler执行方法schedule的返回值,主要用于定时任务的停止。
(c)编写启动定时器的方法startcron();
(d)编写停止方法stopcron(),这里编码的时候,需要注意下需要判断下future为null的时候,不然就很容易抛出nullpointerexception;
(e)编写修改定时任务执行周期方法changecron10(),这里的原理就是关闭之前的定时器,创新在创建一个新的定时器。
(3)修改定时任务执行周期特别说明;
在上一篇博客中,我们使用了一种方式通过全局变量的方式修改cron参数的值,那么在这里的我们也是可以这么做的,这里简单提供下思路,大家自己去实现。注意threadpooltaskscheduler中的schedule()第二个参数支持trigger:
threadpooltaskscheduler.schedule([runnable]( in java.lang) arg0, [trigger]( in org.springframework.scheduling)arg1)
那么我们就可以自己定义一个trigger,然后动态进行修改了,这里提供核心的代码如下:
private string cronstr = */5 * * * * *;
@requestmapping(/startcron1)**public** string startcron1(){ system. ***out*** .println(startcron1 >>>>); threadpooltaskscheduler.schedule(**new** myrunnable(), **new** trigger(){ @override **public** datenextexecutiontime(triggercontext triggercontext){ **return****new** crontrigger(cronstr).nextexecutiontime(triggercontext); } }); system. ***out*** .println(startcron1 <<<<); **return ** startcron1;}

盘点智慧医疗现状及行业发展趋势
雷达物位计在水泥生产中的解决方案
莫仕Molex将展示最新汽车电子安全控制系统和汽车电子设备互连解决方案
西瓜探熟电路
电工接线的九大误区及解决方案
如何动态添加修改删除定时任务?
TCL彩电通病的快速排除法
佳能RF 70-200mm f/4L IS USM设计,数字时代最小的720 F4镜头
国网公司专利拥有量连续九年名列央企第一
内容创作者的良配—全方位解读aigo移动固态硬盘S7 Pro
电压比较器中的运放与运算电路中的运放的主要区别?
Palo Alto Networks推出全新“Prisma”云安全套件
三星电子正式宣布两个全新的汽车芯片品牌——Exynos Auto和ISOCELL Auto
电阻应变计式传感器
JAE天线解决方案使客户的无线连接更加便利
数字音频技术的原理是什么 立体声是双声道吗
沈向洋的《浅谈人工智能创造》 探索人工智能和计算机科学的未来
智能网联汽车将开启未来千亿级市场规模
导致超声流量计产生误差的因素有哪些
基于OVM验证平台的IP芯片验证