SimpleTriggerBean: 简单触发器.
startDelay: 指点一次执行时的延迟.
jobDetail: 任务详情,不解释
jobDataAsMap:设置任务中的JobDataMap
repeatCount: 重复次数[配置为0时执行一次]
repeatInterval: 两次执行时的间隔
timesTriggered: 执行次数[没用过]
(一般的任务定时调度使用SimpleTrigger就够了).
如何实现动态定时程序,比如,我设置A任务在2012年9月28日完成,B任务在2012年9月30日完成,如果到期还没有完成则发个邮件给我.
设计思路,创建一个方法,然后在自己的Scheduler中,动态添加任务.
Quartz任务本身不带状态,比如A任务,只有A的的功能,他不带有2012-09-28,那么必须动态生成一个触发器.还有一件事,任务有没有完成当然需要任务监听器了,监听完成了不需要发,没有完成则发邮件.
于是如下:
1. 首先定义任务A
package test.quartz;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
public class QuartzReport implements StatefulJob {
public void execute(JobExecutionContext cntxt) throws JobExecutionException {
System.out.println("杨春龙,太帅了");
System.out.println("不是我说的");
System.out.println( " Generating report - "
+ cntxt.getJobDetail().getJobDataMap().get("type")
+ "-"
+ new Date());
JobDataMap jobDataMap = cntxt.getJobDetail().getJobDataMap();
jobDataMap.put("success", true);
}
}
任务2:
package test.quartz;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class QuartzReport1 implements Job {
public void execute(JobExecutionContext cntxt) throws JobExecutionException {
System.out.println("杨春龙,你帅我也帅");
System.out.println( " Generating report - "
+ cntxt.getJobDetail().getJobDataMap().get("type")
+ "-"
+ new Date());
JobDataMap jobDataMap = cntxt.getJobDetail().getJobDataMap();
jobDataMap.put("success", false);
}
}
2. 定义监听器:
package test.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.listeners.JobListenerSupport;
public class JobListener extends JobListenerSupport {
public String getName() {
// TODO Auto-generated method stub
return "JobListener";
}
public void jobWasExecuted(JobExecutionContext context,
JobExecutionException jobException) {
Boolean flag = (Boolean) context.getJobDetail().getJobDataMap().get("success");
if(!flag){
System.out.println("拉出去枪BI了,任务没完成");
}else{
System.out.println("好孩子,去睡觉吧");
}
}
}
3. 已经完好了,因为触发器是需要动态生成执行的,代码如下:
package test.quartz;
import java.util.Calendar;
import java.util.Date;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.listeners.JobListenerSupport;
import org.springframework.scheduling.quartz.SimpleTriggerBean;
public class TestJob3 {
/**
* 每一个Scheduler都是一个线程,外部程序如何与线程进行交互
*
* @param args
*/
public static void main(String[] args) {
TestJob3 test = new TestJob3();
SchedulerFactory sf = new StdSchedulerFactory();
JobListener jobListener = new JobListener();
/*
* 任务A详情1如下
*/
String jobName="job";
String jobGroup="job_group";
Class jobClass= QuartzReport.class;
String triggerName="triger";
String triggerGroup = "tirger_group";
long cc=1000l;//2012-09-28
int repeat=0;
test.createScheduler(sf, jobName, jobGroup, jobClass, triggerName, triggerGroup, cc,repeat,jobListener);
/*
* 任务B详情1如下
*/
String jobName1="job1";
String jobGroup1="job_group";
Class jobClass1= QuartzReport1.class;
String triggerName1="triger1";
String triggerGroup1 = "tirger_group";
long cc1=1000*60l;//2012-09-30
int repeat1=0;
test.createScheduler(sf, jobName1, jobGroup1, jobClass1, triggerName1, triggerGroup1, cc1,repeat1,jobListener);
}
/**
* jobGroup 允许为null,Scheduler.DEFAULT_GROUP
* @param scheduler
* @param jobName
* @param jobGroup
* @param jobclass
*/
public void createScheduler(SchedulerFactory sf,
String jobName,String jobGroup,Class jobclass,
String triggerName,String triggerGroup,long c,int repeat,JobListenerSupport jobListener){
JobDetail jobDetail = new JobDetail(jobName,jobGroup,jobclass);
jobDetail.addJobListener("JobListener");
SimpleTrigger trigger = new SimpleTrigger(triggerName,triggerGroup);
trigger.setStartTime(new Date(System.currentTimeMillis() + c));//设置开始时间
//trigger.setStartDelay(c);//设置延迟时间
//trigger.setRepeatInterval(c);//设置间隔时间
trigger.setRepeatCount(repeat);//设置循环次数
try {
Scheduler scheduler = sf.getScheduler();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.addJobListener(jobListener);//添加监听
scheduler.start();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}