Use spring quartz to implement scheduled tasks

Source: Internet
Author: User

Requirements:

A function module needs to be periodically executed. For example, an operation is executed at every day and an operation is executed every hour.

Use spring quartz to implement scheduled tasks

 /**  * The system automatically changes the order status to "REJECTED" and "REJECTED" to "REJECTED" and "REJECTED" within 3 days "*  @ Author  Jokey *  */
// ImplementationQuartzjobbeanOrg. springframework. Scheduling. Quartz. quartzjobbean;
Public Class Autochangeorderstatuswithwaitcall Extends Quartzjobbean { Private Iorderservice orderservice;
// Rewrite the default execution Method Protected Void Executeinternal (jobexecutioncontext arg0) Throws Jobexecutionexception {system. Out. println (( New Date () + "***** start: data has been returned for more than three days after the execution of the rejection is converted into a rejection pending outbound call" ); Orderservice. changeorderstatuswithwaitcall (); system. Out. println (( New Date () + "***** end: data has been returned for more than three days after the execution of the rejection is converted into a rejection pending outbound call" );} Public Void Setorderservice (iorderservice orderservice ){ This . Orderservice = Orderservice ;}}

Configure the timer and trigger in spring

 <! --  Updated every three hours. data has not been transferred from rejected items to rejected calls for outgoing calls within three days.  -->    <  Bean  ID  = "Autochangeorderstatuswithwaitcalltrigger"  Class  = "Org. springframework. Scheduling. Quartz. crontriggerbean" >          <  Property  Name  = "Jobdetail"  > // Work type              <  Ref  Bean  = "Autochangeorderstatuswithwaitcalljob"   />          </  Property  >          <  Property Name  = "Cronexpression"  > // Expression execution cycle              <! --  0 0 0/3 **?  -->              <  Value  > 0 ****? </  Value  >          </  Property  >      </ Bean  >      <  Bean  ID  = "Autochangeorderstatuswithwaitcalljob"  Class  = "Org. springframework. Scheduling. Quartz. jobdetailbean"  >

// Configure the work class < Property Name = "Jobclass" Value = "Com. xwtech. xwout. quartzjob. autochangeorderstatuswithwaitcall" />
// Configure the injection field. The orderservice injection configuration is not configured here.
< Property Name = "Jobdataasmap" > < Map > < Entry Key = "Orderservice" > < Ref Bean = "Orderservice" /> </ Entry > </ Map > </ Property > </ Bean > <! -- General Scheduling --> < Bean ID = "Startquertz" Lazy-init = "False" Autowire = "No" Class = "Org. springframework. Scheduling. Quartz. schedulerfactorybean" > < Property Name = "Triggers" > < List > < Ref Bean = "Autochangeorderstatuswithwaitcalltrigger" /> </ List > </ Property > <! -- <Property name = "autostartup" value = "$ {quartz. autostartup}"/> --> </ Bean >

 

Another method

@ Service ("studentservice" )  Public   Class Studentserviceimpl Implements  Istudentservice {  Private  Ibasedao basedao; Public   Int  Addstudent (student Stu) {string SQL = "Insert into student values (null ,?,?) " ;  Return Basedao.exe cute (SQL, New  Object [] {Stu. getname (), Stu. getstatus ()});}  Public   Void  Updatestudentstatus () {string SQL = "Update student s set S. Status =? Where S. ID =? " ; System. Out. println ((  New Date () + "scheduled task executed" );  //  Basedao.exe cute (SQL, new object [] {Stu. getstatus (), Stu. GETID ()});  } @ Resource  Public   Void  Setbasedao (ibasedao basedao ){  This . Basedao = Basedao ;}} 

 

  <! --  Studenetservice has been annotated and driven. You do not need to declare it here.  -->          <! -- Define the scheduled execution of the updatestudentstatus () method in the bean studentservice  -->   <  Bean  ID  = "Dojob"  Class  = "Org. springframework. Scheduling. Quartz. methodinvokingjobdetailfactorybean"  >     <! --  Bean corresponding to the method you want to execute  -->          <  Property  Name = "Targetobject"  >                <  Ref  Bean  = "Studentservice"   />            </  Property  >     <! --  You need to execute that method. Note that the method does not have a return value, nor does the parameter seem to have a return value.  -->          <  Property  Name = "Targetmethod"  >               <  Value  > Updatestudentstatus </  Value  >            </  Property  >        </  Bean  >               <! -- Set the bean of the trigger. Here we set the jobdetail to be triggered. Here we define that the jobdetail to be triggered is searchengernetask, which bean the trigger will trigger... and we also define the trigger time.  -->         <  Bean  ID  = "Crontrigger"  Class  = "Org. springframework. Scheduling. Quartz. crontriggerbean"  >            <  Property  Name  = "Jobdetail"  >               <  Ref  Bean  = "Dojob"   />            </  Property  >            <  Property  Name  = "Cronexpression"  >                 <! --  The key is to configure this expression. The time setting indicates that the trigger is triggered every minute between PM and PM, and some information is written at last. You can view the information by yourself.  -->                <  Value  > ** 2 **? </  Value  >            </  Property  >        </  Bean  >            <! --  Manage the overall settings of triggers, manage the trigger list, and place multiple triggers in the bean list.  -->         < Bean  Autowire  = "No"  Class  = "Org. springframework. Scheduling. Quartz. schedulerfactorybean"  >            <  Property  Name  = "Triggers"  >                <  List  >                    <  Ref Bean  = "Crontrigger"   />                </  List  >          </  Property  >      </  Bean  >    

 

 

 

 

 

1. second 2. score 3. hour 4. the date in the month (1-31) 5. month (1-12 or JAN-DEC) 6. day of the Week (1-7 or sun-Sat) 7. year (1970-2099)
Seconds 0-59 ,-*/
0-59 ,-*/

Hour 0-23 ,-*/

1-31 ,-*? /L W C

Month 1-12 or JAN-DEC ,-*/

What is the week 1-7 or sun-Sat ,-*? /L c #

Year (optional) empty, 1970-2099 ,-*/
Detailed analysis of available values is as follows:
"*" -- Character can be used for all fields. In the "Minute" field, it is set to "*" to indicate the meaning of "every minute.
"?" -- The character can be used in the "day" and "day of week" fields. it is used to specify the 'ambiguous value '. this will be used when you need to specify a value in the two fields instead of the other one. In the following example, we can see its meaning.
"-" -- The character is used to specify a value range. For example, in the "Hour" field, it is set to "10-12" to "10 to 12 ".
"," -- The character specifies several values. For example, set "mon, wed, Fri" in the "Week" field to "The days Monday, Wednesday, and Friday ".
"/" -- Specifies the increase range of a value.
For example, in the second field, set "0/15" to "0th, 15, 30, and 45 seconds ". "5/15" indicates "5th, 20, 35, and 50 ".
Adding "*" before '/' is equivalent to specifying that it starts from 0 seconds.
Each field has a series of values that can start or end. For the second and minute fields, the value range is 0 to 59. For the hour field, the value ranges from 0 to 23,
For the "day" field, it is 0 to 31, and for the "month" field, it is 1 to 12. The "/" field only helps you start the "nth" value within the allowed value range.
"L" -- the character can be used in the "day" and "day of week" fields. It is the contraction of "last"
Write, but the two fields have different meanings. For example, "l" in the "day" field indicates "the last day of a month "--
For June January, the 31st is the 28th (non-leap year) for June February ). In the "Week" field, it simply represents "7" or
"Sat", but if it is used after a number in the "Week" field, it indicates "the last week of the month × "--
For example, "6l" indicates "the last Friday of the month ". When you use the 'l' option, it is very important to specify a specific list or range. Otherwise, you will be confused by the results.
"W" -- can be used for the "day" field. Used to specify the business days closest to the specified calendar date (from Monday to week)
5 ). For example, if you set the "day" field to "15 W", it means:
"Workday closest to the 15th day of the month ". Therefore, if the 15th is Saturday, the trigger will be called on the 14th or Friday. If the 15th is Sunday,
The trigger is triggered on the 16th, that is, Monday. If the 15th is Tuesday, it will be triggered on that day. However, if you set the "day" field to "1 W", and "1" is Saturday,
The trigger will be triggered on the next Monday, that is, the third day of the current month, because it will not cross the range boundary of the value of the current month. The 'W' character can only be used when the value of the "day" field is a separate day rather than a series of values.
"L" and "W" can be combined for the "day" field to represent 'lw', meaning "the last workday of the month ".
"#" --
The character can be used in the "Week" field. This character indicates the number of weeks x of the month. For example, "6 #3" indicates the third Friday of the month (6 indicates the Friday and "#3" indicates the third of the month ). For example: "2 #1"
= Indicates the first Monday of the month and "4 #5" = the fifth Wednesday of the month. Note that if you specify "#5", there is no fifth "week ×" in the month, this month will not be triggered.
"C "--
The character can be used for the "day" and "day of week" fields. It is short for "calendar. It indicates the value calculated based on the relevant calendar (if any ). If no calendar is associated,
It is equivalent to containing all calendars. The value of the "day" field is "5c", indicating "the first day in the calendar or after the fifth day ", if the value of the "Week" field is "1C", it indicates "the first day in the calendar or after Sunday ".
The valid characters in the month field and the week field are not case sensitive.

For example:

<Property name = "cronexpression" value = "0 0 0/4 **? "/> Indicates that the task is executed every four hours.

<Property name = "cronexpression" value = "0 0 7 **? "/> Indicates that the task is executed at seven o'clock A.M. every day.

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.