Summarize the usage experience of the classic quartz regular processing framework. Based on ver. 1.8.0
1. configuration method
The most common method is properties + XML definition.
Java Code
-
- Org. Quartz. scheduler. InstanceName = cms_batch
-
- Org. Quartz. schedid. instanceid = auto
-
-
- Org. Quartz. threadpool.Class= Org. Quartz. simpl. simplethreadpool
-
- Org. Quartz. threadpool. threadcount =5
-
- Org. Quartz. threadpool. threadpriority =5
-
- Org. Quartz. jobstore. misfirethreshold =60000
-
- Org. Quartz. jobstore.Class= Org. Quartz. simpl. ramjobstore
-
-
- Org. Quartz. plugin. trigghistory.Class= Org. Quartz. plugins. History. loggingjobhistoryplugin
-
-
- Org. Quartz. plugin. jobinitializer.Class= Org. Quartz. plugins. xml. xmlschedulingdataprocessorplugin
-
- Org. Quartz. plugin. jobinitializer. filenames = quartz_data.xml
- Org. Quartz. plugin. jobinitializer. failonfilenotfound =True
-
-
- # By default, the system checks whether there are updates or skip addresses.
-
- Org. Quartz. schedcheck. skipupdatecheck =True
Org. quartz. scheduler. instanceName = cms_batchorg.quartz.scheduler.instanceid = autoorg. quartz. threadpool. class = org. quartz. simpl. simplethreadpoolorg. quartz. threadpool. threadcount = 5org. quartz. threadpool. threadpriority = 5org. quartz. jobstore. misfirethreshold = 60000org. quartz. jobstore. class = org. quartz. simpl. ramjobstoreorg. quartz. plugin. trigghistory. class = org. quartz. plugins. history. loggingjobhistorypluginorg. quartz. plugin. jobinitializer. class = org. quartz. plugins. XML. xmlschedulingdataprocessorpluginorg. quartz. plugin. jobinitializer. filenames = quartz_data.xmlorg.quartz.plugin.jobinitializer.failonfilenotfound = true # by default, the system checks whether the network is updated, and the Skip is better than Org. quartz. scheduler. skipupdatecheck = true
Properties defines global information (Global listener is also defined here), XML defines job and trigger information
Java code
- <? XML version ="1.0"Encoding =UTF-8"?>
-
- <Job-scheduling-data xmlns =Http://www.quartz-scheduler.org/xml/JobSchedulingData"
-
- Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"
-
- Xsi: schemalocation =Http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
-
- Version ="1.8">
-
- <Pre-processing-commands>
-
- <Delete-jobs-in-group> * </delete-jobs-in-group> <! -- Clear all jobs in scheduler -->
-
- <Delete-triggers-in-group> * </delete-triggers-in-group> <! -- Clear all triggers in scheduler -->
-
- </Pre-processing-commands>
-
-
- <Processing-directives>
- <Overwrite-existing-data>True</Overwrite-existing-data>
-
- <Ignore-Duplicates>False</Ignore-Duplicates>
-
- </Processing-directives>
-
-
- <Schedule>
-
- <Job>
-
- <Name> foojob </Name>
- <Group> foobatch </group>
-
- <Description> scheduled job running </description>
-
- <Job-Class> XXX. yyy. foojob </job-Class>
-
- <Volatility>False</Volatility>
-
- <Durability>True</Durability>
- <Recover>False</Recover>
-
- </Job>
-
- <Trigger>
-
- <Cron>
-
- <Name> footrigger </Name>
-
- <Group> foobatch </group>
- <Job-Name> foojob </job-Name>
-
- <Job-group> foobatch </job-group>
-
- <Misfire-instruction> misfire_instruction_fire_once_now </misfire-instruction>
-
- <Cron-expression>30/15*13**? </Cron-expression>
- <! -- Second minute hour day month week year -->
-
- </Cron>
-
- </Trigger>
-
- <Job>
-
- <Name> barjob </Name>
-
- <Group> foobatch </group>
- <Description> no trigger job </description>
-
- <Job-Class> XXX. yyy. barjob </job-Class>
-
- <Volatility>False</Volatility>
-
- <Durability>True</Durability>
- <Recover>False</Recover>
-
- </Job>
-
- </Schedule>
-
- </Job-scheduling-data>
<? XML version = "1.0" encoding = "UTF-8"?> <Job-scheduling-data xmlns = "http://www.quartz-scheduler.org/xml/JobSchedulingData" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" version = "1.8"> <pre-processing-commands> <Delete-jobs-in-group> * </delete-jobs-in-group> <! -- Clear all jobs in scheduler --> <Delete-triggers-in-group> * </delete-triggers-in-group> <! -- Clear all triggers in scheduler --> </pre-processing-commands> <processing-direves ves> <overwrite-existing-data> true </overwrite-existing-data> <ignore- duplicates> false </ignore-Duplicates> </processing-direves ves> <schedule> <job> <Name> foojob </Name> <group> foobatch </group> <description> scheduled job running </description> <job-class> XXX. YYY. foojob </job-class> <volatility> false </volatility> <durability> true </du Rability> <recover> false </recover> </job> <trigger> <cron> <Name> footrigger </Name> <group> foobatch </group> <job-name> foojob </job-Name> <job-group> foobatch </job-group> <misfire-instruction> misfire_instruction_fire_once_now </misfire-instruction> <Cron-expression> 30/15*13 **? </Cron-expression> <! -- Second minute hour day month week year --> </cron> </trigger> <job> <Name> barjob </Name> <group> foobatch </group> <description> job without a trigger </description> <job-class> XXX. YYY. barjob </job-class> <volatility> false </volatility> <durability> true </durability> <recover> false </recover> </job> </schedule> </job-scheduling-data>
Combined with J2EE containers, quartzinitializerlistener is provided, which can be set to automatically start with the container. This is very convenient.
Java code
-
- <? XML version ="1.0"Encoding =UTF-8"?>
-
- <Web-app xmlns =Http://java.sun.com/xml/ns/j2ee"
-
- Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"Version ="2.4"
-
- Xsi: schemalocation = "http:// Java.sun.com/xml/ns/j2ee
- HTTP:// Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
-
-
- <Display-Name> Foo batch </display-Name>
-
-
- <Context-param>
-
- <Param-Name> quartz: config-file </param-Name>
-
- <Param-value>/quartz. properties </param-value>
-
- </Context-param>
-
-
- <Listener>
- <Listener-Class>
-
- Org. Quartz. ee. servlet. quartzinitializerlistener
-
- </Listener-Class>
-
- </Listener>
-
-
- </Web-app>
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.4" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-Name> Foo batch </display-Name> <context-param> <param-Name> quartz: config-file </param-Name> <param-value>/quartz. properties </param-value> </context-param> <listener-class> Org. quartz. ee. servlet. quartzinitializerlistener </listener-class> </listener> </Web-app>
2. Job, trigger, listener
The job needs to be started by the trigger and can have multiple listener
Trigger can also have multiple listener
3. Areas to be improved
The trigger is incomplete and only "timed" trigger is provided. Other triggers, such as job completion or completion status, must be implemented by listener.
The configuration is not complete. The configuration file only provides the global listener definition. For job or trigger listenerProgramAdd. In addition, version 1.8 has a bug When configuring a job without a trigger (line 1015 of xmlschedulingdataprocessor requires nullcheck), which directly leads to XML parsing errors. It seems that this built-in plug-in is still very simple.
Plugin and job at the same level(This statement is a bit biased, but produces a similar effect). This means that the configuration file will not be parsed before schedule is started. You can only adjust the job and other content after it is started, and scheduler. the getjobdetail job is obtained through clone, and any modifications to the job are invalid...This is speechless.(The clone method still makes sense. The configured job cannot be changed at will)
Java code
-
- Scheduler. Start ();// Read the configuration file after startup
-
- Jobdetail foojob = scheduler. getjobdetail ("Foojob",
-
- "Foobatch");
-
-
- Foojob. addjoblistener (somelistener );
-
-
- // Obtain the trigger corresponding to the job
-
- Trigger [] triggers = scheduler. gettriggersofjob (foojob. getname (),
-
- Foojob. getgroup ());
-
- Scheduler. addjob (foojob,True);// True indicates replacing an existing job (no associated trigger)
-
-
- // Clear the old Trigger and re-establish the job and trigger Association
-
- For(Trigger: triggers ){
-
- Schedname. reschedulejob (trigger. getname (), trigger. getgroup (), trigger );
-
- }
Scheduler. start (); // read the configuration file and so on after startup jobdetail foojob = scheduler. getjobdetail ("foojob", "foobatch"); foojob. addjoblistener (somelistener); // obtains the triggertrigger [] triggers = schedgger. gettriggersofjob (foojob. getname (), foojob. getgroup (); scheduler. addjob (foojob, true); // true indicates replacing an existing job (no associated trigger) // clear the old Trigger and re-establish the job and trigger association for (trigger: triggers) {scheduler. reschedulejob (trigger. getname (), Trigger. getgroup (), trigger );}
From http://foxswily.iteye.com/blog/683007