34.4.2 @Scheduled 注解
@Scheduled
注解可以和 触发元数据
(trigger metadata)一起添加在方法上。例如,下面这个方法每隔 5 秒就会被调用,5 秒钟的间隔是指从上一次调用的完成之时开始算起。
@Scheduled(fixedDelay=5000)
public void doSomething() {
// 一些需要周期性执行的代码
}
如果你希望定时执行一段代码而非周期性地执行,则只需要将注解中的 fixedDelay 属性名改为 fixedRate 即可。例如,下面这个方法每隔 5 秒就会被调用。此时的 5 秒就是从上一次调用之始开始算起了。
@Scheduled(fixedRate=5000)
public void doSomething() {
// 一些需要定期执行的代码
}
对于需要延迟执行又需要定期执行的代码,可以通过 initialDelay 属性来指定一个延迟时间。例如,下面这个方法会先等待 1 秒,然后才执行,此后每隔 5 秒再执行一次。
@Scheduled(initialDelay=1000, fixedRate=5000)
public void doSomething() {
// 需要周期性执行的代码
}
如果这些内置的属性还满足不了你的需求的话,那你可以通过 cron 表达式来指定你代码的执行时间。例如,下面这个方法只会在工作日才会执行。
@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// 只需要在工作日之时才定期执行的代码
}
小贴士
你可以使用
zone
属性来指定 cron 表达式对应的时区。
要注意的是,被定期执行的方法不能带有入参,也不能有返回值。如果方法需要和其他应用层的对象进行交互的话,应该使用依赖注入的方式来访问。
注意
自 Spring Framework 4.3 版本起,
@Scheduled
方法允许写在任何范围的 bean 中。确保你没有在程序运行时,初始化多个使用了
@Scheduled
注解的类,除非你是真的希望你的周期性任务回调每个实例。。。底下看不懂了,大致意思是不要和@Configurable
注解混用,自己看原文吧。Make sure that you are not initializing multiple instances of the same
@Scheduled
annotation class at runtime, unless you do want to schedule callbacks to each such instance. Related to this, make sure that you do not use@Configurable
on bean classes which are annotated with@Scheduled
and registered as regular Spring beans with the container: You would get double initialization otherwise, once through the container and once through the@Configurable
aspect, with the consequence of each@Scheduled
method being invoked twice.
原文链接 《Spring Framework Reference - The @Scheduled annotation》
34.4.2 The @Scheduled annotation
The @Scheduled
annotation can be added to a method along with trigger metadata. For example, the following method would be invoked every 5 seconds with a fixed delay, meaning that the period will be measured from the completion time of each preceding invocation.
@Scheduled(fixedDelay=5000)
public void doSomething() {
// something that should execute periodically
}
If a fixed rate execution is desired, simply change the property name specified within the annotation. The following would be executed every 5 seconds measured between the successive start times of each invocation.
@Scheduled(fixedRate=5000)
public void doSomething() {
// something that should execute periodically
}
For fixed-delay and fixed-rate tasks, an initial delay may be specified indicating the number of milliseconds to wait before the first execution of the method.
@Scheduled(initialDelay=1000, fixedRate=5000)
public void doSomething() {
// something that should execute periodically
}
If simple periodic scheduling is not expressive enough, then a cron expression may be provided. For example, the following will only execute on weekdays.
@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// something that should execute on weekdays only
}
[Tip]
You can additionally use the
zone
attribute to specify the time zone in which the cron expression will be resolved.
Notice that the methods to be scheduled must have void returns and must not expect any arguments. If the method needs to interact with other objects from the Application Context, then those would typically have been provided through dependency injection.
[Note]
As of Spring Framework 4.3,
@Scheduled
methods are supported on beans of any scope.Make sure that you are not initializing multiple instances of the same
@Scheduled
annotation class at runtime, unless you do want to schedule callbacks to each such instance. Related to this, make sure that you do not use@Configurable
on bean classes which are annotated with@Scheduled
and registered as regular Spring beans with the container: You would get double initialization otherwise, once through the container and once through the@Configurable
aspect, with the consequence of each@Scheduled
method being invoked twice.