1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.scheduling.timer;
  17. import java.util.TimerTask;
  18. /**
  19. * JavaBean that describes a scheduled TimerTask, consisting of
  20. * the TimerTask itself and a delay plus period. Period needs to
  21. * be specified; there is no point in a default for it.
  22. *
  23. * <p>The J2SE Timer does not offer more sophisticated scheduling
  24. * options like cron expressions. Consider using Quartz for such
  25. * demanding needs.
  26. *
  27. * <p>Note that Timer uses a TimerTask instance that is shared
  28. * between repeated executions, in contrast to Quartz which
  29. * instantiates a new Job for each execution.
  30. *
  31. * @author Juergen Hoeller
  32. * @since 19.02.2004
  33. * @see java.util.TimerTask
  34. * @see java.util.Timer#schedule(TimerTask, long, long)
  35. * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)
  36. */
  37. public class ScheduledTimerTask {
  38. private TimerTask timerTask;
  39. private long delay = 0;
  40. private long period = 0;
  41. private boolean fixedRate = false;
  42. public ScheduledTimerTask() {
  43. }
  44. public ScheduledTimerTask(TimerTask timerTask, long delay, long period, boolean fixedRate) {
  45. this.timerTask = timerTask;
  46. this.delay = delay;
  47. this.period = period;
  48. this.fixedRate = fixedRate;
  49. }
  50. /**
  51. * Set the TimerTask to schedule.
  52. */
  53. public void setTimerTask(TimerTask timerTask) {
  54. this.timerTask = timerTask;
  55. }
  56. /**
  57. * Return the TimerTask to schedule.
  58. */
  59. public TimerTask getTimerTask() {
  60. return timerTask;
  61. }
  62. /**
  63. * Set the delay before starting the task for the first time,
  64. * in milliseconds. Default is 0, immediately starting the
  65. * task after successful scheduling.
  66. */
  67. public void setDelay(long delay) {
  68. this.delay = delay;
  69. }
  70. /**
  71. * Return the delay before starting the job for the first time.
  72. */
  73. public long getDelay() {
  74. return delay;
  75. }
  76. /**
  77. * Set the period between repeated task executions,
  78. * in milliseconds. Default is 0; this property needs to
  79. * be set to a positive value for proper execution.
  80. */
  81. public void setPeriod(long period) {
  82. this.period = period;
  83. }
  84. /**
  85. * Return the period between repeated task executions.
  86. */
  87. public long getPeriod() {
  88. return period;
  89. }
  90. /**
  91. * Set whether to schedule as fixed-rate execution, rather than
  92. * fixed-delay execution. Default is false, i.e. fixed delay.
  93. * See Timer javadoc for details on those execution modes.
  94. * @see java.util.Timer#schedule(TimerTask, long, long)
  95. * @see java.util.Timer#scheduleAtFixedRate(TimerTask, long, long)
  96. */
  97. public void setFixedRate(boolean fixedRate) {
  98. this.fixedRate = fixedRate;
  99. }
  100. /**
  101. * Return whether to schedule as fixed-rate execution.
  102. */
  103. public boolean isFixedRate() {
  104. return fixedRate;
  105. }
  106. }