SpringBoot定時任務

www88jmpcom17I76934555發表於2021-03-29
  1. 在啟動類新增註解@EnableScheduling

    `import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.scheduling.annotation.EnableScheduling;

    @SpringBootApplication
    @EnableScheduling
    public class MarketingApplication {

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

    }
    `

  2. 編寫具體實現,新增@Component和@Scheduled註解

    `import com.joyoung.marketing.service.ChannelSystemService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;

    import java.util.Date;

    /**

    • @author 汪立坤

    • @version 1.0

    • @description

    • @create 2021/3/26

    • /
      @Component
      @Slf4j
      public class SyncSystemChannelSchedule {

      @Autowired
      private ChannelSystemService channelSystemService;
      /**

      • 每天凌晨1點39執行定時任務
      • /
        @Scheduled(cron = “0 39 1 * * ?”)
        public void printSay() {
        log.info(“執行定時任務:{}”,new Date());
        }
        }

    `

  3. cron表示式

    六個位置,以空格隔開,依次是秒分時日月周,如果有第七位代表年

    具體用法可以百度

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章