@Scheduled cron expression for Spring scheduled tasks

A demo based on Spring boot:

Account opening support for Scheduled in Java configuration

copy code
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class ScheduleConfig {
}
copy code

A timing example:

copy code
import com.xiaoyi.sns.cache.constant.Constants;
import com.xiaoyi.sns.cache.constant.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * Created by tang.cheng on 2016/9/1.
 */
@Service//Add this object to the Spring container
public class RedisCleaner {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisCleaner.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 3 * * ?")
    public void demoSchedule() {
           LOGGER.info(redisTemplate.hasKey("hello"))
    }
}
copy code

 

 

 

A cron expression has at least 6 (and possibly 7) time elements separated by spaces.

in order of

Seconds (0~59)

minutes (0~59)

hour (0~23)

Day (month) (0~31, but you need to consider the number of days in your month)

Month (0~11)

Day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT)

7. Year (1970-2099)

where each element can be a value (such as 6), a continuous interval (9-12), an interval (8-18/4) (/ means every 4 hours), a List(1,3,5), wildcard. Since the two elements "day of the month" and "day of the week" are mutually exclusive, it is necessary to set one of them?.

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   , - * /
Reprint address: http://biaoming.iteye.com/blog/39532

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326480708&siteId=291194637