zoukankan      html  css  js  c++  java
  • spring boot Scheduled使用

    spring boot: 计划任务@ EnableScheduling和@Scheduled

    spring boot: 计划任务@ EnableScheduling和@Scheduled

    @Scheduled中的参数说明

    1

    2

    3

    4

    5

    6

    7

    @Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;

    @Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;

    @Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;

    @Scheduled(cron="* * * * * ?"):按cron规则执行。

      

    常用Cron表达式(  秒/分/时/日/月/周/年  )

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

    0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时

    0 0 12 ? * WED 表示每个星期三中午12点

    "0 0 12 * * ?" 每天中午12点触发

    "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发

    "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

    "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

    "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发

    "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发

    "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发

    "0 15 10 15 * ?" 每月15日上午10:15触发

    "0 15 10 L * ?" 每月最后一日的上午10:15触发

    "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发

    "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

    "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

    "0 15 10 ? * *" 每天上午10:15触发

    "0 15 10 * * ?" 每天上午10:15触发

    "0 15 10 * * ? *" 每天上午10:15触发

    "0 15 10 * * ? 2005" 2005年的每天上午10:15触发

      

    实例:

    service

    package test.scheduler;
    
    //时间处理,时间格式化
    import java.util.Date;
    import java.text.SimpleDateFormat;
    
    
    //spring计划任务声明(针对方法声明)
    import org.springframework.scheduling.annotation.Scheduled;
    //spring组件声明
    import org.springframework.stereotype.Service;
    
    
    //组件什么
    @Service
    public class SchedulerService {
    
        //创建日期模板
        private static final SimpleDateFormat  dateFormat = new SimpleDateFormat("HH::MM::ss");
        
        
        //每5秒钟执行一次
        @Scheduled(fixedRate = 5000)
        public void reportCurrentTime()
        {
            System.out.println("每五秒执行一次: " + dateFormat.format( new Date() ));
        }
        
        //按照cron属性指定执行时间:秒分时
        @Scheduled(cron = "0 34 18 ? * *")
        public void fixTimeExecution()
        {
            System.out.println("在指定的时间内执行: " + dateFormat.format( new Date()) );
        }
        
        
    }

    config

    package test.scheduler;
    
    //自动引入包
    import org.springframework.context.annotation.ComponentScan;
    //配置类声明
    import org.springframework.context.annotation.Configuration;
    
    //计划任务类声明
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    
    //配置类声明
    @Configuration
    //自动引入包
    @ComponentScan("ch2.scheduler")
    //通过@EnableScheduling开启对计划任务的支持
    @EnableScheduling
    public class TaskSchedulerConfig {
    
        
        
    }

    main

    package test.scheduler;
    //引入容器
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
    
        
        public static void main(String[] args)
        {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
            //SchedulerService schedulerService  = context.getBean(SchedulerService.class);
            
            
        }
    }
    正因为当初对未来做了太多的憧憬,所以对现在的自己尤其失望。生命中曾经有过的所有灿烂,终究都需要用寂寞来偿还。
  • 相关阅读:
    剑指offer_11:二进制中1的个数
    剑指offer_10:矩形覆盖
    spring mvc 访问静态资源
    spring context:component-scan ex
    spring aop配置未生效
    415 Unsupported Media Type
    spring mvc 接收List对象入参
    JIRA甘特图
    JIRA的工时
    JIRA导出工作日志到Excel
  • 原文地址:https://www.cnblogs.com/candlia/p/11919946.html
Copyright © 2011-2022 走看看