Original: 72781526
First of all, build good one springboot project, can use Maven or Gradle or other (MD not ... Because this time the focus is on how to add a timer, I will be in these days have time to write a how to build a simple springboot project process. Time is limited now, so. I also ask you to forgive me.
Cough. Okay, get into the theme.
Method One: Turn on the timer by bringing the Springboot to its own entrance.
First we all know that Springboot has a own entrance, that is @springbootapplication (he is a combination of annotations by @configuration,@ Enableautoconfiguration and @componentscan make up, OK ... Pulled away).
First the timer needs to have a total switch, because I may have to time a lot of functions, if I want to all temporarily closed can not be one to delete the note. So we need to open the gross position switch first, that is, add @enablescheduling this annotation at the springboot entrance. On the code (this is the entrance to Springboot)
[HTML]View PlainCopy
- @SpringBootApplication
- @EnableScheduling
- public class DemoApplication {
- public static void Main (string[] args) {
- Springapplication.run (Demoapplication.class, args);
- }
- }
When the general switch is added, we only need to configure the timing method, using the note @scheduled (cron = "0/2 * * * * *") followed by a cron expression. Represents the execution every 2 seconds. Cron specific syntax I'll post it at the end of the day.
[HTML]View PlainCopy
- @Scheduled (cron = "0/2 * * * * *")
- public void timer () {
- Get current time
- LocalDateTime LocalDateTime =localdatetime.now ();
- System.out.println ("Current time:" + Localdatetime.format (Datetimeformatter.ofpattern ("Yyyy-mm-dd HH:mm:ss"));
- }
All right, it's done. You will find that this method is run once every two seconds (LocalDateTime is a new feature of java1.8, more clear and useful than the previous date, it is recommended that interested students can learn, start quickly).
[Plain]View PlainCopy
- Current time: 2017-05-27-15:03:42
- Current time: 2017-05-27-15:03:44
- Current time: 2017-05-27-15:03:46
Method Two: Maybe some students said, Springboot I do not like to add a bunch of mess, I will look at clear, concise. Jolly. OK, there have been experience in the development of XML-based students should know. Using timers I just have to configure the XML so I can run the method I want to run according to the events I configured, yes! But Springboot argues that there is no XML, but we can use annotations to make the class XML, on the code.
[HTML]View PlainCopy
- @Configuration//prove that this class is a configuration file
- @EnableScheduling//Open Quartz Timer master switch
- public class Timer {
- }
OK is done. (No more?) Right. It's so simple. This practice is the same as the @enablescheduling note written at the entrance to the Springboot, two write one. Next, configure the method you want to schedule.
Also give everyone mark a little trick. If you generally forget the name of a key in an annotation. Not necessarily urgent to Baidu. You can hold down the CTRL key to look inside the method name, for example, I forgot how to spell the cron in @scheduled. I can check it in.
[HTML]View PlainCopy
- @Target ({elementtype.method, elementtype.annotation_type})
- @Retention (Retentionpolicy.runtime)
- @Documented
- @Repeatable (Schedules.class)
- Public @interface Scheduled {
- String cron () default "";
- String zone () default "";
- Long Fixeddelay () default-1l;
- String fixeddelaystring () default "";
- Long fixedrate () default-1l;
- String fixedratestring () default "";
- Long InitialDelay () default-1l;
- String initialdelaystring () default "";
- }
Soon can find the corresponding spelling, do not need to go to Baidu, in case there is no net at that time is not.
All right, the last cron expression is as follows (Baidu = = Do not spray)
Asterisk (*): Available in all fields, representing every moment of the corresponding time field, for example, * in the minute field, means "per minute";
Question mark (?): This character is used only in date and weekday fields, and it is usually specified as "meaningless value", which is equivalent to a dot character;
Minus sign (-): expresses a range, such as the use of "10-12" in the hour field, that is, from 10 to 12 points, that is, 10,11,12;
Comma (,): Expresses a list value, such as "Mon,wed,fri" in the day of the week field, that is, Monday, Wednesday, and Friday;
Slash (/): The X/y expression is a sequence of equal steps, the value is the start, and Y is the increment step value. If you use 0/15 in the minute field, it is expressed as 0,15,30 and 45 seconds, and 5/15 in the minute field represents 5,20,35,50, you can also use */y, which is equivalent to 0/y;
L: This character is used only in the date and weekday fields, meaning "last", but it has a different meaning in two fields. L In the Date field, the last day of the month, such as number 31st in January, 28th for non-leap year February, or Saturday, equivalent to 7 if L is used in the week. However, if L appears in the Week field and there is a value x in front, it means "the last X days of the Month", for example, 6L represents the last Friday of the month;
W: The character can only appear in a Date field, is a decoration on a leading date, and represents the most recent working day from that date. For example, 15W indicates the nearest working day from 15th of the month, if the month 15th is Saturday, match 14th Friday; if 15th is Sunday, match 16th Monday; If number 15th is Tuesday, the result is 15th Tuesday. But it must be noted that the associated match date is not able to cross the month, if you specify 1W, if 1th is Saturday, the result matches 3rd Monday, not the last day of last month. The W string can only specify a single date, but cannot specify a date range;
LW combination: In the Date field can be combined using LW, which means the last working day of the month;
Pound sign (#): This character can only be used in the week field, indicating a weekday of the month. If 6#3 represents the third Friday of the month (6 indicates Friday, #3表示当前的第三个), and 4#5 represents the fifth Wednesday of the month, assuming that the month does not have fifth Wednesday, ignoring does not trigger;
C: This character is used only in the date and weekday fields, meaning "Calendar". It means the date that the schedule is associated with, and if the date is not associated, it is equivalent to all dates in the calendar. For example 5C in a Date field is equivalent to the first day of the calendar after 5th. 1C is equivalent to the first day of Sunday after the week field.
Cron expressions are insensitive to the case of special characters, and are not sensitive to the case of the abbreviated English-language representation of the week.
Table 2 shows some examples of the complete cron expression:
Cron expression meaning
"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 * *?" Daily from 2 o'clock in the afternoon to 2:59 per minute trigger
"0 0/5 14 * *?" Every 5 minutes from 2 o'clock in the afternoon to 2:55
"0 0/5 14,18 * *?" Daily from 2 o'clock in the afternoon to 2:55 and from 6 to 6:55 every 5 minutes for two time periods
"0 0-5 14 * *?" Daily 14:00 to 14:05 triggers per minute
"0 10,44 14?" 3 WED "March of 14:10 and 14:44 triggers per Wednesday
"0 15 10?" * MON-FRI "10:15 triggers per Monday, Tuesday, Wednesday, Thursday, Friday
Two ways to add a timer to a springboot