Quartz and spring can be used to easily schedule tasks.
In the past few days, the project requires a task scheduling to run a program to check the database at the specified time.
The research decided to use quartz for implementation. If it is common, use quartz for a lot of configuration. But now we have spring, and everything is simpler.
1. First write the implemented class. This class can be independent and does not need to be inherited to quartz. The class name is com. ferly. Web. task. smssendtask.
2. Compile the spring configuration file as follows:
<? XML version = "1.0" encoding = "GBK"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "smssendtask" class = "com. ferly. Web. task. smssendtask"> <! -- Look, this is the class of the execution task we just wrote -->
</Bean>
<! -- Other configurations and normal sping configuration files -->
<Bean id = "smssendtaskdetail" class = "org. springframework. Scheduling. Quartz. methodinvokingjobdetailfactorybean">
<Property name = "targetobject"> <ref bean = "smssendtask"/> </property>
<Property name = "targetmethod"> <value> smssend </value> </property>
</Bean>
<Bean id = "smssendtaskscheduledtask" class = "org. springframework. Scheduling. Quartz. crontriggerbean">
<! -- Start the task 0: 0 every day -->
<! -- Seconds minutes hours day-of-month Day-of-week -->
<Property name = "cronexpression">
<Value> 0 0 0 **? </Value> <! -- Here, the running parameter is specified, and the task is executed on time at every day. -->
</Property>
<Property name = "jobdetail">
<Ref bean = "smssendtaskdetail"/>
</Property>
</Bean>
<Bean id = "smssendtaskfactory" class = "org. springframework. Scheduling. Quartz. schedulerfactorybean">
<Property name = "triggers">
<List> <ref bean = "smssendtaskscheduledtask"/> </List>
</Property>
</Bean>
</Beans>
3. Finished.
Let's salute spring and say, "spring, I love you, really !".
4. In addition, if you do not execute tasks at a fixed time, but every few minutes or several hours, there will be some modifications:
<Bean id = "smssendtaskscheduledtask" class = "org. springframework. Scheduling. Quartz. crontriggerbean"> the following content is changed:
<Bean id = "smssendtaskscheduledtask" class = "org. springframework. Scheduling. Quartz. simpletriggerbean">
<Property name = "startdelay">
<Value> 60000 </value> <! -- Run the command one minute after the service is started -->
</Property>
<Property name = "repeatinterval">
<Value> 6000 </value> <! -- Execute once every 1 minute -->
</Property>
<Property name = "jobdetail">
<Ref bean = "smssendtaskdetail"/>
</Property>
</Bean>