Upload
hollie-palmer
View
213
Download
0
Embed Size (px)
Citation preview
1
Exploring Quartz Schedulerhttp://quartz-scheduler.org
Orlando Java User GroupZemian Deng2011.04.28
2
Agenda (1.5 hours)
• Introduction (5 mins)• Scheduling Jobs (20 mins)• How job is stored and executed (15 mins)• Scheduler Deployment (20 mins)• Tips for working with quartz API (5 mins)• QA session (25 mins)
3
Introduction: What is Quartz
• Fancier java.util.Timer• Unix CRON replacement• A rich API set to the scheduler• Job data persistence• Job executions control
4
Creating A Job Class
public class SimpleJob implements org.quartz.Job { @Override public void execute(JobExecutionContext ctx) throws JobExecutionException {
System.out.println(“job is running”);
}}
5
Scheduling One Time Jobimport org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SimpleTrigger;import org.quartz.impl.StdSchedulerFactory;
public class QuartzExample { public static void main(String[] args) throws Exception { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = new JobDetail("job1", SimpleJob.class); SimpleTrigger trigger = new SimpleTrigger("job1");
scheduler.scheduleJob(job, trigger);
scheduler.start(); Thread.sleep(3000L); }}
6
More Scheduling Job Examples
• One time job with startTime• Repeat job now• Repeat job with startTime• Repeat job forever• Cron job
7
Introduction: Quartz 2.0 vs 1.8
• Released in March 2011• New API• Java DSL for creating jobs• Database schema changes
http://quartz-scheduler.org/docs/2.0/newInQuartz2.html
8
Job Storages
• In Memory Job Storage - RAMJobStore• JDBC Job Storage - JobStoreTX• Tx DataSource Job Storage - JobStoreCMT• Spring Tx DataSource Job Storage -
LocalDataSourceJobStore
9
Quartz Scheduler Deployment
• Standalone single JVM/Quartz scheduler• Server/Client based via RMI registry• Web Container single JVM/Quartz scheduler– Allow http client to interact with scheduler!
• Clustering Quartz scheduler servers
10
Scheduler Tips #1
SimpleTrigger - Total Run Count = RepeatCount - 1- You able to specify an end date regardless of
repeatCount.- Trigger is NOT sensitive to Daylight Saving
Time (DST).
11
Scheduler Tips #2
CronTrigger• Cron Expression has 6 + 1 optional fields.• Finest field unit is in second. • Trigger is senstive to DST.• Day-Of-Week starts with 1=SUN• You are allow only one Nth day-of-week (‘#’).
12
Scheduler Tips #3
Tired of java.util.Calendar & java.util.Date?
Use org.quartz.TriggerUtils orApache commons-langs DateUtils
13
Scheduler Tips #4
Spring Quartz Scheduler– You can’t disable auto shutdown!– The dataSource can partake Spring Tx Manager
Abstraction.– Job may have access to Spring App Context
through the Scheduler’s Context space.
14
Scheduler Tips #5
When using RMI server mode, there is a bug that can crash your scheduler:
http://jira.terracotta.org/jira/browse/QTZ-135
15
Questions?