Reading Time: 7 minutes

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 scheduling support since version 1.1 with the Quartz connector, now with iBeans scheduling just got easier. iBeans offers a annotations-based 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 –

public class PingSomething
{
    @Schedule(interval = 1000)
    public void ping()
    {
        //ping something
    }
}

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 cron 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 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

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 –

public class NewsLetterGenerator
{
    @Schedule(cron = "0 15 10 ? * 6L")
    @Send(uri = "smtp://localhost:25?subject=New This Month")
    public String createLetter(@SendHeaders Map messageHeaders)
    {
        //create the letter, set the recipients in the messageHeaders
    }
}

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.

latest report
Learn why we are the Leaders in API management and
public class BlogToTwitter
{
    @Schedule(cron = "0 0 12 * * ?")
    @Receive(uri = "atom:http://rossmason.blogspot.com/feeds/posts/default")
    public void processEntry(Entry entry) throws Exception
    {
        //read the Entry and Tweet it
    }
}

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.