springboot timer on

First of all, build a springboot project, you can use maven or gradle or other (MD will not...), because this time the focus is on how to add a timer, I will write one when I have time in the past few days How to build a simple springboot project. Time is limited now, so I. Please forgive me.

Cough. Okay into the topic.

Method 1: Start the timer through the entrance of springboot.

First of all, we all know that springboot has its own entry point, which is @SpringBootApplication (he is a composite annotation composed of @Configuration, @EnableAutoConfiguration and @ComponentScan, okay... it's a long way).

First of all, the timer needs to have a master switch, because I may have to time many functions. If I want to turn them off temporarily, I can’t delete the comments one by one. So we need to turn on the main switch first, that is, add the @EnableScheduling annotation at the entrance of springboot. The above code (this is the entrance of springboot)

@SpringBootApplication
@EnableScheduling
public class DemoApplication {     public static void main(String[] args) {         SpringApplication.run(DemoApplication.class, args);     } }After the master switch is added, we only need to configure the timing methods that need to be performed. Use the annotation @Scheduled(cron = "0/2 * * * * *") followed by a cron expression. Indicates that it will be executed every 2 seconds. I will post the specific grammar of cron at the end.     @Scheduled(cron = "0/2 * * * * *")     public void timer(){         //Get the current time         LocalDateTime localDateTime =LocalDateTime.now();         System.out.println("The current time is:" + localDateTime .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));     } Okay, you're done. You will find that this method will be run every two seconds (LocalDateTime is a new feature of java1.8, which is clearer and easier to use than the previous Date. It is recommended that interested students can learn it and get started quickly).
 












The current time is: 2017-05-27 15:03:42 The
current time is: 2017-05-27 15:03:44 The
current time is: 2017-05-27 15:03:46
Method 2: Some students may say , I don't like to add a bunch of messy springboot, I want to look clear and concise. happy. OK Yes, students who have experience in xml-based development should know. To use the timer, I just need to configure it in xml, and then the method I want to run can be run according to the event I configured, yes! But springboot advocates no xml, but we can use annotations to make the class into xml, and add the code.

@Configuration //Prove that this class is a configuration file
@EnableScheduling //Turn on the quartz timer switch
public class Timer { } OK You’re done. (Nothing? Yes. It's that simple.) This method has the same effect as writing the annotation @EnableScheduling at the entrance of springboot, just write one for two. Then go to configure the method you want to time. In addition, I will mark a little trick for everyone. If you generally forget the name of the key in a comment. You don’t have to rush to Baidu. You can press ctrl and click to see the method name inside. For example, I forgot how the cron in @Scheduled was spelled. I can click in to view


@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 "";
}

I can find the corresponding spelling soon, I don’t need to go to Baidu, in case there is no internet at that time, right?
Well, the final cron expression is as follows (Baidu's == Do not spray)

●Asterisk (*): can be used in all fields, representing every moment in the corresponding time domain, for example, * in the minute field means "every minute";

●Question mark (?): This character is only used in the date and week fields. It is usually designated as a "meaningless value", which is equivalent to a point sign;

●Minus sign (-): express a range, such as "10-12" in the hour field, it means from 10 to 12 o'clock, that is, 10, 11, 12;

●Comma (,): express a list value, such as "MON, WED, FRI" in the day of the week field, it means Monday, Wednesday and Friday;

●Slash (/): x/y express a sequence of equal steps, x is the starting value, and y is the incremental step value. If you use 0/15 in the minutes field, it means 0, 15, 30 and 45 seconds, and 5/15 means 5, 20, 35, 50 in the minutes field. You can also use */y, which is equivalent to 0/y;

●L: This character is only used in the date and day of the week fields, representing the meaning of "Last", but it has different meanings in the two fields. In the date field, L represents the last day of the month, such as the 31st of January and the 28th of February in a non-leap year; if L is used in a week, it means Saturday, which is equivalent to 7. However, if L appears in the day of the week field, and there is a value X in front, it means "the last X days of the month", for example, 6L means the last Friday of the month;

●W: This character can only appear in the date field. It is a modification of the leading date and represents the working day closest to the date. For example, 15W means the working day closest to the 15th of the month. If the 15th of the month is a Saturday, it matches the 14th Friday; if the 15th is a Sunday, it matches the 16th Monday; if the 15th is a Tuesday, the result is 15 Tuesday. But it must be noted that the associated matching date cannot span months. For example, if you specify 1W, if the 1st is a Saturday, the result will be the 3rd Monday, not the last day of the previous month. The W string can only specify a single date, not a date range;

●LW combination: LW can be combined in the date field, which means the last working day of the month;

●Pound sign (#): This character can only be used in the weekday field, which means a working day of the current month. For example, 6#3 represents the third Friday of the current month (6 represents Friday, #3 represents the current third), and 4#5 represents the fifth Wednesday of the current month. If there is no fifth Wednesday in the current month, ignore it and do not trigger;

● C: This character is only used in the date and week fields, which means "Calendar". It means the date associated with the plan. If the date is not associated, it is equivalent to all dates in the calendar. For example, 5C in the date field is equivalent to the first day after the 5th day of the calendar. 1C is equivalent to the first day after Sunday in the weekday field.

Cron expressions are not sensitive to the case of special characters, nor to the case of abbreviations that represent the week.

Table 2 below gives some examples of complete Cron
 expressions : CRON expression meaning 
"0 0 12 * * ?" Trigger 
"0 15 10? * *" Trigger at 10:15 every morning 
"0 15 10" * * ?" Trigger at 10:15 every morning 
"0 15 10 * *? *" Trigger at 10:15 every morning 
"0 15 10 * *? 2005" Trigger at 10:15 every morning in 2005 
"0 * 14 * *? "Trigger once every minute 
from 2:00 PM to 2:59 PM "0 0/5 14 * * ?" Trigger every 5 minutes from 2:00 PM to 2:55 PM every day 
"0 0/5 14, 18 * * ?" every 5 minutes from 2 pm to 2:55 pm and 6 pm to 6:55 every day, 
"0 0-5 14 * * ?" every day 14:00 to 14:05 Trigger
"0 10,44 14? ​​3 WED" every minute  every Wednesday at 14:10 and 14:44 in March, trigger 
"0 15 10? * MON-FRI" every Monday, Tuesday, Wednesday, Thursday, and week Trigger at 10:15 on five

————————————————
Copyright Statement: This article is the original article of the CSDN blogger "beyondLi71", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting .
Original link: https://blog.csdn.net/liboyang71/article/details/72781526

Guess you like

Origin blog.csdn.net/qq_37557563/article/details/106334471