![]() |
In many applications the need arises to perform a certain action at a given time without any user interaction, usually to perform some administrative tasks. These tasks need to be scheduled, say to perform a job in the early hours of the morning before the start of business. This functionality is provided by using job scheduling software. Quartz.NET is an excellent open source job scheduler that can be used for these purposes. It provides a wealth of features, such as persistent jobs and clustering. To find out more about Quartz.NET visit their web site. Spring integration allows you to use Spring to configure Quartz jobs, triggers, and schedulers and also provides integration with Spring's transaction management features. The full details of Quartz are outside the scope of this quickstart
but here is 'quick tour for the impatient' of the main classes and
interfaces used in Quartz so you can get your sea legs. A Quartz
The sample application has two types of Jobs. One that inherits from
Spring's convenience base class The Spring base class public class ExampleJob : QuartzJobObject { private string userName; public string UserName { set { userName = value; } } protected override void ExecuteInternal(JobExecutionContext context) { Console.WriteLine("{0}: ExecuteInternal called, user name: {1}, next fire time {2}", DateTime.Now, userName, context.NextFireTimeUtc.Value.ToLocalTime()); } } The method <object name="exampleJob" type="Spring.Scheduling.Quartz.JobDetailObject, Spring.Scheduling.Quartz"> <property name="JobType" value="Spring.Scheduling.Quartz.Example.ExampleJob, Spring.Scheduling.Quartz.Example" /> <!-- We can inject values through JobDataMap --> <property name="JobDataAsMap"> <dictionary> <entry key="UserName" value="Alexandre" /> </dictionary> </property> </object> The dictionary property of the We then will schedule this job to be executed on 20 second
increments of every minute as shown below using Spring's
<object id="cronTrigger" type="Spring.Scheduling.Quartz.CronTriggerObject, Spring.Scheduling.Quartz"> <property name="jobDetail" ref="exampleJob" /> <!-- run every 20 second of minute --> <property name="cronExpressionString" value="0/20 * * * * ?" /> </object> Lastly, we schedule this trigger with the scheduler as shown below <object type="Spring.Scheduling.Quartz.SchedulerFactoryObject, Spring.Scheduling.Quartz"> <property name="triggers"> <list> <ref object="cronTrigger" /> </list> </property> </object> Running this configuration will produce the following output 8/8/2008 1:29:40 PM: ExecuteInternal called, user name: Alexandre, next fire time 8/8/2008 1:30:00 PM 8/8/2008 1:30:00 PM: ExecuteInternal called, user name: Alexandre, next fire time 8/8/2008 1:30:20 PM 8/8/2008 1:30:20 PM: ExecuteInternal called, user name: Alexandre, next fire time 8/8/2008 1:30:40 PM It is very convenient to schedule the execution of method as a job. The AdminService class in the example demonstrates this functionality and is listed below. public class AdminService { private string userName; public string UserName { set { userName = value; } } public void DoAdminWork() { Console.WriteLine("{0}: DoAdminWork called, user name: {1}", DateTime.Now, userName); } } Note that it does not inherit from any base class. To instruct
Spring to create a <object id="adminService" type="Spring.Scheduling.Quartz.Example.AdminService, Spring.Scheduling.Quartz.Example"> <!-- we inject straight to target object --> <property name="UserName" value="admin-service" /> </object> <object id="jobDetail" type="Spring.Scheduling.Quartz.MethodInvokingJobDetailFactoryObject, Spring.Scheduling.Quartz"> <!-- We don't actually need to implement IJob as we can use delegation --> <property name="TargetObject" ref="adminService" /> <property name="TargetMethod" value="DoAdminWork" /> </object> Note that <object id="simpleTrigger" type="Spring.Scheduling.Quartz.SimpleTriggerObject, Spring.Scheduling.Quartz"> <!-- see the example of method invoking job above --> <property name="jobDetail" ref="jobDetail" /> <!-- 5 seconds --> <property name="startDelay" value="5s" /> <!-- repeat every 5 seconds --> <property name="repeatInterval" value="5s" /> </object> This creates an instances of Quartz's SimpleTrigger class (as
compared to its CronTrigger class used in the previous section).
This trigger can then be added to the scheduler's list of registered triggers as shown below. <object type="Spring.Scheduling.Quartz.SchedulerFactoryObject, Spring.Scheduling.Quartz"> <property name="triggers"> <list> <ref object="cronTrigger" /> <ref object="simpleTrigger" /> </list> </property> </object> The interleaved output of both these jobs being triggered is shown below. 8/8/2008 1:40:18 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:20 PM: ExecuteInternal called, user name: Alexandre, next fire time 8/8/2008 1:40:40 PM 8/8/2008 1:40:23 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:28 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:33 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:38 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:40 PM: ExecuteInternal called, user name: Alexandre, next fire time 8/8/2008 1:41:00 PM 8/8/2008 1:40:43 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:48 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:53 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:40:58 PM: DoAdminWork called, user name: Gabriel 8/8/2008 1:41:00 PM: ExecuteInternal called, user name: Alexandre, next fire time 8/8/2008 1:41:20 PM 8/8/2008 1:41:03 PM: DoAdminWork called, user name: Gabriel
|