java定时器动态触发_Spring整合Quartz实现动态定时器

packagecom.ld.nhmz.quartz;importorg.quartz.CronTrigger;importorg.quartz.JobDetail;importorg.quartz.Scheduler;/*** Quartz调度管理器

*

*@authorAdministrator

**/

public classQuartzManager {private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";/*** @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名

*

*@paramsched

* 调度器

*

*@paramjobName

* 任务名

*@paramcls

* 任务

*@paramtime

* 时间设置,参考quartz说明文档

*

* @Title: QuartzManager.java*/

public static void addJob(Scheduler sched, String jobName, @SuppressWarnings("rawtypes") Class cls, String time) {try{

JobDetail jobDetail= new JobDetail(jobName, JOB_GROUP_NAME, cls);//任务名,任务组,任务执行类//触发器

CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);//触发器名,触发器组

trigger.setCronExpression(time);//触发器时间设定

sched.scheduleJob(jobDetail, trigger);//启动

if (!sched.isShutdown()) {

sched.start();

}

}catch(Exception e) {throw newRuntimeException(e);

}

}/*** @Description: 添加一个定时任务

*

*@paramsched

* 调度器

*

*@paramjobName

* 任务名

*@paramjobGroupName

* 任务组名

*@paramtriggerName

* 触发器名

*@paramtriggerGroupName

* 触发器组名

*@paramjobClass

* 任务

*@paramtime

* 时间设置,参考quartz说明文档

*

* @Title: QuartzManager.java*/

public static void addJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName, @SuppressWarnings("rawtypes") Class jobClass, String time) {try{

JobDetail jobDetail= new JobDetail(jobName, jobGroupName, jobClass);//任务名,任务组,任务执行类//触发器

CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);//触发器名,触发器组

trigger.setCronExpression(time);//触发器时间设定

sched.scheduleJob(jobDetail, trigger);

}catch(Exception e) {throw newRuntimeException(e);

}

}/*** @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)

*

*@paramsched

* 调度器

*@paramjobName

*@paramtime

*

* @Title: QuartzManager.java*/@SuppressWarnings("rawtypes")public static voidmodifyJobTime(Scheduler sched, String jobName, String time) {try{

CronTrigger trigger=(CronTrigger) sched.getTrigger(jobName, TRIGGER_GROUP_NAME);if (trigger == null) {return;

}

String oldTime=trigger.getCronExpression();if (!oldTime.equalsIgnoreCase(time)) {

JobDetail jobDetail=sched.getJobDetail(jobName, JOB_GROUP_NAME);

Class objJobClass=jobDetail.getJobClass();

removeJob(sched, jobName);

addJob(sched, jobName, objJobClass, time);

}

}catch(Exception e) {throw newRuntimeException(e);

}

}/*** @Description: 修改一个任务的触发时间

*

*@paramsched

* 调度器 *

*@paramsched

* 调度器

*@paramtriggerName

*@paramtriggerGroupName

*@paramtime

*

* @Title: QuartzManager.java*/

public static voidmodifyJobTime(Scheduler sched, String triggerName, String triggerGroupName, String time) {try{

CronTrigger trigger=(CronTrigger) sched.getTrigger(triggerName, triggerGroupName);if (trigger == null) {return;

}

String oldTime=trigger.getCronExpression();if (!oldTime.equalsIgnoreCase(time)) {

CronTrigger ct=(CronTrigger) trigger;//修改时间

ct.setCronExpression(time);//重启触发器

sched.resumeTrigger(triggerName, triggerGroupName);

}

}catch(Exception e) {throw newRuntimeException(e);

}

}/*** @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名)

*

*@paramsched

* 调度器

*@paramjobName

*

* @Title: QuartzManager.java*/

public static voidremoveJob(Scheduler sched, String jobName) {try{

sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);//停止触发器

sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);//移除触发器

sched.deleteJob(jobName, JOB_GROUP_NAME);//删除任务

} catch(Exception e) {throw newRuntimeException(e);

}

}/*** @Description: 移除一个任务

*

*@paramsched

* 调度器

*@paramjobName

*@paramjobGroupName

*@paramtriggerName

*@paramtriggerGroupName

*

* @Title: QuartzManager.java*/

public static voidremoveJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName) {try{

sched.pauseTrigger(triggerName, triggerGroupName);//停止触发器

sched.unscheduleJob(triggerName, triggerGroupName);//移除触发器

sched.deleteJob(jobName, jobGroupName);//删除任务

} catch(Exception e) {throw newRuntimeException(e);

}

}/*** @Description:启动所有定时任务

*

*@paramsched

* 调度器

*

* @Title: QuartzManager.java*/

public static voidstartJobs(Scheduler sched) {try{

sched.start();

}catch(Exception e) {throw newRuntimeException(e);

}

}/*** @Description:关闭所有定时任务

*

*

*@paramsched

* 调度器

*

*

* @Title: QuartzManager.java*/

public static voidshutdownJobs(Scheduler sched) {try{if (!sched.isShutdown()) {

sched.shutdown();

}

}catch(Exception e) {throw newRuntimeException(e);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值