zoukankan      html  css  js  c++  java
  • Spring Boot任务管理之定时任务

    一、添加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实现声明式事务
    Spring整合MyBatis
    Spring AOP
    代理模式
    Bean的作用域
    Spring的配置
    HQL题目记录以及解题思路--持续更新
    数仓学习之路一:数仓理论
    DBeaver连接Hive遇到的坑
    MySQL常见面试题
  • 原文地址:https://www.cnblogs.com/my-program-life/p/12047526.html
Copyright © 2011-2022 走看看