[b]EmailReportJob[/b]类执行一个简单打印功能,这里有一个参数就name,这就是我们要执行被调度程序
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class EmailReportJob extends QuartzJobBean{
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
JobDataMap data = context.getJobDetail().getJobDataMap();
String s = (String) data.get("name");
System.out.println("Hello "+s);
}
}
[b]SchedulerImpl[/b]就是我们调度程序
import java.text.ParseException;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class SchedulerImpl {
Scheduler sh;
public void scheduler() throws SchedulerException, ParseException{
//create SchedulerFactory创建调度工厂
SchedulerFactory sf = new StdSchedulerFactory();
//pass SchedulerFactory get Scheduler 得到调度类
sh = sf.getScheduler();
//add EmailReportJob.class build JobDetail生成工作细节就是调度EmailReportJob类(参数是名字,组,调类)
JobDetail job = new JobDetail("LB","HOME", EmailReportJob.class);
//set EmailReportJob.class parameter传递参数给被调度类(EmailReportJob)
job.getJobDataMap().put("name", "LB");
//get cron's trigger 创建触发器
CronTrigger cronTrigger = getCronTrigger();
//schedule job and trigger 将调度工作和触发器一起放入调度任务中
sh.scheduleJob(job, cronTrigger);
//start scheduler启动
sh.start();
}
public CronTrigger getCronTrigger() throws ParseException{
//create cron's trigger 创建触发器
CronTrigger cronTrigger = new CronTrigger("cronTrigger", "test");
//create cron's expression 设置触发条件如不理解请参照spring 定时器文章中的介绍
CronExpression cexp = new CronExpression("10 * * * * ?");
//set cron's expression
cronTrigger.setCronExpression(cexp);
return cronTrigger;
}
public void deleteSchedule() throws SchedulerException {
sh.unscheduleJob("cronTrigger", "test");
}
public static void main(String[] args) throws SchedulerException, ParseException, InterruptedException{
new SchedulerImpl().scheduler();启动定时器
}
}
我们将在每分钟的第10秒看到一次输出,一个简单定时器灵活应用完成
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class EmailReportJob extends QuartzJobBean{
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
JobDataMap data = context.getJobDetail().getJobDataMap();
String s = (String) data.get("name");
System.out.println("Hello "+s);
}
}
[b]SchedulerImpl[/b]就是我们调度程序
import java.text.ParseException;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class SchedulerImpl {
Scheduler sh;
public void scheduler() throws SchedulerException, ParseException{
//create SchedulerFactory创建调度工厂
SchedulerFactory sf = new StdSchedulerFactory();
//pass SchedulerFactory get Scheduler 得到调度类
sh = sf.getScheduler();
//add EmailReportJob.class build JobDetail生成工作细节就是调度EmailReportJob类(参数是名字,组,调类)
JobDetail job = new JobDetail("LB","HOME", EmailReportJob.class);
//set EmailReportJob.class parameter传递参数给被调度类(EmailReportJob)
job.getJobDataMap().put("name", "LB");
//get cron's trigger 创建触发器
CronTrigger cronTrigger = getCronTrigger();
//schedule job and trigger 将调度工作和触发器一起放入调度任务中
sh.scheduleJob(job, cronTrigger);
//start scheduler启动
sh.start();
}
public CronTrigger getCronTrigger() throws ParseException{
//create cron's trigger 创建触发器
CronTrigger cronTrigger = new CronTrigger("cronTrigger", "test");
//create cron's expression 设置触发条件如不理解请参照spring 定时器文章中的介绍
CronExpression cexp = new CronExpression("10 * * * * ?");
//set cron's expression
cronTrigger.setCronExpression(cexp);
return cronTrigger;
}
public void deleteSchedule() throws SchedulerException {
sh.unscheduleJob("cronTrigger", "test");
}
public static void main(String[] args) throws SchedulerException, ParseException, InterruptedException{
new SchedulerImpl().scheduler();启动定时器
}
}
我们将在每分钟的第10秒看到一次输出,一个简单定时器灵活应用完成