Home  >  Article  >  Java  >  How to use SpringBoot to implement a timer

How to use SpringBoot to implement a timer

王林
王林forward
2023-05-10 14:58:221125browse

Implementation of timer

Spring boot startup class code, added @EnableScheduling annotation to enable the timer function

@SpringBootApplication   
@EnableScheduling   // 开启定时功能
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

Scheduling task code, the task class needs to be injected into the @Component annotation In the IoC container

@Component
public class TestSchedulingTask {
    // 这个cron表达式代表从0秒开始,每过3秒执行一次。
    @Scheduled(cron = "0/3 * * * * ?")
    public void test(){
        // 大家可以根据公司的业务来实现,更希望是@Autowrite注入service层的接口,业务逻辑写在service层中。
        // 这里测试我就输出当前系统时间了...
        Date date  = new Date(System.currentTimeMillis());
        String rule = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(rule);
        String format = simpleDateFormat.format(date);
        System.out.println(format);
    }
}

, note here that each task method must be annotated with @Scheduled, and the cron expression must be specified.

You can rest assured, and there are also websites that generate cron expressions online. Let’s take a look at the results output by the console.

How to use SpringBoot to implement a timer

Detailed explanation of cron expression

cron expression

Field Range Allowed characters
Seconds 0-59 ,/-*
Minute(minute) 0-59 ,/-*
Hour(hour) 0-23 ,/-*
Date (day of month) 1-31(need to consider month inconsistency) ,/-*?LW
month 1-12 (the corresponding English abbreviation is also acceptable) , /-*
day of week 1-7 ,/-*?L
年(year) This thing is basically not used This thing is basically not used

Allowed characters Explanation:

,: It is an enumeration with multiple values. For example, 5 and 20 in second represent triggers at 5 and 20 seconds.

/: value 1/value 2, value 1 represents the start time, value 2 represents how often, for example, in my case, it triggers every 3 seconds starting from 0 seconds.

-: Range value, from how long to how long.

*: Common to all ranges.

?: Only exists in date and week. If you want to trigger on the 2nd of every month, but if you use * on the week, it will trigger every day, which will cause a bug. So is it necessary? to replace *.

L: means the last, appearing in date and week. For example, using L in date means the last day of the month, and using 3L in week means triggering on the last Wednesday.

W: Indicates working days, used in dates. If 9W is used, it will be triggered directly if the 9th falls on Monday 1-5. If the 9th is a Saturday, it will be triggered on the 8th. If the 9th is a Sunday, it will be triggered on the Monday. The first trigger is trigger number 10.

The above is the detailed content of How to use SpringBoot to implement a timer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete