2.SpringBoot定时任务

 

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

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

@Component
public class SchedulerTask {
   private int count = 0;
   private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

   @Scheduled(cron = "*/6 * * * * ?")
   private void process() {
      System.out.println("this is scheduler task runing  " + (count++));
   }

   @Scheduled(fixedRate = 6000)
   public void reportCurrentTime() {
      System.out.println("现在时间:" + dateFormat.format(new Date()));
   }
}

 

注意点:@Component 注解的使用,定时任务中可以饮用别的Servic方法,调用数据库定时进行操作

 

参考:(包含如何根据数据库配置执行定时任务,多线程定时任务)

https://www.cnblogs.com/mmzs/p/10161936.html

 

 

你可能感兴趣的:(SpringBoot)