Camel Job Scheduling

Camel provides two components for job scheduling and Automating tasks.

Timer component

The Timer component can be used to generate message exchanges at regular intervals. The following table includes the common URI options used to configure the Timer component:

NameDefault Value Description
timenullA java.util.Date the first event should be generated.
period 1000If greater than 0, generate periodic events every period milliseconds.
delay 0The number of milliseconds to wait before the first event is generated. Should not be used in conjunction with the time option.
repeatCount0Specifies a maximum limit of number of fires. If you set it to 5, it will only fire five times. A value of zero or negative means fire forever.

Example

Run someMethod define in myBean 5 times every 60 seconds:

<route>
    <from uri="timer://foo?repeatCount=5;period=60000"/>
    <to uri="bean:myBean?method=someMethod"/>
  </route>

Quartz component

The Quartz component provides more control of when tasks get executed. The following table includes the common URI options used to configure the Quartz component:

NameDefault Value Description
cronnoneSpecifies a cron expression used to determine when the timer fires.
repeatCount0Specifies the number of times to repeat the trigger. A value of -1 causes the timer to repeat indefinitely.
repeatInterval0Specifies the interval in milliseconds at which to generate events.

Cron expressions

Cron-Expressions are made up of seven sub-expressions, that describe individual details of the schedule.

Field nameMandatory? Allowed valuesAllowed special characters
SecondsYes0-59* / , -
MinutesYes0-59* / , -
HoursYes0-23* / , -
Day of monthYes1-31* / , - ? L W
MonthYes1-12 or JAN-DEC* / , -
Day of weekYes1-7 or SUN-SAT* / , - ? L #
YearNo1970�2099* / , -

Special characters

CharacterDescription
*Wild-cards can be used to say "every" possible value of this field.
/can be used to specify increments to values. '0/15' in the Minutes field means 'every 15th minute of the hour, starting at minute zero'. '3/20' in the Minutes field, it would mean 'every 20th minute of the hour, starting at minute three' - same as specifying '3,23,43' .
?is allowed for the day-of-month and day-of-week fields. It is used to specify "no specific value".
LThis character is short-hand for "last". The value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" � for example "6L" or "FRIL" both mean "the last friday of the month". You can also specify an offset from the last day of the month, such as "L-3" which would mean the third-to-last day of the calendar month.
Wused to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify "15W" as the value for the day-of-month field, the meaning is: "the nearest weekday to the 15th of the month".
#is used to specify "the nth" X weekday of the month. "6#3" or "FRI#3" in the day-of-week field means "the third Friday of the month".

Cron Examples

Cron expressionDescription
�0 0 12 ? * WED�every Wednesday at 12:00:00 pm
�0 0 23 ? * MON-FRI�every Monday to Friday at 23:00:00 pm
"0 0/5 * * * ?"every 5 minutes
"10 0/5 * * * ?"every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).
"0 30 10-13 ? * WED,FRI"at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.

Route example

Execute someMethod every weekday night at 11PM.
<route>
    <from uri="quartz://myGroup/myTimerName?cron=0+0+23+?+*+MON-FRI")/>
    <to uri="bean:myBean?method=someMethodName"/>
  </route>
Execute jdbc query every Monday night at 10PM and convert result to CSV format.
<camel:route id="testjdbc">
      <camel:from uri="quartz://jdbcTask?cron=0+0+22+?+*+MON")" />
          <camel:setBody>
             <camel:constant>select * from employee</camel:constant>
          </camel:setBody>
          <camel:to uri="jdbc:dataSource"/>
          <camel:marshal>
                     <camel:csv delimiter=","/>
           </camel:marshal>
            <camel:to uri="file://target/fileName=report.csv" />

</camel:route>