Quartz,关于任务调度
最近工作遇到这样的问题----用户随时执行监控和停止监控;代码如下,稍后在做注解
--------------------------
package com.hollycrm.businessmonitor.scheduler;
import java.text.ParseException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import com.hollycrm.businessmonitor.dao.CollectCycleDao;
import com.hollycrm.businessmonitor.model.CollectCycle;
/**
* 任务调度管理 类
*/
public class SchedulerManagerImpl implements SchedulerManager {
private static final Log logger = LogFactory.getLog(SchedulerManagerImpl.class);
private Scheduler scheduler;
private CollectCycleDao collectCycleDAO;
private String cronExpression = "0,5,10,15,20,25,30,35,40,45,50,55 * * * * ?"; //默认是每隔5秒钟
/**
* 开始执行调度监控任务
* @throws Exception
*/
public void startMonitor(String objectId,String ruleId) {
//Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
try{
JobDetail jobDetail = new JobDetail(objectId+"_Job","monitorJobs",ExecuteMonitor.class);
CronTrigger cronTrigger = new CronTrigger(objectId+"_Trigger","monitorTriggers");
//获取触发器名字
String triggerName = cronTrigger.getName();
String jobName = jobDetail.getName();
//放到上下文中
scheduler.getContext().put("TriggerName_"+objectId, triggerName);
scheduler.getContext().put("JobName_"+objectId, jobName);
String value = null; //周期值默认是空值
String style = "0"; //周期类别默认是 “秒”
//取出规则对应的周期值
List collectCycleList = collectCycleDAO.findCollectCycle(ruleId);
if(collectCycleList.size()>0){
style = ((CollectCycle)collectCycleList.get(0)).getStyle();
value = ((CollectCycle)collectCycleList.get(0)).getValue();
}
if(value != null ){
cronExpression = convertToCronExpr(value,style);
}
cronTrigger.setCronExpression(cronExpression);
//这里的语句就是已经开始执行任务了;系统在启动时schedule就已经开始运行了
scheduler.scheduleJob(jobDetail, cronTrigger);
if(scheduler.isShutdown()){
scheduler.start();
}
}catch(SchedulerException e){
e.printStackTrace();
throw new RuntimeException("创建监控任务失败!");
}catch(ParseException e){
e.printStackTrace();
throw new RuntimeException("cronExpression设置失败!");
}
}
/**
* 停止监控任务调度
* @throws Exception
*/
public boolean StopMonitor(String objectId) {
try {
if(!scheduler.isShutdown()){
String TriggerName = scheduler.getContext().getString("TriggerName_"+objectId);
//删除指定的scheduler中的trigger
scheduler.unscheduleJob(TriggerName, "monitorTriggers");
logger.info(objectId + "--------此对象的监控已经被关闭!");
return true;
}else{
logger.info("scheduler已经被关闭!不能删除监控任务");
return false;
}
} catch (SchedulerException e) {
e.printStackTrace();
throw new RuntimeException("停止监控任务失败!");
}finally{
try {
scheduler.getContext().remove("TriggerName_" + objectId);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
/**
* 更新监控周期值------------------可以当对象还处在监控的时候就改变这个周期值,考虑在页面用DWR来直接调用此方法
* @param cronExpr
*/
public void updateCronExpression(String objectId,String ruleId) {
try {
if(!scheduler.isShutdown()){
String value = null; //周期值默认是空值
String style = "0"; //周期类别默认是 “秒”
//取出规则对应的周期值
List collectCycleList = collectCycleDAO.findCollectCycle(ruleId);
if(collectCycleList.size()>0){
style = ((CollectCycle)collectCycleList.get(0)).getStyle();
value = ((CollectCycle)collectCycleList.get(0)).getValue();
}
if(value != null ){
cronExpression = convertToCronExpr(value,style);
}
String TriggerName = scheduler.getContext().getString("TriggerName_"+objectId);
CronTrigger cronTrigger = (CronTrigger)scheduler.getTrigger(TriggerName,"monitorTriggers");
//暂停此Trigger
scheduler.pauseTrigger(TriggerName,"monitorTriggers");
logger.info("------------------此监控暂停执行------------------");
if(cronTrigger != null){
CronTrigger cronTrigger1 = new CronTrigger(objectId+"_Trigger1","monitorTriggers");
//在重新把触发器的名称设到上下文中,否则这个新建的触发任务是停不掉的
String triggerName = cronTrigger1.getName();
scheduler.getContext().put("TriggerName_"+objectId, triggerName);
cronTrigger1.setCronExpression(cronExpression);
String jobDetailName = scheduler.getContext().getString("JobName_"+objectId);
JobDetail jobDetail = scheduler.getJobDetail(jobDetailName, "monitorJobs");
cronTrigger1.setJobName(jobDetailName);
cronTrigger1.setJobGroup("monitorJobs");
scheduler.addJob(jobDetail, true);
//把原来的trigger删除掉;加入新的trigger
scheduler.rescheduleJob(TriggerName, "monitorTriggers", cronTrigger1);
//scheduler.scheduleJob(cronTrigger1);
//重新激活此Trigger
//scheduler.resumeTrigger(TriggerName,"monitorTriggers");
//scheduler.scheduleJob(cronTrigger);
logger.info("----------------此监控重新开始执行---------------");
}else{
logger.info("此监控任务不存在或为空!");
}
}else{
logger.info("scheduler已经被关闭!不能更新定时触发时间");
}
} catch (SchedulerException e) {
e.printStackTrace();
throw new RuntimeException("更新定时触发时间失败!");
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException("更新定时触发时间失败!");
}
}
/**
* 转换周期格式为quartz表达式格式------现在的时间表达式都默认是每隔多长时间来触发脚本
* @param value
* @return
*/
private String convertToCronExpr(String value,String style) {
if("0".equals(style)){
cronExpression = "*/" + value + " * * * * ?";
}else if("1".equals(style)){
cronExpression = "0 " + "*/" + value + " * * * ?";
}else if("2".equals(style)){
cronExpression = "0 0 " + "*/" + value + " * * ?";
}
logger.info("---------------------cronExpression=" + cronExpression);
return cronExpression;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void setCollectCycleDAO(CollectCycleDao collectCycleDAO) {
this.collectCycleDAO = collectCycleDAO;
}
}