001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scheduling.annotation;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Repeatable;
022import java.lang.annotation.Retention;
023import java.lang.annotation.RetentionPolicy;
024import java.lang.annotation.Target;
025
026import org.springframework.scheduling.config.ScheduledTaskRegistrar;
027
028/**
029 * Annotation that marks a method to be scheduled. Exactly one of the
030 * {@link #cron}, {@link #fixedDelay}, or {@link #fixedRate} attributes must be
031 * specified.
032 *
033 * <p>The annotated method must expect no arguments. It will typically have
034 * a {@code void} return type; if not, the returned value will be ignored
035 * when called through the scheduler.
036 *
037 * <p>Processing of {@code @Scheduled} annotations is performed by
038 * registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be
039 * done manually or, more conveniently, through the {@code <task:annotation-driven/>}
040 * element or @{@link EnableScheduling} annotation.
041 *
042 * <p>This annotation may be used as a <em>meta-annotation</em> to create custom
043 * <em>composed annotations</em> with attribute overrides.
044 *
045 * @author Mark Fisher
046 * @author Juergen Hoeller
047 * @author Dave Syer
048 * @author Chris Beams
049 * @since 3.0
050 * @see EnableScheduling
051 * @see ScheduledAnnotationBeanPostProcessor
052 * @see Schedules
053 */
054@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
055@Retention(RetentionPolicy.RUNTIME)
056@Documented
057@Repeatable(Schedules.class)
058public @interface Scheduled {
059
060        /**
061         * A special cron expression value that indicates a disabled trigger: {@value}.
062         * <p>This is primarily meant for use with <code>${...}</code> placeholders,
063         * allowing for external disabling of corresponding scheduled methods.
064         * @since 5.1
065         * @see ScheduledTaskRegistrar#CRON_DISABLED
066         */
067        String CRON_DISABLED = ScheduledTaskRegistrar.CRON_DISABLED;
068
069
070        /**
071         * A cron-like expression, extending the usual UN*X definition to include triggers
072         * on the second, minute, hour, day of month, month, and day of week.
073         * <p>For example, {@code "0 * * * * MON-FRI"} means once per minute on weekdays
074         * (at the top of the minute - the 0th second).
075         * <p>The fields read from left to right are interpreted as follows.
076         * <ul>
077         * <li>second</li>
078         * <li>minute</li>
079         * <li>hour</li>
080         * <li>day of month</li>
081         * <li>month</li>
082         * <li>day of week</li>
083         * </ul>
084         * <p>The special value {@link #CRON_DISABLED "-"} indicates a disabled cron
085         * trigger, primarily meant for externally specified values resolved by a
086         * <code>${...}</code> placeholder.
087         * @return an expression that can be parsed to a cron schedule
088         * @see org.springframework.scheduling.support.CronSequenceGenerator
089         */
090        String cron() default "";
091
092        /**
093         * A time zone for which the cron expression will be resolved. By default, this
094         * attribute is the empty String (i.e. the server's local time zone will be used).
095         * @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)},
096         * or an empty String to indicate the server's default time zone
097         * @since 4.0
098         * @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone)
099         * @see java.util.TimeZone
100         */
101        String zone() default "";
102
103        /**
104         * Execute the annotated method with a fixed period in milliseconds between the
105         * end of the last invocation and the start of the next.
106         * @return the delay in milliseconds
107         */
108        long fixedDelay() default -1;
109
110        /**
111         * Execute the annotated method with a fixed period in milliseconds between the
112         * end of the last invocation and the start of the next.
113         * @return the delay in milliseconds as a String value, e.g. a placeholder
114         * or a {@link java.time.Duration#parse java.time.Duration} compliant value
115         * @since 3.2.2
116         */
117        String fixedDelayString() default "";
118
119        /**
120         * Execute the annotated method with a fixed period in milliseconds between
121         * invocations.
122         * @return the period in milliseconds
123         */
124        long fixedRate() default -1;
125
126        /**
127         * Execute the annotated method with a fixed period in milliseconds between
128         * invocations.
129         * @return the period in milliseconds as a String value, e.g. a placeholder
130         * or a {@link java.time.Duration#parse java.time.Duration} compliant value
131         * @since 3.2.2
132         */
133        String fixedRateString() default "";
134
135        /**
136         * Number of milliseconds to delay before the first execution of a
137         * {@link #fixedRate} or {@link #fixedDelay} task.
138         * @return the initial delay in milliseconds
139         * @since 3.2
140         */
141        long initialDelay() default -1;
142
143        /**
144         * Number of milliseconds to delay before the first execution of a
145         * {@link #fixedRate} or {@link #fixedDelay} task.
146         * @return the initial delay in milliseconds as a String value, e.g. a placeholder
147         * or a {@link java.time.Duration#parse java.time.Duration} compliant value
148         * @since 3.2.2
149         */
150        String initialDelayString() default "";
151
152}