java regular tasks of Scheduled Scheduled annotation notes java regular tasks of

Transfer: https: //www.cnblogs.com/caiguaismine/p/9920967.html 【Author: dish chicken de home  ]

Cron Expression Builder Online  : http: //cron.qqe2.com

java regular tasks of Scheduled notes

 

Based on a demo Spring boot of:

Java configuration support for Scheduled to open an account of

Copy the code
Copy the code
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class ScheduleConfig {
}
Copy the code
Copy the code

A timing example:

Copy the code
Copy the 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//将这个对象加入Spring容器中
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 the code
Copy the code

 

 

 

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

According to the order for the

Sec (0 to 59)

 
 
 
 
 

Min (0 - 59)

H (0 to 23)

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

Month (0 to 11)

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

7. Year (1970-2099)

in which each element can be a value (e.g., 6), a continuous section (9-12), a time interval (8-18 / 4) (/ indicates every 4 hours), a list (1,3,5), the wildcard. As the "day of the month" and "date of the week," these two elements are mutually exclusive, and must be for one of the settings?.

00 10,14,16 * *? 10 am every day, 14:00, 4:00
0 0/30 * 9-17 *? Inward nine to five hours every half hour
0 0 12? * WED represent each Wednesday 12:00 
"0012 * *?" 12:00 every day trigger 
"01510? * *" at 10:15 am every day trigger 
"01510 * *?" 10:15 am every day trigger 
"01510 * *? * "at 10:15 am every day trigger 
" 01510 * *? 2005 "2005 10:15 am every day trigger 
" 0 * 14 * *? "1 period per day at 2:00 pm to 2:59 pm minute when the 
"0 0/5 14 * *?" trigger every 5 minutes every afternoon 2:00 to 2:55 pm during the 
"0 0/5 * 14, 18 *?" during the 2:00 to 2:55 every afternoon every five minutes and trigger 6:00 pm to 6:55 during the 
"00-514 * *?" trigger per minute during the day at 2:00 pm to 2:05 pm 
"0 10,44 14? 3 WED" Wednesday afternoon of March each year 2:10 and 2:44 trigger 
"? 0 15 10 * MON- FRI" Monday to Friday 10:15 am to trigger 
"0151015 *?" 15th of each month 10 am: 15 trigger 
"0 15 10 L *?" The last day of each month at 10:15 am triggering
"0 15 10? * 6L" the last Friday of each month at 10:15 am triggering 
"0 15 10? * 6L 2002-2005 " 2002 years to 2005, the last Friday of each month at 10:15 am triggering 
"01510? * 6 # 3" is the third Friday of each month at 10:15 am triggering 

Some sub-expressions can contain a range or list

For example: subexpression ( days (weeks) ) can be "MON-FRI", "MON , WED, FRI", "MON-WED, SAT"

"*" Character represents all possible values

Therefore, the "*" in the sub-expression ( months represent the meanings of each month), the "*" in the sub-expression ( days (weeks) ) represent each day of the week

 

"/" Character is used to specify the value increment

For example: In sub-expressions (minutes) in the "0/15" starts from 0 minutes, every 15 minutes

         In the sub-expression (minutes) in the "3/20" indicates that from the first three minutes, every 20 minutes (and it "3,23,43") as meaning


"?" Character is used only day (month) and day (week) two sub-expression that do not specify a value

When one of the two sub-expressions which are assigned a value in the future, in order to avoid conflict, we need to set the value of the other sub-expression '? "

 

"L" character is used only day (month) and day (week) two sub-expressions, which is an abbreviation of the word "last" of

But it meaning in the two sub-expressions is different.

Day (month) sub-expression, "L" represents the last day of the month

In the days (weeks) from the expression, "L" represents the last day of the week, which is the SAT

If there are specific content in the "L" before, it has other meanings of

For example: "6L" the reciprocal of the sixth day of the month, "FRIL" represents the best Friday of the month

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

 

 

Field allows values ​​allow 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   , - * /

