Apache camel实现定时任务 学习二

个人理解,有错请见谅

 

1.定时任务文件配置

   By default Quartz will look for a quartz.properties

    file in the org/quartz directory of the classpath.

    If you are using WAR deployments this means just drop

    the quartz.properties in WEB-INF/classes/org/quartz.

 

2.属性文件配置的要素

    properties(类型:Properties):

           You can configure a java.util.Properties instance.

    propertiesFile(类型:String)

            File name of the properties to load from the classpath

 

3.例子

   <bean id="quartz" class="org.apache.camel.component.quartz.QuartzComponent">
         <property name="propertiesFile" value="com/mycompany/myquartz.properties"/>
   </bean>

 

4.开始一个定时任务配置

   <bean id="quartz" class="org.apache.camel.component.quartz.QuartzComponent">
        <property name="startDelayedSeconds" value="5"/>
   </bean>

5.Clustering(集群)

    If you use Quartz in clustered mode, e.g. the JobStore is clustered.

    Then the Quartz component will not pause/remove triggers when

    a node is being stopped/shutdown. This allows the trigger to keep

    running on the other nodes in the cluster.

 

6.Message Headers(设置头部信息)

   Camel adds the getters from the Quartz Execution Context as header values.

    The following headers are added:

    calendar, fireTime, jobDetail, jobInstance, jobRuntTime, mergedJobDataMap,

    nextFireTime, previousFireTime, refireCount, result, scheduledFireTime,

    scheduler, trigger, triggerName, triggerGroup.

    The fireTime header contains the java.util.Date of when the exchange was fired.

   Note: When running in clustered node no checking is done to

             ensure unique job name/group for endpoints.

7.用Cron表达式,做MQ定时传输

    Using Cron Triggers

    Quartz supports Cron-like expressions for specifying timers in a handy format.

    You can use these expressions in the cron URI parameter;

     though to preserve valid URI encoding we allow + to be used instead of spaces.

    For example, the following will fire a message every five minutes starting

    at 12pm (noon) to 6pm on weekdays:

    from("quartz://myGroup/myTimerName?cron=0+0/5+12-18+?+*+MON-FRI")
    .to("activemq:Totally.Rocks");

 

8.Specifying time zone(特定的时区)

   The Quartz Scheduler allows you to configure time zone per trigger.

   For example to use a timezone of your country, then you can do as follows:

   quartz://groupName/timerName?cron=0+0/5+12-18+?+*+MON-FRI&trigger.timeZone=Europe/Stockholm

   Note:The timeZone value is the values accepted by java.util.TimeZone.

 

9.Configuring misfire instructions(配置需要的参数)

   The quartz scheduler can be configured with a misfire

   instruction to handle misfire situations for the trigger.

   The concrete trigger type that you are using will have

   defined a set of additional MISFIRE_INSTRUCTION_XXX

   constants that may be set as this property’s value.

   For example to configure the simple trigger to use misfire instruction 4:

       quartz://myGroup/myTimerName?trigger.repeatInterval=2000&trigger.misfireInstruction=4

   And likewise you can configure the cron trigger

   with one of its misfire instructions as well:

        quartz://myGroup/myTimerName?cron=0/2+*+*+*+*+?trigger.misfireInstruction=2

 

 

  不同的参数含义:

   1.The simple and cron triggers has the following misfire instructions representative:

          SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW = 1 (default):

                Instructs the Scheduler that upon a mis-fire situation,

               the SimpleTrigger wants to be fired now by Scheduler.

               This instruction should typically only be used

               for 'one-shot' (non-repeating) Triggers.

               If it is used on a trigger with a repeat count > 0 then

              it is equivalent to the instruction

              MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT.

 

      2. SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT = 2

              Instructs the Scheduler that upon a mis-fire situation,

              the SimpleTrigger wants to be re-scheduled to 'now'

              (even if the associated Calendar excludes 'now') with the repeat count left as-is.

              This does obey the Trigger end-time however, so if 'now' is after the end-time the Trigger will not fire again.

              Use of this instruction causes the trigger to 'forget' the start-time and repeat-count that

              it was originally setup with (this is only an issue if you for some reason wanted to

              be able to tell what the original values were at some later time).

 

      

    3.SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT = 3

      Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to

      be re-scheduled to 'now' (even if the associated Calendar excludes 'now') with the

      repeat count set to what it would be, if it had not missed any firings.

     This does obey the Trigger end-time however, so if 'now' is after the

      end-time the Trigger will not fire again.

     Use of this instruction causes the trigger to 'forget' the start-time and repeat-count that

     it was originally setup with. Instead, the repeat count on the trigger will be changed

     to whatever the remaining repeat count is (this is only an issue if you for some

     reason wanted to be able to tell what the original values were at some later time).

     This instruction could cause the Trigger to go to the 'COMPLETE' state after firing 'now',

     if all the repeat-fire-times where missed.

 

    4.SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT = 4

      Instructs the Scheduler that upon a mis-fire situation,

      the SimpleTrigger wants to be re-scheduled to the next scheduled time after

     'now' - taking into account any associated Calendar and with the repeat count

     set to what it would be, if it had not missed any firings.

     waring:This instruction could cause the Trigger to go directly to the 'COMPLETE' state if all fire-times where missed.

  

   5.SimpleTrigger.MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT = 5

     Instructs the Scheduler that upon a mis-fire situation,

     the SimpleTrigger wants to be re-scheduled to the next scheduled time

     after 'now' - taking into account any associated Calendar,

     and with the repeat count left unchanged.

   

     waring:This instruction could cause the Trigger to go directly to the 'COMPLETE'

                 state if the end-time of the trigger has arrived.

              

    6.CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW = 1 (default)

       Instructs the Scheduler that upon a mis-fire situation, the CronTrigger wants to be fired now by Scheduler.

   

    7.CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING = 2

        Instructs the Scheduler that upon a mis-fire situation,

        the CronTrigger wants to have it’s next-fire-time updated to

       the next time in the schedule after the current time (taking into account any

       associated Calendar but it does not want to be fired now.

   

学习来源:https://camel.apache.org/components/latest/quartz-component.html

posted @ 2020-08-28 17:28  小窝蜗  阅读(684)  评论(0)    收藏  举报