通过Schedule开启定时任务

1. 开启注解EnableSchedlingSpringBootApplicationEnableSchedulingpublic class Startup {public static void main(String[] args

1. 开启注解@EnableSchedling

@SpringBootApplication
@EnableScheduling
public class Startup {
    public static void main(String[] args) {
        SpringApplication.run(Startup.class, args);
    }
}

2. 固定速率,按时触发

@Scheduled(fixedRate = 1000)
public void fixRate() throws InterruptedException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("fixRate: " + sdf.format(new Date()));
    Thread.sleep(1000);
}

3. 固定间隔,等待前一次触发完毕后再执行

@Scheduled(fixedDelay = 1000)
public void fixDelay() throws InterruptedException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("fixDelay: " + sdf.format(new Date()));
    Thread.sleep(1000);
}

 

当然,也支持cron表达式

@Scheduled(cron = "* * 21 * * ?")
public void cron() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("cron: " + sdf.format(new Date()));
}

 

 

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755002862a5225650.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信