Spring Cloud微服务实战 打造企业级优惠券系统 3-6 SpringBoot定时任务

0    课程地址

https://coding.imooc.com/lesson/380.html#mid=28274 

https://www.cnblogs.com/1446358788-qq/p/14195372.html

1    浓缩精华
1.1  定时任务需要配置

启动器开启定时任务注解@EnableScheduling

定时任务类定义跑批间隔时间 @Scheduled(fixedRate = 3000)

1.2  定时任务不同使用方式
@Scheduled(fixedRate = 3000)  上一次开始执行时间点之后3000毫秒再执行
@Scheduled(fixedDelay = 3000)  上一次执行完毕时间点之后3s再执行
@Scheduled(initialDelay = 2000, fixedRate = 3000)  第一次延迟2s之后执行, 之后按照每3s执行一次
@Scheduled(cron = "*/3 * * * * ?")  每3s执行一次
2    个人关注
2.1  个人关注  

3.2

3    课程内容
3.1  定时任务需要配置

启动器开启定时任务注解@EnableScheduling

定时任务类定义跑批间隔时间 @Scheduled(fixedRate = 3000)

3.2  jdk8  LocaldateTime的使用

https://blog.csdn.net/weixin_37645838/article/details/92799888

4    代码演练
4.1  定时任务demo
  • 定时任务类scheduled
package com.imooc.springboot.study.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <h1>SpringBoot 定时任务</h1>
 * Created by Qinyi.
 */
@Slf4j
//@Component
public class BootSchedule {

    private final DateTimeFormatter fmt = DateTimeFormatter.ofPattern(
            "HH:mm:ss"
    );

    /**
     * <h2>上一次开始执行时间点之后3000毫秒再执行</h2>
     * */
    @Scheduled(fixedRate = 3000)
    public void schedule01() {
        log.info("schedule01 -> {}", LocalDateTime.now().format(fmt));
    }

    /**
     * <h2>上一次执行完毕时间点之后3s再执行</h2>
     * */
    @Scheduled(fixedDelay = 3000)
    public void schedule02() {
        log.info("schedule02 -> {}", LocalDateTime.now().format(fmt));
    }

    /**
     * <h2>第一次延迟2s之后执行, 之后按照每3s执行一次</h2>
     * */
    @Scheduled(initialDelay = 2000, fixedRate = 3000)
    public void schedule03() {
        log.info("schedule03 -> {}", LocalDateTime.now().format(fmt));
    }

    /**
     * <h2>每3s执行一次</h2>
     * */
    @Scheduled(cron = "*/3 * * * * ?")
    public void schedule04() {
        log.info("schedule04 -> {}", LocalDateTime.now().format(fmt));
    }
}
  • starter类
package com.imooc.springboot.application;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * SpringBootApplication
 *
 * @author 魏豆豆
 * @date 2021/1/13
 */
@SpringBootApplication
@EnableScheduling
public class SpringBootStudyApplication {
    public static void main(String [] args){
        //第一种方式启动
        //SpringApplication.run(SpringBootStudyApplication.class,args);

        //第二种方式启动
        /*SpringApplication springApplication = new SpringApplication(SpringBootStudyApplication.class);
        //关掉打印logo相关日志
        springApplication.setBannerMode(Banner.Mode.OFF);
        //非wub启动,控制台启动后会关闭,不会一直保持开启状态
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        springApplication.run(args);*/

        //第三种方式启动 链式调用
       new SpringApplicationBuilder(SpringBootStudyApplication.class)
                //.bannerMode(Banner.Mode.OFF)
               // .web(WebApplicationType.NONE)
               .run(args);
    }

}
诸葛
原文地址:https://www.cnblogs.com/1446358788-qq/p/14296098.html