The new @Schedule annotation in Mule and iBeans

December 17 2009

1 comment. 0
motif

Scheduling is great, it really is. It’s also very useful for application integration since we often have to repeat tasks of over time interval or schedule tasks for a date in the future. has had support since version 1.1 with the Quartz connector, now with iBeans scheduling just got easier. offers a annotations-based API for performing common integration tasks such as sending email, subscribing to a JMS queue or polling an ATOM feed.

The annotation approach means that iBeans can be used for existing projects as well as new projects since you can just decorate existing classes and allow iBeans to do the plumbing for you. Mule 3.0 will have full support for iBeans so you’ll be able to use iBeans annotations in Mule projects too.

One of the new annotations in iBeans is @Schedule. This 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 PingSomething class is loaded in the iBeans or Mule container a schedule will be set up to call the ping() method every second.

The @Schedule annotation also supports expressions. These provide a powerful way to express time triggers.

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 NameMandatoryAllowed ValuesAllowed Special Chars
SecondsYES0-59, – * /
MinutesYES0-59, – * /
HoursYES0-23, – * /
Day of MonthYES1-31, – * ? / L W C
MonthYES1-12 or JAN-DEC, – * /
Day of WeekYES1-7 or SUN-SAT, – * ? / L C #
YearNOempty, 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

For a full description see the Schedule annotation JavaDoc. The Quartz documentation also provides an in depth description of what you can do with cron expressions.

The @Schedule annotation can be used in conjunction with other iBeans annotations such as @Send and @Receive.

Scheduled Send

When used with @Send it allows for messages to be sent at a particular time, i.e. to email a monthly newsletter –

Scheduled Receive

When used with @Receive it allows developers to poll a resource. In my last blog post I showed how an ATOM feed could be polled to tweet new blog posts. Every day at midday the ATOM feed will be read for new entries, our method will get invoked for each new ATOM entry.

In iBeans and in the next Mule 3.0 milestone the annotations are discovered automatically, you just need to have your class on the classpath.


We'd love to hear your opinion on this post


One Response to “The new @Schedule annotation in Mule and iBeans”