關於Scheduled的參數
1.corn
2.fixedDelay
3.fixedDelayString
4.fixedRate
5.fixedRateString
6.initialDelay
7.initialDelayString
8.zone
總共有八種參數類型,對於第一種類型一般使用就最熟悉了,是可以控制方法在任意的年月日時分秒上執行,同時不斷循環。比較簡單,網上的說明也比較多,就不做解釋。
fixedRate屬性
該屬性的含義是上一個調用開始后再次調用的延時(不用等待上一次調用完成),這樣就會存在重復執行的問題,所以不是建議使用,但數據量如果不大時在配置的間隔時間內可以執行完也是可以使用的。
fixedRate & fixedRateString
這兩個參數在使用上的作用應該是基本一樣的,只是傳入的參數類型不同,一個是傳入long類型,一個是傳入String類型。
/**
* Execute the annotated method with a fixed period in milliseconds between the
* end of the last invocation and the start of the next.
* 翻譯:在最后一次調用結束和下一次調用結束之間以毫秒為單位執行注解的方法。
* @return the delay in milliseconds
*/
long fixedDelay() default -1;
fixedDelay屬性
該屬性的功效與上面的fixedRate則是相反的,配置了該屬性后會等到方法執行完成后延遲配置的時間再次執行該方法.
fixedDelay & fixedDelayString
這兩個參數在使用上的作用應該是基本一樣的,只是傳入的參數類型不同,一個是傳入long類型,一個是傳入String類型。
/**
* Number of milliseconds to delay before the first execution of a
* 翻譯:在第一次執行之前延遲的毫秒數
* {@link #fixedRate()} or {@link #fixedDelay()} task.
* @return the initial delay in milliseconds
* @since 3.2
*/
long initialDelay() default -1
initialDelay屬性
該屬性跟上面的fixedDelay、fixedRate有着密切的關系,為什么這么說呢?該屬性的作用是第一次執行延遲時間,只是做延遲的設定,並不會控制其他邏輯,所以要配合fixedDelay或者fixedRate來使用
initalDelay&initialDelayString
這兩個是控制方法調用的間隔時間,同樣也是傳入參數類型不同,一個是傳入long類型,一個是傳入String類型。
下面是@Scheduled注解源碼,以及注釋
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scheduling.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* An annotation that marks a method to be scheduled. Exactly one of
* the {@link #cron()}, {@link #fixedDelay()}, or {@link #fixedRate()}
* attributes must be specified.
*
* <p>The annotated method must expect no arguments. It will typically have
* a {@code void} return type; if not, the returned value will be ignored
* when called through the scheduler.
*
* <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.
*
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
* <em>composed annotations</em> with attribute overrides.
*
* @author Mark Fisher
* @author Dave Syer
* @author Chris Beams
* @since 3.0
* @see EnableScheduling
* @see ScheduledAnnotationBeanPostProcessor
* @see Schedules
*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
/**
* A cron-like expression, extending the usual UN*X definition to include
* triggers on the second as well as minute, hour, day of month, month
* and day of week. e.g. {@code "0 * * * * MON-FRI"} means once per minute on
* weekdays (at the top of the minute - the 0th second).
* @return an expression that can be parsed to a cron schedule
* @see org.springframework.scheduling.support.CronSequenceGenerator
*/
String cron() default "";
/**
* A time zone for which the cron expression will be resolved. By default, this
* attribute is the empty String (i.e. the server's local time zone will be used).
* @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)},
* or an empty String to indicate the server's default time zone
* @since 4.0
* @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone)
* @see java.util.TimeZone
*/
String zone() default "";
/**
* Execute the annotated method with a fixed period in milliseconds between the
* end of the last invocation and the start of the next.
* @return the delay in milliseconds
*/
long fixedDelay() default -1;
/**
* Execute the annotated method with a fixed period in milliseconds between the
* end of the last invocation and the start of the next.
* @return the delay in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedDelayString() default "";
/**
* Execute the annotated method with a fixed period in milliseconds between
* invocations.
* @return the period in milliseconds
*/
long fixedRate() default -1;
/**
* Execute the annotated method with a fixed period in milliseconds between
* invocations.
* @return the period in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedRateString() default "";
/**
* Number of milliseconds to delay before the first execution of a
* {@link #fixedRate()} or {@link #fixedDelay()} task.
* @return the initial delay in milliseconds
* @since 3.2
*/
long initialDelay() default -1;
/**
* Number of milliseconds to delay before the first execution of a
* {@link #fixedRate()} or {@link #fixedDelay()} task.
* @return the initial delay in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String initialDelayString() default "";
}
題外話:今天做項目,遇到需要定時處理之類的操作,用的是spring-boot的框架。百度了一下基本都是清一色的cron的配置參數,但是跟我的需求不是很貼合,對@Scheduled注解參數又不是很了解,所以花了些時間,搜集了一下資料,以及結合源碼,百度翻譯等,做了一下總結,學習一下。