@JFinal 你好,想跟你请教个问题:quartz和jfinal如何做定时任务的动态控制?比如任务暂停啊,添加啊之类。或者其他的办法,绝望的扒皮分享的那个需要去配置cron表达式,很死板啊
该喝喝
我也在研究类似功能的实现方法,可是无奈自己写的jfinal插件类总是报奇怪的错误
回复引入jar,调用我给的这些方法就够了
这个是quartz的调度管理类,未进行持久化的一个工具类,cron应该也是和这个类似,参考下
/**
* @Description: * *QuartzManager.java *@Package com.joyce.quartz *Copyright (c) 2014 * *@author Comsys-LZP * @date 2014-6-26 下午03:15:52 * @version V2.0 */package com.joyce.quartz;
import org.quartz.CronTrigger;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerFactory;import org.quartz.impl.StdSchedulerFactory;
/** * @Description: 定时任务管理类 * * @ClassName: QuartzManager * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-6-26 下午03:15:52 * @version V2.0 */public class QuartzManager {private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
/*** @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名* * @param jobName* 任务名* @param cls* 任务* @param time* 时间设置,参考quartz说明文档* * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:47:44* @version V2.0*/@SuppressWarnings("unchecked")public static void addJob(String jobName, Class cls, String time) {try {Scheduler sched = gSchedulerFactory.getScheduler();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 new RuntimeException(e);}}
/*** @Description: 添加一个定时任务* * @param jobName* 任务名* @param jobGroupName* 任务组名* @param triggerName* 触发器名* @param triggerGroupName* 触发器组名* @param jobClass* 任务* @param time* 时间设置,参考quartz说明文档* * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:48:15* @version V2.0*/@SuppressWarnings("unchecked")public static void addJob(String jobName, String jobGroupName,String triggerName, String triggerGroupName, Class jobClass,String time) {try {Scheduler sched = gSchedulerFactory.getScheduler();JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类// 触发器CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组trigger.setCronExpression(time);// 触发器时间设定sched.scheduleJob(jobDetail, trigger);} catch (Exception e) {throw new RuntimeException(e);}}
/*** @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)* * @param jobName* @param time* * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:49:21* @version V2.0*/@SuppressWarnings("unchecked")public static void modifyJobTime(String jobName, String time) {try {Scheduler sched = gSchedulerFactory.getScheduler();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(jobName);addJob(jobName, objJobClass, time);}} catch (Exception e) {throw new RuntimeException(e);}}
/*** @Description: 修改一个任务的触发时间* * @param triggerName* @param triggerGroupName* @param time* * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:49:37* @version V2.0*/public static void modifyJobTime(String triggerName,String triggerGroupName, String time) {try {Scheduler sched = gSchedulerFactory.getScheduler();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 new RuntimeException(e);}}
/*** @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名)* * @param jobName* * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:49:51* @version V2.0*/public static void removeJob(String jobName) {try {Scheduler sched = gSchedulerFactory.getScheduler();sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务} catch (Exception e) {throw new RuntimeException(e);}}
/*** @Description: 移除一个任务* * @param jobName* @param jobGroupName* @param triggerName* @param triggerGroupName* * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:50:01* @version V2.0*/public static void removeJob(String jobName, String jobGroupName,String triggerName, String triggerGroupName) {try {Scheduler sched = gSchedulerFactory.getScheduler();sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器sched.deleteJob(jobName, jobGroupName);// 删除任务} catch (Exception e) {throw new RuntimeException(e);}}
/*** @Description:启动所有定时任务* * * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:50:18* @version V2.0*/public static void startJobs() {try {Scheduler sched = gSchedulerFactory.getScheduler();sched.start();} catch (Exception e) {throw new RuntimeException(e);}}
/*** @Description:关闭所有定时任务* * * @Title: QuartzManager.java* @Copyright: Copyright (c) 2014* * @author Comsys-LZP* @date 2014-6-26 下午03:50:26* @version V2.0*/public static void shutdownJobs() {try {Scheduler sched = gSchedulerFactory.getScheduler();if (!sched.isShutdown()) {sched.shutdown();}} catch (Exception e) {throw new RuntimeException(e);}}}
这个问答中我给出了 cron4j 的实现代码,以及 cron 表达式的基本用法:http://www.oschina.net/question/217962_2187213 ,copy 下来直接用。
通常来说使用 cron 表达式就可以非常灵活地调度任务了,如果非要在运行过程中动态添加任务、停止调度器等操作,只需要在上面代码的基础上,用一个工具类管理起来,动态的 start()、stop 相关插件,记得搞定后回来分享一下。
i see.thanks
可以自己根据需要实现呐,ext那个是常规静态job配置,想动态控制任务,简单来说任务(配置)可修改化,然后根据quartz中的Scheduler API 如delete pause add等操作任务以及获得相应任务的执行状态。也有很多人实现了,搜一下借阅一番~
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(7)
该喝喝
我也在研究类似功能的实现方法,可是无奈自己写的jfinal插件类总是报奇怪的错误
回复
引入jar,调用我给的这些方法就够了
这个是quartz的调度管理类,未进行持久化的一个工具类,cron应该也是和这个类似,参考下
/**
* @Description:
*
*
QuartzManager.java
*
@Package com.joyce.quartz
*
Copyright (c) 2014
*
*
@author Comsys-LZP
* @date 2014-6-26 下午03:15:52
* @version V2.0
*/
package com.joyce.quartz;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
/**
* @Description: 定时任务管理类
*
* @ClassName: QuartzManager
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:15:52
* @version V2.0
*/
public class QuartzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
/**
* @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param jobName
* 任务名
* @param cls
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:47:44
* @version V2.0
*/
@SuppressWarnings("unchecked")
public static void addJob(String jobName, Class cls, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
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 new RuntimeException(e);
}
}
/**
* @Description: 添加一个定时任务
*
* @param jobName
* 任务名
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param triggerGroupName
* 触发器组名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:48:15
* @version V2.0
*/
@SuppressWarnings("unchecked")
public static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, Class jobClass,
String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName
* @param time
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:49:21
* @version V2.0
*/
@SuppressWarnings("unchecked")
public static void modifyJobTime(String jobName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
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(jobName);
addJob(jobName, objJobClass, time);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 修改一个任务的触发时间
*
* @param triggerName
* @param triggerGroupName
* @param time
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:49:37
* @version V2.0
*/
public static void modifyJobTime(String triggerName,
String triggerGroupName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
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 new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:49:51
* @version V2.0
*/
public static void removeJob(String jobName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器
sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器
sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description: 移除一个任务
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:50:01
* @version V2.0
*/
public static void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
sched.deleteJob(jobName, jobGroupName);// 删除任务
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:启动所有定时任务
*
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:50:18
* @version V2.0
*/
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @Description:关闭所有定时任务
*
*
* @Title: QuartzManager.java
* @Copyright: Copyright (c) 2014
*
* @author Comsys-LZP
* @date 2014-6-26 下午03:50:26
* @version V2.0
*/
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
这个问答中我给出了 cron4j 的实现代码,以及 cron 表达式的基本用法:http://www.oschina.net/question/217962_2187213 ,copy 下来直接用。
通常来说使用 cron 表达式就可以非常灵活地调度任务了,如果非要在运行过程中动态添加任务、停止调度器等操作,只需要在上面代码的基础上,用一个工具类管理起来,动态的 start()、stop 相关插件,记得搞定后回来分享一下。
i see.thanks
可以自己根据需要实现呐,ext那个是常规静态job配置,想动态控制任务,简单来说任务(配置)可修改化,然后根据quartz中的Scheduler API 如delete pause add等操作任务以及获得相应任务的执行状态。也有很多人实现了,搜一下借阅一番~