• 总结几点Quartz的经验


    总结一下经典的定时处理框架quartz的使用经验。基于 Ver. 1.8.0

      1.配置方式

            最常用的方式properties + xml定义

    Java代码 复制代码 收藏代码
    1. org.quartz.scheduler.instanceName = CMS_Batch   
    2. org.quartz.scheduler.instanceId = AUTO   
    3.   
    4. org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool   
    5. org.quartz.threadPool.threadCount = 5  
    6. org.quartz.threadPool.threadPriority = 5  
    7.   
    8. org.quartz.jobStore.misfireThreshold = 60000  
    9. org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore   
    10.   
    11. org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin   
    12.   
    13. org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin   
    14. org.quartz.plugin.jobInitializer.fileNames = quartz_data.xml   
    15. org.quartz.plugin.jobInitializer.failOnFileNotFound = true  
    16.   
    17. #默认会联网检查是否有更新,还是skip为好   
    18. 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信息

    Java代码 复制代码 收藏代码
    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.        
    7.     <pre-processing-commands>   
    8.         <delete-jobs-in-group>*</delete-jobs-in-group>  <!-- clear all jobs in scheduler -->   
    9.         <delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler -->   
    10.     </pre-processing-commands>   
    11.        
    12.     <processing-directives>   
    13.         <overwrite-existing-data>true</overwrite-existing-data>   
    14.         <ignore-duplicates>false</ignore-duplicates>    
    15.     </processing-directives>   
    16.        
    17.     <schedule>   
    18.         <job>   
    19.             <name>FooJob</name>   
    20.             <group>FooBatch</group>   
    21.             <description>定时运行Job</description>   
    22.             <job-class>xxx.yyy.FooJob</job-class>   
    23.             <volatility>false</volatility>   
    24.             <durability>true</durability>   
    25.             <recover>false</recover>   
    26.         </job>   
    27.         <trigger>   
    28.             <cron>   
    29.                 <name>FooTrigger</name>   
    30.                 <group>FooBatch</group>   
    31.                 <job-name>FooJob</job-name>   
    32.                 <job-group>FooBatch</job-group>   
    33.                 <misfire-instruction>MISFIRE_INSTRUCTION_FIRE_ONCE_NOW</misfire-instruction>   
    34.                 <cron-expression>30/15 * 13 * * ?</cron-expression>   
    35.                 <!-- 秒 分 时 日 月 星期 年 -->   
    36.             </cron>   
    37.         </trigger>   
    38.         <job>   
    39.             <name>barJob</name>   
    40.             <group>FooBatch</group>   
    41.             <description>无trigger的Job</description>   
    42.             <job-class>xxx.yyy.barJob</job-class>   
    43.             <volatility>false</volatility>   
    44.             <durability>true</durability>   
    45.             <recover>false</recover>   
    46.         </job>   
    47.     </schedule>       
    48. </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,可设定随容器自动启动,这个很方便

    Java代码 复制代码 收藏代码
    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.   
    7.   <display-name>Foo Batch</display-name>   
    8.   
    9.   <context-param>   
    10.     <param-name>quartz:config-file</param-name>   
    11.     <param-value>/quartz.properties</param-value>   
    12.   </context-param>   
    13.   
    14.   <listener>   
    15.     <listener-class>   
    16.        org.quartz.ee.servlet.QuartzInitializerListener   
    17.     </listener-class>   
    18.   </listener>   
    19.   
    20.   </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不能随便改动)

    Java代码 复制代码 收藏代码
    1. scheduler.start(); //启动后读取配置文件等等   
    2.   
    3. JobDetail fooJob = scheduler.getJobDetail("FooJob",   
    4.         "FooBatch");   
    5.   
    6. fooJob.addJobListener(someListener);   
    7.   
    8. //取Job对应的Trigger   
    9. Trigger[] triggers = scheduler.getTriggersOfJob(fooJob.getName(),   
    10.         fooJob.getGroup());   
    11.   
    12. scheduler.addJob(fooJob, true); //true指明替换已有Job(没有关联Trigger)   
    13.   
    14. //清除旧Trigger,重新建立Job和Trigger关联   
    15. for (Trigger trigger : triggers) {   
    16.     scheduler.rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);   
    17. }   
    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);
    } 

    摘自http://foxswily.iteye.com/blog/683007 

  • 相关阅读:
    LeetCode:Multiply Strings
    c/c++ 输入输出缓冲区
    Scala access modifiers and qualifiers in detail
    PUT vs POST in REST
    Redis优化总结
    硬中断和软中断(转)
    Redis监控技巧(转)
    Understanding mysql max_connect_errors
    TCP : two different sockets sharing a port?
    libevent 安装异常
  • 原文地址:https://www.cnblogs.com/xd502djj/p/2413502.html
Copyright © 2020-2023  润新知