Java作业调度之Quartz

Quartz 是一个完全由java编写的开源作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz 允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz 的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业。

由James House创建并最初于2001年春天被加入sourceforge工程。之后归入OpenSymphony开源组织(2010年11月份关闭)。Terracotta公司在2009年收购了著名的Java开源缓存项目Ehcache以及Java任务调度项目Quartz。

http://quartz-scheduler.org/downloads/catalog

版本:Quartz 2.1.7

1、开始时间 startAt
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
Date runTime = DateBuilder.evenSecondDate(new Date());
JobDetail job = JobBuilder.newJob(Job1.class).withIdentity("job1", "group1").build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group1")
		.startAt(runTime)
		.build();		
sched.scheduleJob(job, trigger);
sched.start();


2、简单触发器 withSchedule
SimpleScheduleBuilder.simpleSchedule() 可以设置间隔、重复次数
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Job2.class).withIdentity("job2", "group2").build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger2","group2")
		.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever())
		.build();
sched.scheduleJob(job, trigger);
sched.start();


3、Cron触发器
CronScheduleBuilder.cronSchedule("0/5 * * * * ?") 使用Unix cron表达式
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Job3.class).withIdentity("job3", "group3").build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger3","group3")
		.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
		.build();
sched.scheduleJob(job, trigger);
sched.start();


4、结束时间 endAt
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
Date runTime = DateBuilder.evenSecondDate(new Date());
Date endTime = DateBuilder.evenMinuteDate(new Date());
JobDetail job = JobBuilder.newJob(Job4.class).withIdentity("job4", "group4").build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger4","group4")
		.startAt(runTime)
		.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever())
		.endAt(endTime)
		.build();
sched.scheduleJob(job, trigger);
sched.start();


5、作业监听器 addJobListener
JobKey jobKey = new JobKey("dummyJobName", "group5");
JobDetail job = JobBuilder.newJob(Job5.class).withIdentity(jobKey).build();

Trigger trigger = TriggerBuilder.newTrigger().withIdentity(
		"dummyTriggerName", "group5").withSchedule(
		CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

Scheduler scheduler = new StdSchedulerFactory().getScheduler();

// Listener attached to jobKey
scheduler.getListenerManager().addJobListener(new Job5Listener(),
		KeyMatcher.keyEquals(jobKey));

// Listener attached to group named "group 1" only.
// scheduler.getListenerManager().addJobListener(
// new HelloJobListener(), GroupMatcher.jobGroupEquals("group1")
// );

scheduler.start();
scheduler.scheduleJob(job, trigger);


6、启动多个作业 scheduleJob
JobKey jobKeyA = new JobKey("job6A", "group6");
JobDetail jobA = JobBuilder.newJob(Job6A.class).withIdentity(jobKeyA).build();

JobKey jobKeyB = new JobKey("job6B", "group6");
JobDetail jobB = JobBuilder.newJob(Job6B.class).withIdentity(jobKeyB).build();

JobKey jobKeyC = new JobKey("job6C", "group6");
JobDetail jobC = JobBuilder.newJob(Job6C.class).withIdentity(jobKeyC).build();

Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity(
		"dummyTriggerName1", "group6").withSchedule(
		CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity(
		"dummyTriggerName2", "group6").withSchedule(
		CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

Trigger trigger3 = TriggerBuilder.newTrigger().withIdentity(
		"dummyTriggerName3", "group6").withSchedule(
		CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

Scheduler scheduler = new StdSchedulerFactory().getScheduler();

scheduler.start();
scheduler.scheduleJob(jobA, trigger1);
scheduler.scheduleJob(jobB, trigger2);
scheduler.scheduleJob(jobC, trigger3);


7、列举所有作业 getJobKeys
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
for (String groupName : scheduler.getJobGroupNames()) {
	for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
		String jobName = jobKey.getName();
		String jobGroup = jobKey.getGroup();
		List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
		Date nextFireTime = triggers.get(0).getNextFireTime();
		System.out.println("[jobName] : " + jobName + " [groupName] : " + jobGroup + " - " + nextFireTime);
	}
}


8、手动触发作业 triggerJob
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
Date runTime = DateBuilder.evenSecondDate(new Date());
JobDetail job = JobBuilder.newJob(Job8.class).withIdentity("job8", "group8").build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger8","group8")
		.startAt(runTime)
		.build();		
sched.scheduleJob(job, trigger);
sched.start();

sched.triggerJob(new JobKey("job8", "group8"));
sched.triggerJob(new JobKey("job8", "group8"));


9、传递参数 usingJobData
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
Date runTime = DateBuilder.evenSecondDate(new Date());
JobDetail job = JobBuilder.newJob(Job9.class).withIdentity("job9", "group9")
		.usingJobData("jobSays", "Hello Quartz!")
		.build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger9","group9")
		.startAt(runTime)
		.build();
sched.scheduleJob(job, trigger);
sched.start();


10、取消/删除作业
// removes the given trigger
sched.unscheduleJob(new TriggerKey("job10", "group10"));	
// removes all triggers to the given job
sched.deleteJob(new JobKey("job10", "group10"));


11、作业出错时自动再执行

一直执行到成功:
JobExecutionException e2 = new JobExecutionException(e);
// fire it again
e2.setRefireImmediately(true);
throw e2;


执行n次后停止:
// allow 5 retries
if (count >= 5) {
	JobExecutionException e = new JobExecutionException(
			"Retries exceeded");
	//make sure it doesn't run again
	e.setUnscheduleAllTriggers(true);
	throw e;
}


12、与Spring等框架的集成
略...

JWatch - A Quartz Monitor
http://code.google.com/p/jwatch/
Java作业调度之Quartz
Java作业调度之Quartz

Cron 表达式生成器 CronMaker
Java作业调度之Quartz

你可能感兴趣的:(quartz)