Summarize some experience of quartz

Source: Internet
Author: User

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
  1. Org. Quartz. scheduler. InstanceName = cms_batch
  2. Org. Quartz. schedid. instanceid = auto
  3. Org. Quartz. threadpool.Class= Org. Quartz. simpl. simplethreadpool
  4. Org. Quartz. threadpool. threadcount =5
  5. Org. Quartz. threadpool. threadpriority =5
  6. Org. Quartz. jobstore. misfirethreshold =60000
  7. Org. Quartz. jobstore.Class= Org. Quartz. simpl. ramjobstore
  8. Org. Quartz. plugin. trigghistory.Class= Org. Quartz. plugins. History. loggingjobhistoryplugin
  9. Org. Quartz. plugin. jobinitializer.Class= Org. Quartz. plugins. xml. xmlschedulingdataprocessorplugin
  10. Org. Quartz. plugin. jobinitializer. filenames = quartz_data.xml
  11. Org. Quartz. plugin. jobinitializer. failonfilenotfound =True
  12. # By default, the system checks whether there are updates or skip addresses.
  13. 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
  1. <? XML version ="1.0"Encoding =UTF-8"?>
  2. <Job-scheduling-data xmlns =Http://www.quartz-scheduler.org/xml/JobSchedulingData"
  3. Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi: schemalocation =Http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
  5. Version ="1.8">
  6. <Pre-processing-commands>
  7. <Delete-jobs-in-group> * </delete-jobs-in-group> <! -- Clear all jobs in scheduler -->
  8. <Delete-triggers-in-group> * </delete-triggers-in-group> <! -- Clear all triggers in scheduler -->
  9. </Pre-processing-commands>
  10. <Processing-directives>
  11. <Overwrite-existing-data>True</Overwrite-existing-data>
  12. <Ignore-Duplicates>False</Ignore-Duplicates>
  13. </Processing-directives>
  14. <Schedule>
  15. <Job>
  16. <Name> foojob </Name>
  17. <Group> foobatch </group>
  18. <Description> scheduled job running </description>
  19. <Job-Class> XXX. yyy. foojob </job-Class>
  20. <Volatility>False</Volatility>
  21. <Durability>True</Durability>
  22. <Recover>False</Recover>
  23. </Job>
  24. <Trigger>
  25. <Cron>
  26. <Name> footrigger </Name>
  27. <Group> foobatch </group>
  28. <Job-Name> foojob </job-Name>
  29. <Job-group> foobatch </job-group>
  30. <Misfire-instruction> misfire_instruction_fire_once_now </misfire-instruction>
  31. <Cron-expression>30/15*13**? </Cron-expression>
  32. <! -- Second minute hour day month week year -->
  33. </Cron>
  34. </Trigger>
  35. <Job>
  36. <Name> barjob </Name>
  37. <Group> foobatch </group>
  38. <Description> no trigger job </description>
  39. <Job-Class> XXX. yyy. barjob </job-Class>
  40. <Volatility>False</Volatility>
  41. <Durability>True</Durability>
  42. <Recover>False</Recover>
  43. </Job>
  44. </Schedule>
  45. </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
  1. <? XML version ="1.0"Encoding =UTF-8"?>
  2. <Web-app xmlns =Http://java.sun.com/xml/ns/j2ee"
  3. Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"Version ="2.4"
  4. Xsi: schemalocation = "http:// Java.sun.com/xml/ns/j2ee
  5. HTTP:// Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
  6. <Display-Name> Foo batch </display-Name>
  7. <Context-param>
  8. <Param-Name> quartz: config-file </param-Name>
  9. <Param-value>/quartz. properties </param-value>
  10. </Context-param>
  11. <Listener>
  12. <Listener-Class>
  13. Org. Quartz. ee. servlet. quartzinitializerlistener
  14. </Listener-Class>
  15. </Listener>
  16. </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
  1. Scheduler. Start ();// Read the configuration file after startup
  2. Jobdetail foojob = scheduler. getjobdetail ("Foojob",
  3. "Foobatch");
  4. Foojob. addjoblistener (somelistener );
  5. // Obtain the trigger corresponding to the job
  6. Trigger [] triggers = scheduler. gettriggersofjob (foojob. getname (),
  7. Foojob. getgroup ());
  8. Scheduler. addjob (foojob,True);// True indicates replacing an existing job (no associated trigger)
  9. // Clear the old Trigger and re-establish the job and trigger Association
  10. For(Trigger: triggers ){
  11. Schedname. reschedulejob (trigger. getname (), trigger. getgroup (), trigger );
  12. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.