Scheduling a Job in Mule using Cron Expression.

Niladri.Biswas
Posted by in Mule category on for Beginner level | Points: 250 | Views : 13054 red flag

In this article we will learn about job scheduling using the powerful "Cron Expression".

Introduction

We have already seen as how to schedule job in Mule using the Quartz component.But we have used the default "Repeat Interval" property of the Quartz componet to do so.However,there is a more powerful tool available in the Unix paradigm call as "CRON" that has more powerful scheduling capabilities. The CronTrigger uses the powerful "cron expressions" that helps to set more powerful schedules like "Executing a job every Thrusday moring at 6.00 AM" or "Executing a job at 45 minutes past midnight on a daily basis" etc. In this article we will learn about job scheduling using the powerful "Cron Expression".

What we need to have for doing the experiment?

We need

  1. PostgreSQL
  2. PostgreSQL JDBC driver
  3. Download Mule ESB
  4. A database to be created in PostgreSQL say "TestDB" where atleast one Table say "CronTabExample" should be created as under

    		-- Table Creation Script
    		CREATE TABLE "CronTabExample"
    		(
    		   "StoreData" Text
    		) 
    		
  5. Everything should be up and running properly

Objective

We will have a Custom Component that will return the current data time of the day.We will schedule the custom component to run on every Monday through Thrusday at 10AM in the morning.Finally the record should be inserted into a PostgreSQL database

Let us start the experiment

First create a Mule Project.Then import the Postgre SQL JDBC Driver properly.Next create a Datasource.After that, create the Connector

All these steps are covered in my previous Mule articles and henceforth are not repeated here.

Next create a custom component that will return the current data time of the day. Earlier we have seen as how to create a custom componet and so this article assumes that the readers are familiar with that.

package org.mule.customcomponentexamples;
import java.util.*;
import java.text.*;

public class MyClass 
{	
	public String GetCurrentTime()
	{
		Date presentDate = new Date();		
		SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
		String currentDateTime = ft.format(presentDate);
		return currentDateTime;	    
	}
}

Let us then add Quartz Inbound component.Next double click on the Quartz Inbound component to open it's properties tab.In the "General" tab, give a proper "Job Name" (say "CronJobSchedule") and enter the below "Cron Expression".

0 30 10 ? * MON-FRI

A genaral syntax can be

Seconds | Minutes | Hours | Day-Of-Month | Month | Day-Of-Week | Year

Except the "Year" component, all are mandatory.Now let us analyze the values that we have provided in our Cron Expression. The Seconds field has a zero(0) value and Minutes fields has a 30 value.While the Hour fields has value of 10. This indicates that at 10.30AM , the job will trigger.If it is past 12 AM say we want to schedule at 2 PM, then the value for hour field will be 14.

We have placed a "?" at the Day-Of-Month field.It means that we are leaving the Day-Of-Month field as blank. A "*" in the Month field indicates that the cron expression will match for all values of the field i.e. for every month it will execute.We have placed "MON-FRI" in the Day-Of-Week which means that for every Monday through Friday the job will execute.

Now we need to add a new job. Click on plus sign.From the "Select element to use" popup that appears, choose the "quartz:event-generator-job" and click "Finish" button.

Now visit the "Advance" tab and let the "ResponseTimeout" be 1000(the default one).Click "OK"

The flow

The Configuration File

<flow name="TestProjectFlow1" doc:name="TestProjectFlow1">

	<!-- Step 1: Schedule the event using Cron Expression -->	
	<quartz:inbound-endpoint 
				jobName="CronJobSchedule" 
				doc:name="Quartz"
				repeatInterval="0" 
				responseTimeout="10000" 
				cronExpression="0 30 10 ? * MON-FRI">         			
	   <quartz:event-generator-job/>           
	</quartz:inbound-endpoint>
	
	 <!-- Step 2: Execute the java function -->	
	<component class="org.mule.customcomponentexamples.MyClass" doc:name="Java"/>
	
	<!-- Step 3: Dump into the destination Database -->	
	<jdbc:outbound-endpoint exchange-pattern="one-way" 
							queryKey="InsertRecord" 
							queryTimeout="-1" 
							connector-ref="PostgreSQL_JDBC_Connector" 
							doc:name="Database"/>
	
</flow>

Run the application

So we are almost done.As a last step, let us run the flow and we can see that the operation is happening at exactly 10.30AM

If we now look into the database, the result will be as under

References

  1. A Cron Expressions
  2. CronTrigger Tutorial

Conclusion

So this article has taught us as how to schedule a service using CRON expression.Hope it will be helpful.Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)