370 likes | 2.39k Views
Andreas Kuhn. Quartz-Job Scheduling Framework. 2. Quartz - History. Founder: James HouseFirst articles about job scheduling in 1998Give it to open source community in 2001 (SourceForge)Widely spread
E N D
1. Quartz – Job Scheduling Framework Quartz – Features, Basic techniques and components
Author: Andreas Kuhn
Date: 16.05.2007
2. Andreas Kuhn Quartz-Job Scheduling Framework 2
3. Andreas Kuhn Quartz-Job Scheduling Framework 3 Quartz - Features Scalability and extendability (Clustering, load balancing, Plugin mechanisms)
Thread pooling
Job persistence (Oracle, DB2, Mysql ...)
Grouping of jobs and triggers
Simple Triggers, Cron Triggers
Working in Java-standalone applications and Web – and J2EE environments
OSWorkflow
4. Andreas Kuhn Quartz-Job Scheduling Framework 4 Quartz – Alternatives (1) Since JDK 1.3 -> Timer and TimerTask class
Uses one thread per task
No persistence, thread pooling, grouping
Just basic functionality for job scheduling; not scalable, extendable
5. Andreas Kuhn Quartz-Job Scheduling Framework 5 Quartz – Alternatives (2) EJB TimerService since EJB 2.1
TimerService for MessageDriven Beans, Entity Beans and stateless session beans
Based on J2EE environment (might be overhead in some cases)
Can‘t work with POJO‘s
No persistence
6. Andreas Kuhn Quartz-Job Scheduling Framework 6 Basic Quartz Components Scheduler
Jobs
JobDetails
Triggers
Listeners
Plugins
7. Andreas Kuhn Quartz-Job Scheduling Framework 7 Quartz - Scheduler Central Component of the Quartz-Framework
Responsibility: administering of triggers and jobs, scheduling
8. Andreas Kuhn Quartz-Job Scheduling Framework 8 Quartz – Job -> Task Implementing JobInterface (Interruptable Job, StatefulJob)
public class TestBatchImport implements StatefulJob …
Implementing just one method:execute
In case of error throwing a JobExecutionException
Parameter: JobExecutionContext
delivers information about the runtimeEnvironment of the job
9. Andreas Kuhn Quartz-Job Scheduling Framework 9 Quartz JobDetails (1) Jobs are described via JobDetail
<job>
<job-detail>
<name>TestBatchImportLJob</name>
<group>xfi-jobs</group>
<description>Batch-Importing into importTable</description>
<job-class>
de.andreas_kuhn.jobs.TestBatchImport
</job-class>
<job-listener-ref>TestBatchImportJobListener</job-listener-ref>
<volatility>false</volatility>
<durability>true</durability>
<recover>false</recover>
<job-data-map allows-transient-data="true">
<entry>
<key>PROCESSING_DATE</key>
<value>dd.mm.yyyy</value>
</entry>
<entry>
<key>EXTRA_RUN</key>
<value>false</value>
</entry>
<entry>
<key>MAX_IMPORT_TIME</key>
<value>4</value>
</entry>
</job-data-map>
</job-detail>
10. Andreas Kuhn Quartz-Job Scheduling Framework 10 Quartz JobDetails (2) Scheduler instantiates jobs and use properties based on JobDetail description
11. Andreas Kuhn Quartz-Job Scheduling Framework 11 JobDetails Table View
12. Andreas Kuhn Quartz-Job Scheduling Framework 12 Quartz – Trigger (1) Implementing Trigger through Java-Interfaces
Schedule the job
Time scheduling of the job
SimpleTrigger
CronTrigger
nthDayTrigger
Calendar (BlockOutTime)
13. Andreas Kuhn Quartz-Job Scheduling Framework 13 Quartz – Cron-Trigger (2) Definition of trigger in job.xml // here cron-trigger
<trigger>
<cron>
<name>TestBatchImportTrigger</name>
<group>xfi-trigger</group>
<job-name>TestBatchImportJob</job-name>
<job-group>xfi-jobs</job-group>
<cron-expression>0 30 1 * * ? -> 01.30 AM
#sec/min/hh/day of month/month/Day of week/year
</cron-expression>
</cron>
</trigger>
14. Andreas Kuhn Quartz-Job Scheduling Framework 14 Quartz – Simple-Trigger (3) <trigger>
<simple>
<name>ERRORJobTrigger</name>
<group>DEFAULT</group>
<trigger-listener-ref>ErrorTriggerListener</trigger-listener-ref>
<job-name>ErrorJob</job-name>
<job-group>DEFAULT</job-group>
<!– alternative format for web-application-> <start-time>2004-02-26T12:26:00</start-time>?
<!-- PM abends 17:10 -->
<start-time>2007-02-09 5:10:00 PM</start-time>
<!-- repeat indefinitely every 10 seconds -->
<!-- create a trigger that fires every 10 seconds forever -->
<!-- <repeat-count>-1</repeat-count> -->
<!-- only 5 times -->
<repeat-count>100</repeat-count>
<!-- <repeat-interval>10000</repeat-interval> -->
<repeat-interval>2000</repeat-interval>
</simple>
</trigger>
15. Andreas Kuhn Quartz-Job Scheduling Framework 15 Quartz – SimpleTrigger - TableView
16. Andreas Kuhn Quartz-Job Scheduling Framework 16 Quartz - Listener Implementing Quartz-Listener Interfaces
TriggerListener
JobListener
SchedulerListener
Hooks, events, called by scheduler during processing of jobs
17. Andreas Kuhn Quartz-Job Scheduling Framework 17 Quartz - Plugins Extendability of quartz with plugins
Java class can implement the interface: org.quartz.spi.SchedulerPluginInterface
Methods: #initialize,start, shutdown
Are connected to quartz via entry in quartz.property file
Methods are called from the framework corresponding to the scheduler calls
18. Andreas Kuhn Quartz-Job Scheduling Framework 18 Important configuration files Quartz.properties (Standard config-file)
Jobs.xml (contains JobDescriptions in xml Format)
Log4j.properties (contains feature list of the log4j – Logging framework
19. Andreas Kuhn Quartz-Job Scheduling Framework 19 Programming example (1) Source from QuartzInitializerServlet //initializing scheduler
....
quartzProps.load(this.getClass().getResourceAsStream("/" + propertyFileName ));
//quartzProps.list(System.out);
logger.info("SchedulerFacotry initialized with: " + propertyFileName);
factory = new StdSchedulerFactory(quartzProps);
// Always want to get the scheduler, even if it isn't starting,
// to make sure it is both initialized and registered.
scheduler = factory.getScheduler();
// Should the Scheduler being started now or later
startOnLoad = cfg
.getInitParameter("start-scheduler-on-load");
/*
* If the "start-scheduler-on-load" init-parameter is not specified,
* the scheduler will be started. This is to maintain backwards
* compatability.
*/
if (shouldPerformStartUpOnLoad()) {
// Start now
scheduler.start();
...
20. Andreas Kuhn Quartz-Job Scheduling Framework 20 Programming example (2) Entries from quartz.property file
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName=jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = false
======================================
# XFI-Plugins
org.quartz.plugin.XFIInitializerPlugin.class=de.andreas_kuhn.plugins.XFIInitializationPlugin
21. Andreas Kuhn Quartz-Job Scheduling Framework 21 Quartz - JobExample ...
import org.apache.log4j.Logger;
import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.quartz.StatefulJob;
import org.quartz.UnableToInterruptJobException;
import de.andreas_kuhnpersistence.dao.XFIMP_Dao;
import de.andreas_kuhnpersistence.dao.XFLEM_Dao;
import de.andreas_kuhnpersistence.dao.XFPROT_Dao;
import de.andreas_kuhnpersistence.models.LEMG_Import;
import de.andreas_kuhnpersistence.models.XFIMPT;
import de.andreas_kuhnpersistence.models.XFPROT;
import de.andreas_kuhnutilities.Constants;
import de.andreas_kuhnutilities.PropertiesSingleton;
public class TestBatchImport implements StatefulJob {
private Logger logger = Logger.getLogger(BatchImportLEMG.class.getName());
private int importCounter = 0;
private int processedCounter = 0;
private Integer numberOfSentencesToBeProcessed = new Integer(0);
private PropertiesSingleton propertySingleton = PropertiesSingleton.getInstance();
private Timestamp startBatchImportAsTimestamp=null;
//------------------------------------------------------------------------------------------------------------
public void execute(JobExecutionContext jce) throws JobExecutionException {
String[] jobListenerNames = jce.getJobDetail().getJobListenerNames();
if(jobListenerNames.length == 0) //started the job from new created JobDetail !!
....
22. Andreas Kuhn Quartz-Job Scheduling Framework 22 Quartz - JobStores RAMJobStore
JobStoreTX
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.useProperties = true
#============================================================================
# Configure Datasources
#=========================================================================
org.quartz.jobStore.dataSource = Z00001
org.quartz.dataSource. Z00001 .jndiURL = jdbc/db_pool
org.quartz.jobStore.tablePrefix = DB.
org.quartz.jobStore.isClustered = false
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.dataSource. Z00001 .maxConnections = 5
org.quartz.dataSource. Z00001 .validationQuery = select count(*) from sysibm.syscolumns;
org.quartz.jobStore.maxMisfiresToHandleAtATime = 20
org.quartz.scheduler.dbFailureRetryInterval=15000
org.quartz.scheduler.wrapExecutionInUserTransaction=true
JobStoreCMT
23. Andreas Kuhn Quartz-Job Scheduling Framework 23 Quartz – Further possibilities Implementing jobs which automatically generates excel reports; sending resulting reports via email
Internal use: deleting temporary files via CronTrigger (Reporting) within J2EE Container
Using OSWorkflow
administrating of quartz through quartz web application
24. Andreas Kuhn Quartz-Job Scheduling Framework 24 Contact
Andreas Kuhn
http://www.andreas-kuhn.de