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.commonj;
018
019import commonj.timers.TimerListener;
020
021import org.springframework.lang.Nullable;
022
023/**
024 * JavaBean that describes a scheduled TimerListener, consisting of
025 * the TimerListener itself (or a Runnable to create a TimerListener for)
026 * and a delay plus period. Period needs to be specified;
027 * there is no point in a default for it.
028 *
029 * <p>The CommonJ TimerManager does not offer more sophisticated scheduling
030 * options such as cron expressions. Consider using Quartz for such
031 * advanced needs.
032 *
033 * <p>Note that the TimerManager uses a TimerListener instance that is
034 * shared between repeated executions, in contrast to Quartz which
035 * instantiates a new Job for each execution.
036 *
037 * @author Juergen Hoeller
038 * @since 2.0
039 * @deprecated as of 5.1, in favor of EE 7's
040 * {@link org.springframework.scheduling.concurrent.DefaultManagedTaskScheduler}
041 */
042@Deprecated
043public class ScheduledTimerListener {
044
045        @Nullable
046        private TimerListener timerListener;
047
048        private long delay = 0;
049
050        private long period = -1;
051
052        private boolean fixedRate = false;
053
054
055        /**
056         * Create a new ScheduledTimerListener,
057         * to be populated via bean properties.
058         * @see #setTimerListener
059         * @see #setDelay
060         * @see #setPeriod
061         * @see #setFixedRate
062         */
063        public ScheduledTimerListener() {
064        }
065
066        /**
067         * Create a new ScheduledTimerListener, with default
068         * one-time execution without delay.
069         * @param timerListener the TimerListener to schedule
070         */
071        public ScheduledTimerListener(TimerListener timerListener) {
072                this.timerListener = timerListener;
073        }
074
075        /**
076         * Create a new ScheduledTimerListener, with default
077         * one-time execution with the given delay.
078         * @param timerListener the TimerListener to schedule
079         * @param delay the delay before starting the task for the first time (ms)
080         */
081        public ScheduledTimerListener(TimerListener timerListener, long delay) {
082                this.timerListener = timerListener;
083                this.delay = delay;
084        }
085
086        /**
087         * Create a new ScheduledTimerListener.
088         * @param timerListener the TimerListener to schedule
089         * @param delay the delay before starting the task for the first time (ms)
090         * @param period the period between repeated task executions (ms)
091         * @param fixedRate whether to schedule as fixed-rate execution
092         */
093        public ScheduledTimerListener(TimerListener timerListener, long delay, long period, boolean fixedRate) {
094                this.timerListener = timerListener;
095                this.delay = delay;
096                this.period = period;
097                this.fixedRate = fixedRate;
098        }
099
100        /**
101         * Create a new ScheduledTimerListener, with default
102         * one-time execution without delay.
103         * @param timerTask the Runnable to schedule as TimerListener
104         */
105        public ScheduledTimerListener(Runnable timerTask) {
106                setRunnable(timerTask);
107        }
108
109        /**
110         * Create a new ScheduledTimerListener, with default
111         * one-time execution with the given delay.
112         * @param timerTask the Runnable to schedule as TimerListener
113         * @param delay the delay before starting the task for the first time (ms)
114         */
115        public ScheduledTimerListener(Runnable timerTask, long delay) {
116                setRunnable(timerTask);
117                this.delay = delay;
118        }
119
120        /**
121         * Create a new ScheduledTimerListener.
122         * @param timerTask the Runnable to schedule as TimerListener
123         * @param delay the delay before starting the task for the first time (ms)
124         * @param period the period between repeated task executions (ms)
125         * @param fixedRate whether to schedule as fixed-rate execution
126         */
127        public ScheduledTimerListener(Runnable timerTask, long delay, long period, boolean fixedRate) {
128                setRunnable(timerTask);
129                this.delay = delay;
130                this.period = period;
131                this.fixedRate = fixedRate;
132        }
133
134
135        /**
136         * Set the Runnable to schedule as TimerListener.
137         * @see DelegatingTimerListener
138         */
139        public void setRunnable(Runnable timerTask) {
140                this.timerListener = new DelegatingTimerListener(timerTask);
141        }
142
143        /**
144         * Set the TimerListener to schedule.
145         */
146        public void setTimerListener(@Nullable TimerListener timerListener) {
147                this.timerListener = timerListener;
148        }
149
150        /**
151         * Return the TimerListener to schedule.
152         */
153        @Nullable
154        public TimerListener getTimerListener() {
155                return this.timerListener;
156        }
157
158        /**
159         * Set the delay before starting the task for the first time,
160         * in milliseconds. Default is 0, immediately starting the
161         * task after successful scheduling.
162         * <p>If the "firstTime" property is specified, this property will be ignored.
163         * Specify one or the other, not both.
164         */
165        public void setDelay(long delay) {
166                this.delay = delay;
167        }
168
169        /**
170         * Return the delay before starting the job for the first time.
171         */
172        public long getDelay() {
173                return this.delay;
174        }
175
176        /**
177         * Set the period between repeated task executions, in milliseconds.
178         * <p>Default is -1, leading to one-time execution. In case of zero or a
179         * positive value, the task will be executed repeatedly, with the given
180         * interval in-between executions.
181         * <p>Note that the semantics of the period value vary between fixed-rate
182         * and fixed-delay execution.
183         * <p><b>Note:</b> A period of 0 (for example as fixed delay) <i>is</i>
184         * supported, because the CommonJ specification defines this as a legal value.
185         * Hence a value of 0 will result in immediate re-execution after a job has
186         * finished (not in one-time execution like with {@code java.util.Timer}).
187         * @see #setFixedRate
188         * @see #isOneTimeTask()
189         * @see commonj.timers.TimerManager#schedule(commonj.timers.TimerListener, long, long)
190         */
191        public void setPeriod(long period) {
192                this.period = period;
193        }
194
195        /**
196         * Return the period between repeated task executions.
197         */
198        public long getPeriod() {
199                return this.period;
200        }
201
202        /**
203         * Is this task only ever going to execute once?
204         * @return {@code true} if this task is only ever going to execute once
205         * @see #getPeriod()
206         */
207        public boolean isOneTimeTask() {
208                return (this.period < 0);
209        }
210
211        /**
212         * Set whether to schedule as fixed-rate execution, rather than
213         * fixed-delay execution. Default is "false", i.e. fixed delay.
214         * <p>See TimerManager javadoc for details on those execution modes.
215         * @see commonj.timers.TimerManager#schedule(commonj.timers.TimerListener, long, long)
216         * @see commonj.timers.TimerManager#scheduleAtFixedRate(commonj.timers.TimerListener, long, long)
217         */
218        public void setFixedRate(boolean fixedRate) {
219                this.fixedRate = fixedRate;
220        }
221
222        /**
223         * Return whether to schedule as fixed-rate execution.
224         */
225        public boolean isFixedRate() {
226                return this.fixedRate;
227        }
228
229}