Spring timers are implemented in two main ways, including Java Timer timer and quartz timer!
1.Java Timer Timer
First, inherit the Java.util.TimerTask class to implement the Run method
Package Com.land;
Import Java.util.Date;
Import Java.util.TimerTask;
public class Timertest extends TimerTask {
@Override
public void Run () {
TODO auto-generated Method Stub
System.out.println ("Timertest" + (new Date ()). ToString ());
}
}
Spring Configuration
<bean id= "Timertest" class= "Com.land.TimerTest" >
</bean>
<bean id= "Timertesttask" class= " Org.springframework.scheduling.timer.ScheduledTimerTask,
<!--wait ten seconds Before starting repeated execution-->
<property name= "delay" value= "10000"/> <!--delay time after startup-->
<!--interval-->
<property Name = "Period" value= "72000"/>
<property name= "TimerTask" ref= "Timertest"/>
</bean>
<bean id= "Timerfactory" class= "Org.springframework.scheduling.timer.TimerFactoryBean" >
<property name= "Scheduledtimertasks" >
<list>
<!--See the example above--
<ref bean= "Timertesttask"/>
</list>
</property>
</bean>
2.Quartz Timer
First, inherit the Quartzjobbean class to implement the Executeinternal method
public class Schedulertest extends quartzjobbean{
@Override
protected void Executeinternal (Jobexecutioncontext arg0) throws Jobexecutionexception {
TODO auto-generated Method Stub
System.out.println ("-----------");
System.out.println ("1111111" + (new Date ()). ToString ());
}
}
-------------------------------------------
public class SchedulerTest2 extends quartzjobbean{
@Override
protected void Executeinternal (Jobexecutioncontext arg0) throws Jobexecutionexception {
TODO auto-generated Method Stub
System.out.println ("-----------");
System.out.println ("22222222" + (new Date ()). ToString ());
}
}
Spring Configuration
<bean id= "Schedulertask" class= " Org.springframework.scheduling.quartz.JobDetailBean ",
<property name=" Jobclass ";
<value>com.land.schedulertest</value>
</property>
</bean>
<bean id= "SchedulerTask2" class= "Org.springframework.scheduling.quartz.JobDetailBean"
<property name= "Jobclass";
<value>com.land.schedulertest2 </value>
</property>
</bean>
<bean id= "Schedulertasktrigger" Class= "Org.springframework.scheduling.quartz.CronTriggerBean";
<property name= "Jobdetail" ref= "Schedulertask"/>
<property name= "cronexpression";
<value >0/10 * * * * * </VALUE>
</property>
</bean>
<bean id= "SchedulerTaskTrigger2" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" >
<property name= "Jobdetail" ref= "SchedulerTask2"/>
<property name= "Startdelay" >
<value>1000</value><!--Delay Time-
</property>
<property name= "Repeatinterval" >
<value>20000</value><!--Interval Time-
</property>
</bean>
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Triggers" >
<list>
<ref bean= "Schedulertasktrigger"/>
<ref bean= "SchedulerTaskTrigger2"/>
</list>
</property>
</bean>
3. Other Notes
Java Timer Timer has a configuration to start the party org.springframework.scheduling.timer.ScheduledTimerTask,
There are two ways to configure the quartz timer to start: Org.springframework.scheduling.quartz.simpletriggerbean,org.springframework.scheduling.quartz.crontri Ggerbean.
Org.springframework.scheduling.quartz.CronTriggerBean
The Linux system's scheduled tasks usually have cron to bear. From left to right
Special characters allowed for field allowed values
Seconds 0-59,-*/
Sub 0-59,-*/
Hours 0-23,-*/
Date 1-31,-*? /L W C
Month 1-12 or JAN-DEC,-*/
Week 1-7 or Sun-sat,-*? /L C #
Year (optional) leave blank, 1970-2099,-*/
Meaning of an expression
"0 0 12 * *?" Triggered 12 o'clock noon every day
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Triggered 10:15 daily
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" Triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" Triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" Every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" Triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month, triggered
"0 L *?" 10:15 trigger on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 Trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger
Special character meaning
* denotes all values;
? Represents a value that is not stated, that is, not concerned about its value;
-Represents a specified range;
, which indicates that a possible value is attached;
/symbol indicates the start time, and the symbol indicates the value increment each time;
L ("last") "L" is used in the Day-of-month field meaning "the final day of the month"; In the Day-of-week field, it simply means "7" or "SAT". If used in conjunction with a number in the Day-of-week field, it means "the last one weeks of the month" – for example: "6L" means "last Friday of the month". When we use "L", it is important not to specify a list value or range, otherwise we will get some unexpected results.
W ("weekday") can only be used in the Day-of-month field. Used to describe the working day closest to the specified day (Monday to Friday). For example: In the Day-of-month field with "15W" means "closest to the 15th day of the month", that is, if the 15th day of the month is Saturday, then the trigger will be triggered on the 14th day of the month, Friday, if the 15th day is Sunday, then the trigger will be triggered in the month 16th day that Monday If the 15th day of the month is Tuesday, trigger this day on the trigger. Note that this usage only calculates the value in the current month and does not cross the current month. The "W" character can only be specified one day in Day-of-month and cannot be a range or list. You can also use "LW" to specify the last working day of the month.
# can only be used in the Day-of-week field. Used to specify the first few weeks of the month. Example: In the Day-of-week field "6#3" means the 3rd Friday of this month (6 means Friday, 3 refers to 3rd). If the specified date does not exist, the trigger is not triggered.
C refers to the calculated value after contacting the calendar. Example: In the Day-of-month field, "5C" means the first day of the calendar including the 5th day of the month, or the first day in the Day-of-week field that includes the calendar after this Sunday or later.
Not sensitive to case letters in month and day of week fields
Configure several configuration methods and examples for scheduled tasks using Java