首页 > 其他 > 详细

注解配置定时器Scheduling

时间:2021-04-29 15:35:01      阅读:12      评论:0      收藏:0      [点我收藏+]

注解配置定时器配置

package com.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @author zhujianming
 * @date 2021-04-29 09:56
 */
@EnableScheduling
@Configuration
public class SchedulingConfig {

}

配置多定时器

package com.demo;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
@Service
public class TestService2 {
 
    private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
 
    //初始延迟1秒,每隔2秒
    @Scheduled(fixedRateString = "2000",initialDelay = 1000)
    public void testFixedRate(){
        System.out.println("fixedRateString,当前时间:" +format.format(new Date()));
    }
 
    //每次执行完延迟2秒
    @Scheduled(fixedDelayString= "2000")
    public void testFixedDelay(){
        System.out.println("fixedDelayString,当前时间:" +format.format(new Date()));
    }
 
    //每隔3秒执行一次
    @Scheduled(cron="0/3 * * * * ?")
    public void testCron(){
        System.out.println("cron,当前时间:" +format.format(new Date()));
    }
}

执行结果

技术分享图片

 

注解配置定时器Scheduling

原文:https://www.cnblogs.com/nming/p/14716856.html

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