Camel Component: Quartz - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

The quartz: component provides a scheduled delivery of messages using the Quartz Scheduler 1.x . Each endpoint represents a different timer (in Quartz terms, a Trigger and JobDetail).

If you are using Quartz 2.x then from Camel 2.12 onwards there is a Quartz2 component you should use.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-quartz</artifactId>
   <!-- use the same version as your Camel core version -->
   <version>x.x.x</version>
</dependency>

URI format

quartz://timerName?options
quartz://groupName/timerName?options
quartz://groupName/timerName?cron=expression
quartz://timerName?cron=expression

The component uses either a CronTrigger or a SimpleTrigger. If no cron expression is provided, the component uses a simple trigger. If no groupName is provided, the quartz component uses the Camel group name.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Parameter

Default

Description

cron

None

Specifies a cron expression (not compatible with the trigger.* or job.* options).

trigger.repeatCount

0

SimpleTrigger: How many times should the timer repeat?

trigger.repeatInterval

0

SimpleTrigger: The amount of time in milliseconds between repeated triggers.

job.name

null

Sets the job name.

job.XXX

null

Sets the job option with the XXX setter name.

trigger.XXX

null

Sets the trigger option with the XXX setter name.

stateful

false

Uses a Quartz StatefulJob instead of the default job.

fireNow

false

If it is true will fire the trigger when the route is start when using SimpleTrigger.

deleteJob

true

Camel 2.12: If set to true, then the trigger automatically delete when route stop. Else if set to false, it will remain in scheduler. When set to false, it will also mean user may reuse pre-configured trigger with camel Uri. Just ensure the names match. Notice you cannot have both deleteJob and pauseJob set to true.

pauseJob

false

Camel 2.12: If set to true, then the trigger automatically pauses when route stop. Else if set to false, it will remain in scheduler. When set to false, it will also mean user may reuse pre-configured trigger with camel Uri. Just ensure the names match. Notice you cannot have both deleteJob and pauseJob set to true.

For example, the following routing rule will fire two timer events to the mock:results endpoint:

from("quartz://myGroup/myTimerName?trigger.repeatInterval=2"
   + "&trigger.repeatCount=1")
   .routeId("myRoute").to("mock:result");

When using a StatefulJob, the JobDataMap is re-persisted after every execution of the job, thus preserving state for the next execution. For more information about the StatefulJob interface and the JobDataMap class, see the Quartz API documentation on http://quartz-scheduler.org/.

If you run in OSGi such as within Apache Karaf and have multiple bundles with Camel routes that start from Quartz endpoints, then make sure if you assign an id to the <camelContext> that this id is unique, as this is required by the QuartzScheduler in the OSGi container. If you do not set any id on <camelContext> then a unique id will be auto assigned instead.

Configuring quartz.properties file

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.

However the Camel Camel Component: Quartz component also allows you to configure properties:

Parameter

Default

Type

Description

properties

null

Properties

You can configure a java.util.Properties instance.

propertiesFile

null

String

File name of the properties to load from the classpath

To do this you can configure this in Spring XML as follows

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

Enabling Quartz scheduler in JMX

You need to configure the quartz scheduler properties to enable JMX.

That is typically setting the option "org.quartz.scheduler.jmx.export" to a true value in the configuration file.

From Camel 2.13 onwards Camel will automatic set this option to true, unless explicit disabled.

Starting the Quartz scheduler

The Camel Component: Quartz component offers an option to let the Quartz scheduler be started delayed, or not auto started at all.

Parameter

Default

Type

Description

startDelayedSeconds

0

int

Seconds to wait before starting the quartz scheduler.

autoStartScheduler

true

boolean

Whether or not the scheduler should be auto started.

To do this you can configure this in Spring XML as follows

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

Clustering

If you use Quartz in clustered mode, for example, the JobStore is clustered. Then the Camel Component: 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.

Note : When running in clustered node no checking is done to ensure unique job name/group for endpoints.

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.

Using Cron Triggers

Quartz supports Cron-like expressions for specifying timers in a handy format. For more information, see the CronTrigger interface in the Quartz API documentation on http://quartz-scheduler.org/. You can use these expressions in the cron URI parameter; though to preserve valid URI encoding we allow + to be used instead of spaces. Quartz provides a little tutorial on how to use cron expressions.

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");

which is equivalent to using the cron expression

0 0/5 12-18 ? * MON-FRI

The following table shows the URI character encodings we use to preserve valid URI syntax:

URI Character

Cron character

+

Space