@Scheduled (fixedRate =  5000)  // with a fixed delay time of 5 seconds to call the first performance  // this cycle is more than one task start time ## ## as a reference, from a task to be started after 5s called again:
 
 

Based on a demo Spring boot of:

Java configuration support for Scheduled to open an account of

Copy the code
Copy the code
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class ScheduleConfig {
}
Copy the code
Copy the code

A timing example:

Copy the code
Copy the 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//将这个对象加入Spring容器中
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 the code
Copy the code

 

 

 

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

According to the order for the

Sec (0 to 59)

 
 
 
 
 

Min (0 - 59)

H (0 to 23)

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

Month (0 to 11)

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

7. Year (1970-2099)

in which each element can be a value (e.g., 6), a continuous section (9-12), a time interval (8-18 / 4) (/ indicates every 4 hours), a list (1,3,5), the wildcard. As the "day of the month" and "date of the week," these two elements are mutually exclusive, and must be for one of the settings?.

00 10,14,16 * *? 10 am every day, 14:00, 4:00
0 0/30 * 9-17 *? Inward nine to five hours every half hour
0 0 12? * WED represent each Wednesday 12:00 
"0012 * *?" 12:00 every day trigger 
"01510? * *" at 10:15 am every day trigger 
"01510 * *?" 10:15 am every day trigger 
"01510 * *? * "at 10:15 am every day trigger 
" 01510 * *? 2005 "2005 10:15 am every day trigger 
" 0 * 14 * *? "1 period per day at 2:00 pm to 2:59 pm minute when the 
"0 0/5 14 * *?" trigger every 5 minutes every afternoon 2:00 to 2:55 pm during the 
"0 0/5 * 14, 18 *?" during the 2:00 to 2:55 every afternoon every five minutes and trigger 6:00 pm to 6:55 during the 
"00-514 * *?" trigger per minute during the day at 2:00 pm to 2:05 pm 
"0 10,44 14? 3 WED" Wednesday afternoon of March each year 2:10 and 2:44 trigger 
"? 0 15 10 * MON- FRI" Monday to Friday 10:15 am to trigger 
"0151015 *?" 15th of each month 10 am: 15 trigger 
"0 15 10 L *?" The last day of each month at 10:15 am triggering
"0 15 10? * 6L" the last Friday of each month at 10:15 am triggering 
"0 15 10? * 6L 2002-2005 " 2002 years to 2005, the last Friday of each month at 10:15 am triggering 
"01510? * 6 # 3" is the third Friday of each month at 10:15 am triggering 

Some sub-expressions can contain a range or list

For example: subexpression ( days (weeks) ) can be "MON-FRI", "MON , WED, FRI", "MON-WED, SAT"

"*" Character represents all possible values

Therefore, the "*" in the sub-expression ( months represent the meanings of each month), the "*" in the sub-expression ( days (weeks) ) represent each day of the week

 

"/" Character is used to specify the value increment

For example: In sub-expressions (minutes) in the "0/15" starts from 0 minutes, every 15 minutes

         In the sub-expression (minutes) in the "3/20" indicates that from the first three minutes, every 20 minutes (and it "3,23,43") as meaning


"?" Character is used only day (month) and day (week) two sub-expression that do not specify a value

When one of the two sub-expressions which are assigned a value in the future, in order to avoid conflict, we need to set the value of the other sub-expression '? "

 

"L" character is used only day (month) and day (week) two sub-expressions, which is an abbreviation of the word "last" of

But it meaning in the two sub-expressions is different.

Day (month) sub-expression, "L" represents the last day of the month

In the days (weeks) from the expression, "L" represents the last day of the week, which is the SAT

If there are specific content in the "L" before, it has other meanings of

For example: "6L" the reciprocal of the sixth day of the month, "FRIL" represents the best Friday of the month

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

 

 

Field allows values ​​allow 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   , - * /

@Scheduled (fixedRate =  5000)  // with a fixed delay time of 5 seconds to call the first performance  // this cycle is more than one task start time ## ## as a reference, from a task to be started after 5s called again:

Guess you like

Origin www.cnblogs.com/hanxiaofei/p/12085269.html