SpringBoot定时任务

SpringBoot定时任务

  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 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!