1. 程式人生 > 實用技巧 >Apache camel實現定時任務 學習二

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

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.

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).

學習來源:https://camel.apache.org/components/latest/quartz-component.html