zoukankan      html  css  js  c++  java
  • Spring @SCHEDULED(CRON = "0 0 * * * ?")实现定时任务

    Spring配置文件xmlns加入

    xmlns:task="http://www.springframework.org/schema/task"

    xsi:schemaLocation中加入

         http://www.springframework.org/schema/task
         http://www.springframework.org/schema/task/spring-task-3.0.xsd"

    spring扫描注解的配置

    <context:component-scan base-package="com.imwoniu.*" />

    任务扫描注解

    <task:executor id="executor" pool-size="5" />  
    <task:scheduler id="scheduler" pool-size="10" />  
    <task:annotation-driven executor="executor" scheduler="scheduler" />

    代码实现:

    注解@Scheduled 可以作为一个触发源添加到一个方法中,例如,以下的方法将以一个固定延迟时间5秒钟调用一次执行,这个周期是以上一个调用任务的完成时间为基准,在上一个任务完成之后,5s后再次执行:

    @Scheduled(fixedDelay = 5000)
    public void doSomething() { 
        // something that should execute periodically
    }

    如果需要以固定速率执行,只要将注解中指定的属性名称改成fixedRate即可,以下方法将以一个固定速率5s来调用一次执行,这个周期是以上一个任务开始时间为基准,从上一任务开始执行后5s再次调用:

    @Scheduled(fixedRate = 5000)
    public void doSomething() { 
        // something that should execute periodically
    }

    如果简单的定期调度不能满足,那么cron表达式提供了可能

    package com.imwoniu.task;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class TaskDemo {
        
        @Scheduled(cron = "0 0 2 * * ?")  //每天凌晨两点执行
            void doSomethingWith(){
                logger.info("定时任务开始......");
                long begin = System.currentTimeMillis();
            
                //执行数据库操作了哦...
            
                long end = System.currentTimeMillis();
                logger.info("定时任务结束,共耗时:[" + (end-begin) / 1000 + "]秒");
        }
    }
    

      

    按顺序依次为

    秒(0~59)

    分钟(0~59)

    小时(0~23)

    天(月)(0~31,但是你需要考虑你月的天数)

    月(0~11)

    天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

    7.年份(1970-2099)

  • 相关阅读:
    cento7快速修改主机名和修改root密码
    [goolegke]nginxingress建立测试
    filebeat安装读取nginx json日志
    MySQL索引背后的数据结构及算法原理
    Lua脚本在redis分布式锁场景的运用
    Sentinel实现限流
    java架构技术流程图
    mybatis数据加解密处理方案
    vue 自定义代码片段
    node项目vue 自动化部署之pm2
  • 原文地址:https://www.cnblogs.com/zqyanywn/p/7700126.html
Copyright © 2011-2022 走看看