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