public class PingMe
{
@Schedule(interval = 1000)
public String ping()
{
return "ping!";
}
}
@Schedule Annotation
The @Schedule
annotation is a method level annotation that is used to schedule how often the method is called. To call a method every second you could use the following -
The interval is defined in milliseconds. When the PingMe
class is loaded in the Mule container a schedule will be set up to call the ping()
method every second.
The @Schedule
annotation also supports Introduction to Cron expressions expressions. These provide a powerful way to express time triggers.
The getStatus
method below will be called every hour.
@Schedule(cron = "* * 0 * * ?") //every hour
public String getStatus()
{
//use Twitter iBean
}
Arguments
Argument | Description | Required |
---|---|---|
cron |
A cron command that specifies when to call the method. |
You must set either |
interval |
The number of milliseconds between two scheduled invocations of the method. |
You must set either |
config |
A reference to the |
No |
startDelay |
The number of milliseconds that will elapse before the first event is fired. The default is -1, which means the first event is fired as soon as the application is started. |
No |
Introduction to Cron expressions
Cron is a UNIX tool that has been around for ever and is used for scheduling operating system tasks. It uses "cron expressions", which are able to create firing schedules such as: "At 8:00am every Weekday" or "every 5 minutes". Cron expressions are powerful but can be a little confusing so I have provided some examples below. A cron expression consists of 7 fields, one of which is optional, listed below.
Field Name | Mandatory | Allowed Values | Allowed Special Chars |
---|---|---|---|
Seconds |
YES |
0-59 |
, - * / |
Minutes |
YES |
0-59 |
, - * / |
Hours |
YES |
0-23 |
, - * / |
Day of Month |
YES |
1-31 |
, - * ? / L W C |
Month |
YES |
1-12 or JAN-DEC |
, - * / |
Day of Week |
YES |
1-7 or SUN-SAT |
, - * ? / L C # |
Year |
NO |
empty, 1970-2099 |
, - * / |
Some examples
-
0 0 12 * * ? Fire at 12pm (noon) every day
-
0 15 10 ? * * Fire at 10:15am every day
-
0 15 10 * * ? 2009 Fire at 10:15am every day during the year 2009
-
0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day
-
0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
-
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
-
0 11 11 11 11 ? Fire every November 11th at 11:11am