spring @Scheduled用法

@Scheduled(cron = "0 5 * * * ?")
org.springframework.scheduling.annotation.Scheduled

/**
 * Annotation that marks a method to be scheduled. Exactly one of the
 * <code>cron</code>, <code>fixedDelay</code>, or <code>fixedRate</code>
 * attributes must be provided.
 *
 * <p>The annotated method must expect no arguments and have a
 * <code>void</code> return type.
 *
 * <p>Processing of {@code @Scheduled} annotations is performed by
 * registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be
 * done manually or, more conveniently, through the {@code <task:annotation-driven/>}
 * element or @{@link EnableScheduling} annotation.
 *

 

Try with:

@Scheduled(cron = "0 1 1 * * ?")
Here you are some example pattern found on spring forum:

* "0 0 * * * *" = the top of every hour of every day.
* "*/10 * * * * *" = every ten seconds.
* "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
* "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
* "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
* "0 0 0 25 12 ?" = every Christmas Day at midnight
Cron expression is represented by six fields:

second, minute, hour, day of month, month, day(s) of week
(*) means match any

*/X means "every X"

? ("no specific value") - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field.

PS: in order to make it works, remember to enable it in your application context: http://docs.spring.io/autorepo/docs/spring-framework/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support

 

 

参考

http://stackoverflow.com/questions/26147044/spring-cron-expression-for-every-day-101am

posted on 2015-12-14 12:59  majia1949  阅读(437)  评论(0编辑  收藏  举报

导航