Quarz入门(一)
程序员文章站
2022-05-24 19:55:39
...
public class QuartzReport implements Job {
public void execute(JobExecutionContext cntxt) throws JobExecutionException {
System.out.println("杨春龙,太帅了");
System.out.println( " Generating report - "
+ cntxt.getJobDetail().getJobDataMap().get("type")
+ "-"
+ new Date());
}
}
public class TestJob {
public static void main(String[] args) {
try {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
JobDetail jobDetail = new JobDetail( " Income Report " ,
" Report Generation " , QuartzReport. class );
jobDetail.getJobDataMap().put("type" , " FULL " );
CronTrigger trigger = new CronTrigger( " Income Report " ,
" Report Generation " );
/* 每1分钟执行一次 */
trigger.setCronExpression( " 0 0/1 * * * ? " );
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
e.printStackTrace();
}
}
}
从Scheduler工厂中得到Scheduler ,设置JobDetail中的Job等属性,这里设置了jobDetail可以通过DetailDataMap向Job中传递参数,从而执行相应的Job.CronTrigger是一个触发器,设置何时触发等信息,最后Scheduler 设置一个任务[JobDetail,Trigger]