首页 > 编程语言 > 详细

Spring Boot任务管理之定时任务

时间:2019-12-16 10:43:07      阅读:106      评论:0      收藏:0      [点我收藏+]

一、添加ScheduledTaskService服务类

package com.uos.schedule.service;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduledTaskService {
    private static final SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Integer count1 = 1;
    private Integer count2 = 1;
    private Integer count3 = 1;

    @Scheduled(fixedRate = 60000)
    public void scheduledTaskImmediately() {
        System.out.println(String.format("fixedRate第%s次执行,当时时间为:%s", count1++, simpleDataFormat.format(new Date())));
    }


    @Scheduled(fixedRate = 60000)
    public void scheduledTaskAfterSleep() {
        System.out.println(String.format("fixedDelay第%s次执行,当时时间为:%s", count2++, simpleDataFormat.format(new Date())));
    }

    @Scheduled(cron = "0 * * * * *")
    public void scheduledTaskCron() {
        System.out.println(String.format("Cron第%s次执行,当时时间为:%s", count3++, simpleDataFormat.format(new Date())));
    }
}

二、在主程序类中添加@EnableScheduling注解

技术分享图片

 

 三、测试结果

技术分享图片

Spring Boot任务管理之定时任务

原文:https://www.cnblogs.com/my-program-life/p/12047526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!