springboot实现任务调度

创建springboot工程,并在入口内添加@Scheduleing注解,开启任务调度功能


//开启任务调度
@EnableScheduling
@SpringBootApplication
public class ScheduleingApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScheduleingApplication.class, args);
    }
}

创建定时任务类


@Component
public class ScheduleTask {

    private static final Logger log = LoggerFactory.getLogger(ScheduleTask.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    //一小时
    private static final long ONE_HOURS = 60 * 60 * 1000;
    //一天
    private static final long ONE_DAY = 60 * 60 * 1000 *24;
    //5秒
    private static final long FIVE_SECONDS = 5000;
    @Scheduled(fixedRate = FIVE_SECONDS)
    public void scheduledTask() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
    @Scheduled(fixedRate = ONE_DAY)
    public void scheduledTask1() {
        log.info("我是一个每间隔一天执行一次的调度任务");
    }
    @Scheduled(fixedDelay  = ONE_HOURS)
    public void scheduledTask2() {
        log.info("我是一个每间隔一天执行一次的调度任务");
    }
}

测试

这里写图片描述

小结

  • 1.入口内添加@Scheduleing注解。
  • 2.在定时方法上加@Scheduled注解。

猜你喜欢

转载自blog.csdn.net/qq_30065395/article/details/79615233