Quartz and spring can be used to easily schedule tasks.

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.