001/*
002 * Copyright 2002-2017 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.concurrent;
018
019import java.util.concurrent.TimeUnit;
020
021/**
022 * JavaBean that describes a scheduled executor task, consisting of the
023 * {@link Runnable} and a delay plus period. The period needs to be specified;
024 * there is no point in a default for it.
025 *
026 * <p>The {@link java.util.concurrent.ScheduledExecutorService} does not offer
027 * more sophisticated scheduling options such as cron expressions.
028 * Consider using {@link ThreadPoolTaskScheduler} for such needs.
029 *
030 * <p>Note that the {@link java.util.concurrent.ScheduledExecutorService} mechanism
031 * uses a {@link Runnable} instance that is shared between repeated executions,
032 * in contrast to Quartz which creates a new Job instance for each execution.
033 *
034 * @author Juergen Hoeller
035 * @since 2.0
036 * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
037 * @see java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
038 */
039public class ScheduledExecutorTask {
040
041        private Runnable runnable;
042
043        private long delay = 0;
044
045        private long period = -1;
046
047        private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
048
049        private boolean fixedRate = false;
050
051
052        /**
053         * Create a new ScheduledExecutorTask,
054         * to be populated via bean properties.
055         * @see #setDelay
056         * @see #setPeriod
057         * @see #setFixedRate
058         */
059        public ScheduledExecutorTask() {
060        }
061
062        /**
063         * Create a new ScheduledExecutorTask, with default
064         * one-time execution without delay.
065         * @param executorTask the Runnable to schedule
066         */
067        public ScheduledExecutorTask(Runnable executorTask) {
068                this.runnable = executorTask;
069        }
070
071        /**
072         * Create a new ScheduledExecutorTask, with default
073         * one-time execution with the given delay.
074         * @param executorTask the Runnable to schedule
075         * @param delay the delay before starting the task for the first time (ms)
076         */
077        public ScheduledExecutorTask(Runnable executorTask, long delay) {
078                this.runnable = executorTask;
079                this.delay = delay;
080        }
081
082        /**
083         * Create a new ScheduledExecutorTask.
084         * @param executorTask the Runnable to schedule
085         * @param delay the delay before starting the task for the first time (ms)
086         * @param period the period between repeated task executions (ms)
087         * @param fixedRate whether to schedule as fixed-rate execution
088         */
089        public ScheduledExecutorTask(Runnable executorTask, long delay, long period, boolean fixedRate) {
090                this.runnable = executorTask;
091                this.delay = delay;
092                this.period = period;
093                this.fixedRate = fixedRate;
094        }
095
096
097        /**
098         * Set the Runnable to schedule as executor task.
099         */
100        public void setRunnable(Runnable executorTask) {
101                this.runnable = executorTask;
102        }
103
104        /**
105         * Return the Runnable to schedule as executor task.
106         */
107        public Runnable getRunnable() {
108                return this.runnable;
109        }
110
111        /**
112         * Set the delay before starting the task for the first time,
113         * in milliseconds. Default is 0, immediately starting the
114         * task after successful scheduling.
115         */
116        public void setDelay(long delay) {
117                this.delay = delay;
118        }
119
120        /**
121         * Return the delay before starting the job for the first time.
122         */
123        public long getDelay() {
124                return this.delay;
125        }
126
127        /**
128         * Set the period between repeated task executions, in milliseconds.
129         * <p>Default is -1, leading to one-time execution. In case of a positive value,
130         * the task will be executed repeatedly, with the given interval in-between executions.
131         * <p>Note that the semantics of the period value vary between fixed-rate and
132         * fixed-delay execution.
133         * <p><b>Note:</b> A period of 0 (for example as fixed delay) is <i>not</i> supported,
134         * simply because {@code java.util.concurrent.ScheduledExecutorService} itself
135         * does not support it. Hence a value of 0 will be treated as one-time execution;
136         * however, that value should never be specified explicitly in the first place!
137         * @see #setFixedRate
138         * @see #isOneTimeTask()
139         * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, java.util.concurrent.TimeUnit)
140         */
141        public void setPeriod(long period) {
142                this.period = period;
143        }
144
145        /**
146         * Return the period between repeated task executions.
147         */
148        public long getPeriod() {
149                return this.period;
150        }
151
152        /**
153         * Is this task only ever going to execute once?
154         * @return {@code true} if this task is only ever going to execute once
155         * @see #getPeriod()
156         */
157        public boolean isOneTimeTask() {
158                return (this.period <= 0);
159        }
160
161        /**
162         * Specify the time unit for the delay and period values.
163         * Default is milliseconds ({@code TimeUnit.MILLISECONDS}).
164         * @see java.util.concurrent.TimeUnit#MILLISECONDS
165         * @see java.util.concurrent.TimeUnit#SECONDS
166         */
167        public void setTimeUnit(TimeUnit timeUnit) {
168                this.timeUnit = (timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS);
169        }
170
171        /**
172         * Return the time unit for the delay and period values.
173         */
174        public TimeUnit getTimeUnit() {
175                return this.timeUnit;
176        }
177
178        /**
179         * Set whether to schedule as fixed-rate execution, rather than
180         * fixed-delay execution. Default is "false", that is, fixed delay.
181         * <p>See ScheduledExecutorService javadoc for details on those execution modes.
182         * @see java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
183         * @see java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
184         */
185        public void setFixedRate(boolean fixedRate) {
186                this.fixedRate = fixedRate;
187        }
188
189        /**
190         * Return whether to schedule as fixed-rate execution.
191         */
192        public boolean isFixedRate() {
193                return this.fixedRate;
194        }
195
196}