<scheduler doc:name="Scheduler" doc:id="snswxf" >
<scheduling-strategy/>
</scheduler>
Scheduler (<scheduler/>)
Trigger execution of a flow based on a time-based condition. For example, a Scheduler might create and send a message that will trigger the flow processing every 5 seconds or every day at 12:00 in a given time zone.
The Scheduler sets a scheduling strategy at a fixed frequency or an interval based on a cron expression.
When adding a Scheduler to your Mule application, consider these points:
-
Schedulers use the same timezone as the machine on which Mule is running. However, if an application is running in CloudHub, the Scheduler conforms to the UTC timezone, regardless of the geographic region in which the application is running.
To set a different time zone for the Scheduler, see
timeZone
in the cron scheduling strategy attributes. -
The
disallowConcurrentExecution
property enables you to prevent the Scheduler from triggering the execution of the flow while a previously-triggered execution is running. -
If back-pressure occurs because no resources are available at the time of the scheduled trigger, Mule skips that execution.
-
In a Mule runtime engine cluster or multi-worker CloudHub deployment, the Scheduler executes only on the primary node (that is, only in one Mule instance).
Component XML
This component supports the following XML structure:
Scheduler (<scheduler/>
) attributes are configurable through the UI and XML.
Attribute Name | Attribute XML | Description |
---|---|---|
Scheduler (default) |
|
Editable name for the component to display in the canvas. |
N/A |
|
Automatically generated identifier for the component. |
Disallow concurrent execution |
|
When this attribute is set to |
Scheduling strategy (<scheduling-strategy/>
) is a child element of Scheduler.
Child Element | Description |
---|---|
|
Accepts a |
Fixed Frequency Attributes
Scheduler accepts a fixed frequency (<fixed-frequency/>
) element that accepts these attributes:
Attribute Name | Attribute XML | Description |
---|---|---|
N/A |
|
The frequency at which the Scheduler triggers the flow given the |
N/A |
|
The amount of time to delay execution of the Scheduler for the first time after the application starts. This property uses the same |
N/A |
|
One of the following time units for the values of |
See the fixed frequency examples.
Cron Attributes
For more complex scheduling, configure a cron expression in the <cron/>
element.
Attribute Name | Attribute XML | Description |
---|---|---|
N/A |
|
A cron expression for triggering the Scheduler. There is no default cron expression. For guidance with expressions, see Cron Expressions. Example: |
N/A |
|
Time zone passed as a system property, or in a machine’s operating system. Java time zone values are supported. Avoid Java abbreviations, such as |
See the cron examples.
Examples
The examples show how to set fixed frequency and cron scheduling strategies.
Fixed Frequency Examples
In the following example, the Scheduler executes every fifteen seconds:
<flow name="ex-fixed-frequency-flow" >
<scheduler doc:name="Scheduler">
<scheduling-strategy>
<fixed-frequency frequency="15" timeUnit="SECONDS" />
</scheduling-strategy>
</scheduler>
<!-- One or more processors here. -->
</flow>
In the following example, the Scheduler skips its execution if the flow takes longer to complete than the fixed frequency of 10 milliseconds:
<flow name="scheduler-disallowConcurrentExecution-ex" >
<scheduler doc:name="Scheduler" disallowConcurrentExecution="true" >
<scheduling-strategy>
<fixed-frequency frequency="10" timeUnit="MILLISECONDS"/>
</scheduling-strategy>
</scheduler>
<!-- processors here -->
</flow>
Each time a Scheduler skips its execution, Mule logs an Execution skipped
message, for example (edited for readability):
INFO 2022-11-09 15:15:43,082 ...
...scheduler.DefaultSchedulerMessageSource:
Flow 'scheduler-disallowConcurrentExecution-ex' is already running and
'disallowConcurrentExecution' is set to 'true'. Execution skipped.
INFO 2022-11-09 15:15:43,083 ...
...scheduler.DefaultSchedulerMessageSource:
Flow 'scheduler-disallowConcurrentExecution-ex' is already running and
'disallowConcurrentExecution' is set to 'true'. Execution skipped.
The following example shows a fixed-frequency scheduling strategy that executes the Scheduler every 10 seconds after an initial 10 second delay:
<scheduler doc:name="Scheduler" >
<scheduling-strategy >
<fixed-frequency frequency="10" timeUnit="SECONDS" startDelay="10"/>
</scheduling-strategy>
</scheduler>
Cron Examples
The following example uses a cron expression to trigger the generation of a Mule message in a new execution of the flow every fifteen seconds:
<flow name="ex-cron-expression-flow" >
<scheduler doc:name="Scheduler" >
<scheduling-strategy >
<cron expression='0/15 * * * * ? '/>
</scheduling-strategy>
</scheduler>
<!-- One or more processors here. -->
</flow>
The following example uses a cron expression to trigger the flow at 12:00 every day in the America/Los_Angeles
time zone.
<flow name="componentsFlow">
<scheduler>
<scheduling-strategy>
<cron expression="0 0 12 * * ?" timeZone="America/Los_Angeles"/>
</scheduling-strategy>
</scheduler>
<logger message="my message"/>
</flow>
Cron Expressions
Cron is a widely used standard for describing time and date information. The Cron Expression (<cron expression />
scheduling strategy (<scheduling-strategy >
) is useful for triggering a flow at intervals not available through the Fixed Frequency scheduling strategy.
The Scheduler keeps track of every second and creates a Mule event when the Quartz Cron expression matches your time-date setting. You can trigger the event just once or at regular intervals.
A date-time expression consists of six required settings and can include the optional year setting. Specify the settings in the following order:
-
Seconds (
0
-59
) -
Minutes (
0
-59
) -
Hours (
0
-23
) -
Day of month (
1
-31
) -
Month (
1
-12
orJAN
-DEC
) -
Day of the week (
1
-7
orSUN
-SAT
) -
Year (empty or a 4-digit year between
1970
-2099
, for example,2019
)
The Scheduler supports Quartz Cron expressions. Here are a few examples:
Expression | Behavior |
---|---|
|
Run every 2 seconds of the day, every day. |
|
Run at 10:15 a.m., every day. |
|
Run at 10:15 a.m., every day during the year 2019. |
|
Run every minute starting at 2pm and ending at 2:59pm, every day. |
|
Run every 5 minutes starting at 2pm and ending at 2:55pm, every day |
|
Run the first second of the first minute of the first hour, on the first and seventh day, every month. |
The Scheduler component also supports Quartz Scheduler special characters:
-
*
: All values. -
?
: No specific value. -
-
: Range of values, for example,1-3
. -
,
: Additional values, for example,1,7
. -
/
: Incremental values, for example,1/7
. -
L
: Last day of the week or month, or last specific day of the month (such as6L
for the last Saturday of the month). -
W
: Weekday, which is valid in the month and day-of-the-week fields. -
#
: "nth" day of the month. For example,#3
is the third day of the month.
This example logs the message "hello" every second:
<flow name="cronFlow" >
<scheduler doc:name="Scheduler" >
<scheduling-strategy >
<cron expression="* * * * * ?" />
</scheduling-strategy>
</scheduler>
<logger level="INFO" doc:name="Logger" message='"hello"'/>
</flow>
Note that there are many free online tools that can help you build Cron expressions.
Error Handling
At least one processor must follow the Scheduler. Processors include Mule components and connector operations, such as an HTTP Request operation or Transform Message component. Failure to provide a processor produces a MuleRuntimeException
and causes the deployment of the Mule application to fail with the following ERROR message in the logs (edited for readability):
ERROR ...MuleRuntimeException ...
The content of element 'flow' is not complete.
One of '{"http://www.mulesoft.org/schema/mule/core":abstract-message-processor,
"http://www.mulesoft.org/schema/mule/core":abstract-mixed-content-message-processor}'
is expected.