总结一下经典的定时处理框架quartz的使用经验。基于 Ver. 1.8.0
1.配置方式
最常用的方式properties + xml定义
- org.quartz.scheduler.instanceName = CMS_Batch
- org.quartz.scheduler.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
- #默认会联网检查是否有更新,还是skip为好
- org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = CMS_Batch org.quartz.scheduler.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 #默认会联网检查是否有更新,还是skip为好 org.quartz.scheduler.skipUpdateCheck = true
properties定义全局信息(全局Listener也在这定义),xml定义Job、trigger信息
- <?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>定时运行Job</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>
- <!-- 秒 分 时 日 月 星期 年 -->
- </cron>
- </trigger>
- <job>
- <name>barJob</name>
- <group>FooBatch</group>
- <description>无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-directives> <overwrite-existing-data>true</overwrite-existing-data> <ignore-duplicates>false</ignore-duplicates> </processing-directives> <schedule> <job> <name>FooJob</name> <group>FooBatch</group> <description>定时运行Job</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> <!-- 秒 分 时 日 月 星期 年 --> </cron> </trigger> <job> <name>barJob</name> <group>FooBatch</group> <description>无trigger的Job</description> <job-class>xxx.yyy.barJob</job-class> <volatility>false</volatility> <durability>true</durability> <recover>false</recover> </job> </schedule> </job-scheduling-data>
与J2ee容器结合,提供QuartzInitializerListener,可设定随容器自动启动,这个很方便
- <?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> <listener-class> org.quartz.ee.servlet.QuartzInitializerListener </listener-class> </listener> </web-app>
2.Job、Trigger、Listener
Job需要由Trigger启动,可以有多个Listener
Trigger同样可以有多个Listener
3.有待改进的地方
Trigger不完善,只提供了“定时”Trigger,其他诸如基于Job完成与否或完成状态的Trigger需要自己靠Listener实现。
配置不完善,配置文件只提供全局Listener定义,针对Job或Trigger的Listener要程序添加,另外1.8版本在配置无Trigger的Job时有bug(XMLSchedulingDataProcessor 1015行需要nullcheck),直接导致xml解析错误,感觉这自带的plugin还很简陋。
plugin和Job同级别 ,(这个说法有点偏颇,但是产生的效果类似)这意味着在schedule启动前是不会解析配置文件的,只能在启动后调整Job等等内容,同时scheduler.getJobDetail取的Job是通过clone方式取得的,任何对Job的修改都无效...这个很无语 (clone的方式还是有道理的,已经设置好的Job不能随便改动)
- scheduler.start(); //启动后读取配置文件等等
- JobDetail fooJob = scheduler.getJobDetail("FooJob",
- "FooBatch");
- fooJob.addJobListener(someListener);
- //取Job对应的Trigger
- Trigger[] triggers = scheduler.getTriggersOfJob(fooJob.getName(),
- fooJob.getGroup());
- scheduler.addJob(fooJob, true); //true指明替换已有Job(没有关联Trigger)
- //清除旧Trigger,重新建立Job和Trigger关联
- for (Trigger trigger : triggers) {
- scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
- }
scheduler.start(); //启动后读取配置文件等等 JobDetail fooJob = scheduler.getJobDetail("FooJob", "FooBatch"); fooJob.addJobListener(someListener); //取Job对应的Trigger Trigger[] triggers = scheduler.getTriggersOfJob(fooJob.getName(), fooJob.getGroup()); scheduler.addJob(fooJob, true); //true指明替换已有Job(没有关联Trigger) //清除旧Trigger,重新建立Job和Trigger关联 for (Trigger trigger : triggers) { scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(), trigger); }
本文来自博客园,作者:茄子_2008,转载请注明原文链接:https://www.cnblogs.com/xd502djj/archive/2012/03/23/2413502.html