1.1.1 JDK Timer & TimerTask
If it's a simple execution cycle, then the timer with the JDK is the easiest. Specific:
①, write a simple class inheritance timertask, rewrite the Run method in the parent class in this newly-written class, and perform the action you want to perform in run;
②, write a simple class, write a method in a class that uses a timer to invoke the class created in ① and set the timer execution cycle.
Package cn.chenny.nsfw.complain;
Import Java.text.SimpleDateFormat;
Import java.util.Date;
Import Java.util.TimerTask;
public class MyTask extends TimerTask {
//performed task
@Override public
void Run () {
SimpleDateFormat format= New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
System.out.println ("Do timertask!!) Time: "+format.format (New Date ());
}
}
Package cn.chenny.nsfw.complain;
Import Java.util.Timer;
public class App. {public
static void Main (String args[]) {
Timer timer = new timer ();
Delay 1 seconds, perform the following tasks in 2s for the period
Timer.schedule (new MyTask (), 1000,);
}
1.1.2 Spring Integration Quartz
When using the JDK timer, it was found that the development requirement could not be met and that the task could not be performed on the specified date. This introduces an excellent open source task scheduling framework "Quartz". Quart's official website: http://www.quartz-scheduler.org/, the latest version quartz2.2.1 in the project.
consolidate quartz in spring to perform task scheduling; First, you introduce the jar package that you need into the project's lib directory:
"Spring-context-support-3.2.4.release.jar" This package is spring is encapsulated again into a bean-style class based on the main classes in quartz;
The core package of "Quartz-2.2.1.jar" quartz
To use the Quartz scheduling task in spring, you need to configure three beans:
1. Jobdetail task details, including which class to call, which method in the class, and whether to execute the task in parallel when executed.
2, trigger task scheduling triggers, mainly used to define when jobdetail execution. There are two types of triggers that are most commonly used: simple trigger Simpletrigger and task trigger Crontrigger . Simpletrigger, like the JDK timer, can only specify the frequency at which a task executes, but not the exact execution time. Crontrigger can either perform the time that a simple trigger is designed to execute on a frequency, or you can make a complex schedule to execute. If you can make: 15 points for the last day of each month to perform related tasks. The following figure takes 3 seconds to perform a related task
What is received in the Cronexpression expression property in Crontrigger:
The time format in an expression is separated by a space, and each time is represented by: seconds minutes, Sun and Moon anniversary (optional)
Values that can occur and are compliant:
The ' * ' character can be used for all fields, and the "*" in the "in" field indicates the meaning of "every Minute".
The '? ' character can be used in the days and weeks fields. It is used to specify the ' ambiguous value '. This is used when you need to specify one of the values in these two fields, not the other. In the following example, you can see what it means.
The '-' character is used to specify a range of values, such as setting "10-12" in the "hour" field to indicate "10 to 12 points".
', ' character specifies a number of values. For example, in the "Weeks" field set to "Mon,wed,fri" means "thedays Monday, Wednesday, and Friday."
The '/' character is used to specify the increment of a value. For example, in the "Seconds" field set to "0/15" means "No. 0, 15, 30, and 45 seconds." and "5/15" means "5th, 20, 35, and 50". The '/' plus ' * ' character is equivalent to specifying starting from 0 seconds. Each field has a series of values that can start or end. For the seconds and minutes fields, the range is 0 to 59, 0 to 23 for the hour field, 0 to 31 for the day field, and 1 to 12 for the month field. The "/" field is only a value that helps you to start "nth" within the range of allowable values. So for the month field "7/6" just means that July is open instead of "every six months", please note the subtle differences.
The ' L ' character can be used in two fields, days and weeks. It is the abbreviation for "last", but there are different meanings in these two fields. For example, "L" in the "Day" field means "last day in one months"-for January is number 31st for February is 28th (not a leap year). In the weeks field, it simply means "7" or "SAT", but if used in the "Weeks" field after a number, it means "the last one weeks of the month X"-for example, "6L" means "the last Friday of the month". When using the ' L ' option, it is important to specify a list or range, otherwise you will be confused by the results.
' W ' can be used in the ' Day ' field. Use to specify the most recent weekday (Monday to Friday) for the given date of the calendar. For example, you set the "Day" field to "15W", meaning: "From the month of 15th, the most recent working day." So if number 15th is Saturday, the trigger will be called at number 14th, Friday. If number 15th is Sunday, the trigger will be triggered at number 16th, which is Monday. If the number 15th is Tuesday, then the day will be triggered. However, if you set the "Day" field to "1W" and the first number is Saturday, the trigger will be triggered in the next Monday, the month 3rd, because it will not cross the range boundary of the month's value. The ' W ' character can only be used when the value of the ' Day ' field is a single day instead of a series of values.
' L ' and ' W ' can be combined for the "Day" field to be expressed as ' LW ', meaning ' last weekday of the month '.
The ' # ' character can be used in the weeks field. The character represents "Weeks X" of the month, such as "6#3", which represents the third Friday of the month (6 for Friday and "#3" for the third month). Another example: "2#1" = means 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 X" for the month, the month is not triggered.
The ' C ' character can be used for the "Day" and "Week" fields, which are abbreviations for "calendar". It is represented as a value computed, if any, based on the relevant calendar. If there is no calendar associated with it, it is equivalent to including all calendars. The day field value of "5C" indicates the first or 5th number in the calendar and beyond, and the week field value "1C" means "first day in the calendar or Sunday and beyond."
The legal characters for the month and week fields are not case sensitive.
Official Documentation Example:
3, schedulerfactory task scheduling factory; For scheduling individual task triggers.