Endpoints basically include those components that trigger or initiate the processing in a working flow of Mule application. They are called Source in Anypoint Studio and Triggers in the Design Center of Mule. One important endpoint in Mule 4 is Scheduler component.
This component works on time-based conditions, which means, it enables us to trigger a flow whenever a time-based condition is met. For example, a scheduler can trigger an event to start a Mule working flow every, say 10 seconds. We can also use flexible Cron expression to trigger a Scheduler Endpoint.
While using Scheduler event, we need to take care of some important points as given below −
Scheduler Endpoint follows the time-zone of the machine where Mule runtime is running.
Suppose if a Mule application is running in CloudHub, the Scheduler will follow the time-zone of the region in which the CloudHub worker is running.
At any given time, only one flow triggered by the Scheduler Endpoint can be active.
In Mule runtime cluster, the Scheduler Endpoint runs or triggers only on primary node.
As discussed above, we can configure a scheduler endpoint to be triggered at a fixed interval or we can also give a Cron expression.
Following are the parameters to set a scheduler to trigger a flow at regular intervals −
Frequency − It basically describes at which frequency the Scheduler Endpoint will trigger the Mule flow. Unit of time for this can be selected from the Time Unit field. In case you do not provide any values for this, it will use the default value which is 1000. On the other side, if you provide 0 or a negative value, then also it uses the default value.
Start Delay − It is the amount of time we must wait before triggering the Mule flow for the first time once the application is started. The value of Start delay is expressed in the same unit of time as the frequency. Its default value is 0.
Time Unit − It describes the time unit for both Frequency and Start Delay. The possible values of time unit are Milliseconds, Seconds, Minute, Hours, Days. The default value is Milliseconds.
Actually, Cron is a standard used for describing time and date information. In case you use the flexible Cron expression to make Scheduler trigger, the Scheduler Endpoint keeps track of every second and creates a Mule event whenever the Quartz Cron expression matches the time-date setting. With Cron expression, the event can be triggered just once or at regular intervals.
Following table gives the date-time expression of six required settings −
|Day of month||1-31|
|Month||1-12 or JAN-DEC|
|Day of the week||1-7 or SUN-SAT|
Some examples of Quartz Cron expressions supported by the Scheduler Endpoint are given below −
½ * * * * ? − means that the scheduler runs every 2 seconds of the day, every day.
0 0/5 16 ** ? − means that the scheduler runs every 5 minutes starting at 4 pm and ending at 4:55 pm, every day.
1 1 1 1, 5 * ? − means that the scheduler runs the first day of January and the first day of April, every year.
The following code logs the message “hi” every second −
<flow name = "cronFlow" doc:id = "ae257a5d-6b4f-4006-80c8-e7c76d2f67a0"> <doc:name = "Scheduler" doc:id = "e7b6scheduler8ccb-c6d8-4567-87af-aa7904a50359"> <scheduling-strategy> <cron expression = "* * * * * ?" timeZone = "America/Los_Angeles"/> </scheduling-strategy> </scheduler> <logger level = "INFO" doc:name = "Logger" doc:id = "e2626dbb-54a9-4791-8ffa-b7c9a23e88a1" message = '"hi"'/> </flow>