springboot use @ EnableScheduling, @ Scheduled to open regular tasks

1. Add a comment @EnableScheduling the main startup items

package com.example.springmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
//@MapperScan("com.example.springmybatis.dao")
public class SpringMybatisApplication {

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

2. Add @Component in a class, the method of adding @Scheduled

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class SchedulerTask {
    //想要开启定时任务,必须在启动页加上@EnableScheduling
    @Scheduled(cron="30 10 1 * * ?")
    public void aTask(){
        try {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf.format(newDate ()) + "********* A to perform a task every 20 seconds into the test" ); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
    } 
}

3.cron expression meaning:

* * * 0 0 10,14,16 ? 10 am every day, two in the afternoon, 16 o'clock the whole trigger
  0 0/30 * 9-17 *? Every day from nine to V to trigger every half hour
  0 15 10? * MON- FRI 10 am Monday to Friday : 15 trigger
  ? 0 0/5 * * * trigger every 5 minutes,
  10 0/5 * * *? Every 5 minutes 10 seconds of the first trigger (ie, 10: 00: 10,10: 05: 10,10: 10 : 10, etc.)
  ? 30 * * * * every half a minute trigger
  ? * * * 30 10 10 per hour minutes and 30 seconds to trigger
  30101 * *? 1:10:30 trigger day
  3010120 *? 20th of every month 1:10:30 trigger
  301,012,010? * Every October 20, 1:10 minutes and 30 seconds to trigger
  301,012,010? 2011 October 2011 20 1:10:30 trigger
  30101? 10 * 2011 October 2011 day 1:10:30 trigger
  30 10 1? 10 SUN 2011 October 2011 Sunday 1:10:30 trigger
  15,30,45 * * * *?Every 15 seconds, the trigger for 30 seconds, 45 seconds
  15-45 * * * *? Within 15-45 seconds, triggers per second
  15/5 * * * *? Every 15 seconds to trigger the start of every minute, every 5 seconds trigger a
  15-30 / 5 * * * *? start trigger between 15 to 30 seconds every minute, every 5 seconds to trigger a
  0 0/3 * * *? 0 minutes 0 seconds every hour, every three minutes to trigger a
  0 15 10? * MON- FRI Monday to Friday 10:15:00 trigger
  0 15 10 L *? a month on the last day of 10:15:00 trigger
  0 15 10 LW *? per the last working day of the month 10:15:00 trigger
  0 15 10? * 5L every month on the last Thursday of 10:15:00 trigger
  01510? * 5 # 3 of the third week of every month Thursday the 10:15:00 trigger

 

Guess you like

Origin www.cnblogs.com/heqiyoujing/p/11106881.html