@Scheduled(cron = "0 0 * * * ?")
public void saveDailyScoreScheduled() {
try {
logger.info("loadDeviceEvents start>>>>" + new Date());
loadDeviceEvents(ZonedDateTime.now().toEpochSecond(),null);
logger.info("loadDeviceEvents end >>>>" + new Date());
} catch (Exception e) {
logger.error("loadDeviceEvents error >>>>" ,e.toString());

}
}

 

Spring configuration file xmlns added

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

added to xsi:schemaLocation

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

Spring scan annotation configuration

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

Task Scan Notes

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

Code:

The annotation @Scheduled can be added to a method as a trigger source. For example, the following method will be called and executed with a fixed delay time of 5 seconds. This cycle is based on the completion time of the previous calling task. After the previous task is completed After that, execute again after 5s:

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

If you need to execute at a fixed rate, just change the attribute name specified in the annotation to fixedRate. The following method will be executed at a fixed rate of 5s. This cycle is based on the start time of the previous task and starts from the previous task. Call again 5s after execution:

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

 

If simple periodic scheduling cannot be satisfied, then cron expressions provide the possibility

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 + "]秒");
    }
}

 

一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。

按顺序依次为

秒(0~59)

分钟(0~59)

小时(0~23)

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

月(0~11)

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

7.年份(1970-2099)

其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.

0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点 
"0 0 12 * * ?" 每天中午12点触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
"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" Triggered on the last Friday of every month from 2002 to 2005 at 10:15 am 
"0 15 10 ? * 6#3" Triggered on the third Friday of every month at 10:15 am 

Some subexpressions can contain ranges or lists

For example: subexpression ( day (week) ) can be "MON-FRI", "MON, WED, FRI", "MON-WED,SAT"

The "*" character represents all possible values

Therefore, "*" in the subexpression ( month ) represents the meaning of each month, and "*" in the subexpression ( day (week) ) represents each day of the week

 

The "/" character is used to specify the increment of the value

For example: "0/15" in the subexpression (minutes) means starting from the 0th minute, every 15 minutes

         "3/20" in the subexpression (minutes) means starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")


The "?" character is only used for day (month) and day (week) subexpressions, indicating that no value is specified

When one of the two subexpressions is assigned a value, in order to avoid conflicts, the value of the other subexpression needs to be set to "?"

 

The "L" character is only used in day (month) and day (week) subexpressions, it is an abbreviation for the word "last"

But its meaning is different in the two subexpressions.

In the day (month) subexpression, "L" represents the last day of the month

In the day (week) self-expression, "L" represents the last day of the week, which is SAT

If there is specific content before the "L", it has other meanings

For example: "6L" means the 6th last day of the month, "FRIL" means the last Friday of the month

Note: When using the "L" parameter, do not specify a list or range as this can cause problems

 

 

Field Allowed Values ​​Allowed Special Characters
  0-59   , - * /
  0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /