SpringBoot- set the timer task

@Scheduled task is to set the timing of notes.

Parameters commonly used for the two types:

  • The first is fixedRate, expressed in a fixed frequency to perform, in milliseconds; e.g. @Scheduled (fixedRate = 5000) represented is executed once every five seconds.
  • Second, the expression is executed in accordance with cron cron; do not know if cron expression, Baidu can be the first to know.

Annotation-based set up regular tasks

1. Create a scheduled task handler class TimedTask

package com.lw.coodytask.scheduled;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;

/**
 * @Classname TimedTask
 * @Description regular tasks
 * @Author lw
 * @Date 2019-12-20 16:43
 */
@EnableScheduling
@Component
public class TimedTask {

    @Scheduled(fixedRate = 5000)
    public void task_1(){
        System.out.println ( "timer task. 1:" + LocalDateTime.now ());
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void task_2(){
        System.out.println ( "timer task 2:" + LocalDateTime.now ());
    }

}

2. Start CoodyTaskApplication Service

package com.lw.coodytask;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CoodyTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(CoodyTaskApplication.class, args);
    }

}

3. Effect

Guess you like

Origin www.cnblogs.com/lwcode6/p/12074414.html