SpringBoot定时任务(Spring Schedule )实现方法

 

 

 

FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

fdf.format(new Date())

 

import java.util.Date;

import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.dudu.constant.Constant;

@Component
public class ScheduleJobs {
 
    public final static long SECOND = 1 * 1000;
    FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
  
  
     /**
       * 固定等待时间 @Scheduled(fixedDelay = 时间间隔 )
     * @throws InterruptedException
     */
      @Scheduled(fixedDelay = SECOND * 2)
      public void fixedDelayJob() throws InterruptedException {
        Constant.LOGGER.info("[FixedDelayJob Execute]"+fdf.format(new Date()));
      }

     /**
     * 固定间隔时间 @Scheduled(fixedRate = 时间间隔 )
     */
      @Scheduled(fixedRate = SECOND * 4)
      public void fixedRateJob() {
          Constant.LOGGER.info("[FixedRateJob Execute]"+fdf.format(new Date()));
      }
     
     /**
     * Corn表达式 @Scheduled(cron = Corn表达式)
     */
      @Scheduled(cron = "0/4 * * * * ?")
      public void cronJob() {
          Constant.LOGGER.info("[CronJob Execute]"+fdf.format(new Date()));
      }

}

 

posted @ 2017-11-10 14:42  这个名字想了很久~  阅读(1644)  评论(0编辑  收藏  举报