![]() |
The Spring Framework features integration classes for scheduling
support. Currently, Spring supports the Quartz Scheduler (http://quartznet.sourceforge.net/). The scheduler is set up using
a
Quartz uses
<object name="ExampleJob" type="Spring.Scheduling.Quartz.JobDetailObject, Spring.Scheduling.Quartz"> <property name="JobType" value="Example.Quartz.ExampleJob, Example.Quartz" /> <property name="JobDataAsMap"> <dictionary> <entry key="Timeout" value="5" /> </dictionary> </property> </object> The job detail object has all information it needs to run the job
( namespace Example.Quartz; public class ExampleJob : QuartzJobObject { private int timeout; /// <summary> /// Setter called after the ExampleJob is instantiated /// with the value from the JobDetailObject (5) /// </summary> public int Timeout { set { timeout = value; }; } protected override void ExecuteInternal(JobExecutionContext context) { // do the actual work } } All additional settings from the job detail object are of course available to you as well. Note: Using the Often you just need to invoke a method on a specific object. Using
the <object id="JobDetail" type="Spring.Scheduling.Quartz.MethodInvokingJobDetailFactoryObject, Spring.Scheduling.Quartz"> <property name="TargetObject" ref="ExampleBusinessObject" /> <property name="TargetMethod" value="DoIt" /> </object> The above example will result in the public class ExampleBusinessObject { // properties and collaborators public void DoIt() { // do the actual work } } <object id="ExampleBusinessObject" type="Examples.BusinessObjects.ExampleBusinessObject, Examples.BusinessObjects"/> Using the By default, Quartz Jobs are stateless, resulting in the
possibility of jobs interfering with each other. If you specify two
triggers for the same <object id="JobDetail" type="Spring.Scheduling.Quartz.MethodInvokingJobDetailFactoryObject, Spring.Scheduling.Quartz"> <property name="TargetObject" ref="ExampleBusinessObject" /> <property name="TargetMethod" value="DoIt" /> <property name="Concurrent" value="false" /> </object>
We've created job details and jobs. We've also reviewed the
convenience class that allows to you invoke a method on a specific
object. Of course, we still need to schedule the jobs themselves. This
is done using triggers and a Triggers need to be scheduled. Spring offers a
Find below a couple of examples: <object id="SimpleTrigger" type="Spring.Scheduling.Quartz.SimpleTriggerObject, Spring.Scheduling.Quartz"> <!-- see the example of method invoking job above --> <property name="JobDetail" ref="ExampleJob" /> <!-- 10 seconds --> <property name="StartDelay" value="10s" /> <!-- repeat every 50 seconds --> <property name="RepeatInterval" value="50s" /> </object> <object id="CronTrigger" type="Spring.Scheduling.Quartz.CronTriggerObject, Spring.Scheduling.Quartz"> <property name="JobDetail" ref="ExampleJob" /> <!-- run every morning at 6 AM --> <property name="CronExpressionString" value="0 0 6 * * ?" /> </object> Now we've set up two triggers, one running every 50 seconds with a
starting delay of 10 seconds and one every morning at 6 AM. To finalize
everything, we need to set up the
<object id="quartzSchedulerFactory" type="Spring.Scheduling.Quartz.SchedulerFactoryObject, Spring.Scheduling.Quartz"> <property name="triggers"> <list> <ref object="CronTrigger" /> <ref object="SimpleTrigger" /> </list> </property> </object> More properties are available for the
|