Transferred from: http://oss.org.cn/ossdocs/framework/spring/zh-cn/scheduling.html
The 18th chapter uses quartz or timer to complete the timing scheduling work |
Previous page |
|
Next page |
The 18th Chapter uses quartz or timer to complete the timing scheduling work
18.1. Introduction
Spring provides an integrated class that supports timing scheduling (translator Note: scheduling, below). Now, spring supports timer and quartz Scheduler in the JDK built into the 1.3 version (HTTP/ www.quartzscheduler.org). The two timing scheduler is established by Factorybean and maintains an optional reference to timers or triggers. Further, there is a handy class for Quartz scheduler and timer that allows you to invoke a method 18.2 on the target object (similar to the usual Methodinvokingfactorybeans) .
Use Opensymphony Quartz Scheduler
Quartz uses triggers,jobs and jobdetail to implement various tasks in timing scheduling. To understand the basic ideas behind quartz, you can move to Http://www.opensymphony.com/quartz. For ease of use, Spring provides several classes to simplify the use of quartz in spring-based applications.
18.2.1. Using Jobdetailbean
The Jobdetail object includes all the information that is required to run a job. So spring provides a so-called Jobdetailbean that makes Jobdetail have a real, meaningful default value. Let's look at an example:
<bean name= "Examplejob" class= "Org.springframework.scheduling.quartz.JobDetailBean" >
<property name= "Jobclass" >
<value>example. examplejob</value>
</property>
<property name= "Jobdataasmap" >
<map>
<entry key= "Timeout" ><value>5</value></entry>
</map>
</property>
</bean>
The job detail Bean has all the necessary information to run the job (examplejob). Use the job's data map to create a timeout. The job's data map can be obtained by Jobexecutioncontext (passed to you at runtime), but Jobdetailbean also maps the properties obtained from the job's data map to the attributes in the actual job. So, if Examplejob contains a property named Timeout, Jobdetailbean will automatically assign it a value:
package example;
public class Examplejob extends Quartzjobbean {
private int timeout;
/**
* Setter called after the examplejob are instantiated * with the value from the
Jobdetailbean (5)
*/
PU Blic void setTimeout (int timeout) {
this.timeout = timeout;
}
protected void Executeinternal (Jobexecutioncontext ctx)
throws jobexecutionexception {
//do the actual work< c12/>}
}
Some of the other settings in all job detail beans can also be set for you.
Note: With the name and group properties, you can modify which group the job runs under and what name to use. By default, the job name equals the name of the job Detai Bean (Examplejob in the example above).
18.2.2. Using Methodinvokingjobdetailfactorybean
Typically, you only need to invoke a method on a specific object. You can use Methodinvokingjobdetailfactorybean to do this exactly:
<bean id= "Methodinvokingjobdetail"
class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
<property name=" TargetObject "><ref bean=" Examplebusinessobject "/></property>
<property name=" TargetMethod " ><value>doIt</value></property>
</bean>
The above example will cause the doit method in Examplebusinessobject to be called (see below):
public class Businessobject {
//properties and collaborators public
void DoIt () {
//Does the actual work
}
}
<bean id= "Examplebusinessobject" class= "examples. Examplebusinessobject "/>
With Methodinvokingjobdetailfactorybean you don't need to create a job that only has one line of code and only calls a method, you just need to create real business objects to wrap the specific details of the object.
By default, Quartz jobs is stateless and can lead to a mutual effect between jobs. If you specify two triggers for the same jobdetail, it is likely that the second job will begin before the first job is completed. This does not happen if the Jobdetail object implements the stateful interface. The second job will not start until the first job is completed. In order for jobs not to run concurrently, set the concurrent mark in Methodinvokingjobdetailfactorybean to false.
<bean id= "Methodinvokingjobdetail"
class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
<property name=" TargetObject "><ref bean=" Examplebusinessobject "/></property>
<property name=" TargetMethod " ><value>doIt</value></property>
</bean>
Note: By default, Jobs runs in a parallel manner.
18.2.3. Using triggers and Schedulerfactorybean to wrap tasks
We have created the job details,jobs. We reviewed the handy bean that allows you to invoke a method on a particular object. Of course we still need to dispatch these jobs. This needs to be done using triggers and schedulerfactorybean. Quartz comes with some triggers that are available for use. Spring provides two subclass triggers, Crontriggerbean and Simpletriggerbean, respectively.
Triggers also needs to be dispatched. Spring provides schedulerfactorybean to expose some properties to set triggers. The Schedulerfactorybean is responsible for dispatching those actual triggers.
Two examples:
<bean id= "Simpletrigger" class= " Org.springframework.scheduling.quartz.SimpleTriggerBean "> <property name=" Jobdetail "> <!--See the Examp Le of method invoking job above--<ref bean= "Methodinvokingjobdetail"/> </property> <prope Rty name= "Startdelay" > <!--seconds---<value>10000</value> </property> <pro Perty name= "Repeatinterval" > <!--repeat every, seconds-<value>50000</value> </pro perty> </bean> <bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" > <property name= "jobdetail" > <ref bean= "examplejob"/> </property> <property name= "Crone Xpression "> <!--run every morning at 6 am-and <value>0 6 * * 1</value> </property> & Lt;/bean>
Now we have created two triggers, one of which starts to run every 50 seconds after 10 seconds of delay and the other runs every 6 o'clock in the morning. We need to create a schedulerfactorybean to ultimately achieve everything:
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "triggers" >
<list>
<ref local= "Crontrigger"/>
<ref local= "Simpletrigger"/>
</list >
</property>
</bean>
Some of the more properties you can set through Schedulerfactorybean, such as the calendars used by job details, are used to customize quartz properties and others. You can see the corresponding Javadoc (http://www.springframework.org/docs/api/org/springframework/scheduling/quartz/ schedulerfactorybean.html) to learn further information.
18.3. Using the JDK Timer support class
Another way to dispatch a task is to use the JDK timer object. More information about timers can be found here http://java.sun.com/docs/books/tutorial/essential/threads/timer.html. The concepts discussed above can still be applied to the support of a timer. You can create a custom timer or call a timer for some methods. The work of packing timers is done by Timerfactorybean.
18.3.1. Creating a custom Timers
You can use TimerTask to create a custom timer tasks, similar to jobs in Quartz:
public class Checkemailaddresses extends TimerTask {
private List emailaddresses;
public void setemailaddresses (List emailaddresses) {
this.emailaddresses = emailaddresses;
}
public void Run () {
//iterate-over-all email addresses and archive them
}}
Packaging It is simple:
<bean id= "Checkemail" class= "examples. Checkemailaddress ">
<property name=" emailaddresses ">
<list>
<value> test@springframework.org</value>
<value>foo@bar.com</value>
<value> john@doe.net</value>
</list>
</property>
</bean>
<bean id= " Scheduledtask "class=" Org.springframework.scheduling.timer.ScheduledTimerTask ">
<!--wait ten seconds Before starting repeated execution-to
<property name= "delay" >
<value>10000</value>
</property>
<!--run every seconds to
<property name= "period" >
<value >50000</value>
</property>
<property name= "TimerTask" >
<ref local= " Checkemail "/>
</property>
</bean>
18.3.2. Using Methodinvokingtimertaskfactorybean
Just like the support of quartz, the support of the timer also has a component that allows you to invoke a method periodically:
<bean id= "Methodinvokingtask"
class= " Org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean ">
<property name=" TargetObject "><ref bean=" Examplebusinessobject "/></property>
<property name=" TargetMethod " ><value>doIt</value></property>
</bean>
The above example will cause the doit method on the examplebusinessobject to be called (see below):
public class Businessobject {
//properties and collaborators public
void DoIt () {
//Does the actual work
}
}
Changing the reference to Scheduledtimertask mentioned in the above example to Methodinvokingtask will cause the task to be executed.
18.3.3. Packaging: Using Timerfactorybean to build tasks
Timerfactorybean, similar to Quartzschedulerfactorybean, serves one purpose: to establish an actual timing schedule. Timerfactorybean set up an actual timer to dispatch the tasks it refers to. You can specify whether it uses a daemon thread.
<bean id= "Timerfactory" class= "Org.springframework.scheduling.timer.TimerFactoryBean" >
<property Name= "Scheduledtimertasks" >
<list>
<!--See the example above---
<ref local= " Scheduledtask "/>
</list>
</property>
</bean>
This is it!
Previous page |
Upper level |
Next page |
17th. Send an email using the Spring Mail abstraction layer |
Start Page |
Appendix A. Spring ' s BEANS.DTD |