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.Timer;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.springframework.beans.factory.DisposableBean;
  21. import org.springframework.beans.factory.FactoryBean;
  22. import org.springframework.beans.factory.InitializingBean;
  23. /**
  24. * FactoryBean that sets up a J2SE Timer and exposes it for bean references.
  25. *
  26. * <p>Allows registration of ScheduledTimerTasks, automatically starting
  27. * the timer on initialization and cancelling it on destruction.
  28. * In typical scenarios, there is no need to access the Timer instance
  29. * itself in application code.
  30. *
  31. * <p>Note that Timer uses a TimerTask instance that is shared between
  32. * repeated executions, in contrast to Quartz which instantiates a new Job
  33. * for each execution.
  34. *
  35. * @author Juergen Hoeller
  36. * @since 19.02.2004
  37. * @see ScheduledTimerTask
  38. * @see java.util.Timer
  39. * @see java.util.TimerTask
  40. */
  41. public class TimerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
  42. protected final Log logger = LogFactory.getLog(getClass());
  43. private ScheduledTimerTask[] scheduledTimerTasks;
  44. private boolean daemon = true;
  45. private Timer timer;
  46. /**
  47. * Register a list of ScheduledTimerTask objects with the Timer that
  48. * this FactoryBean creates.
  49. * @see java.util.Timer#schedule(java.util.TimerTask, long, long)
  50. * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
  51. */
  52. public void setScheduledTimerTasks(ScheduledTimerTask[] scheduledTimerTasks) {
  53. this.scheduledTimerTasks = scheduledTimerTasks;
  54. }
  55. /**
  56. * Set whether the timer should use a daemon thread,
  57. * just executing as long as the application itself is running.
  58. * Default is true: In a J2EE environment, the container is in
  59. * control of the application lifecycle.
  60. * @see java.util.Timer#Timer(boolean)
  61. */
  62. public void setDaemon(boolean daemon) {
  63. this.daemon = daemon;
  64. }
  65. public void afterPropertiesSet() {
  66. logger.info("Initializing Timer");
  67. this.timer = createTimer(this.daemon);
  68. for (int i = 0; i < this.scheduledTimerTasks.length; i++) {
  69. ScheduledTimerTask scheduledTask = this.scheduledTimerTasks[i];
  70. if (scheduledTask.isFixedRate()) {
  71. this.timer.scheduleAtFixedRate(scheduledTask.getTimerTask(), scheduledTask.getDelay(),
  72. scheduledTask.getPeriod());
  73. }
  74. else {
  75. this.timer.schedule(scheduledTask.getTimerTask(), scheduledTask.getDelay(),
  76. scheduledTask.getPeriod());
  77. }
  78. }
  79. }
  80. /**
  81. * Create a new Timer instance. Called by afterPropertiesSet.
  82. * Can be overridden in subclasses to provide custom Timer subclasses.
  83. * @return a new Timer instance
  84. * @see #afterPropertiesSet
  85. * @see java.util.Timer#Timer(boolean)
  86. */
  87. protected Timer createTimer(boolean daemon) {
  88. return new Timer(daemon);
  89. }
  90. public Object getObject() {
  91. return this.timer;
  92. }
  93. public Class getObjectType() {
  94. return Timer.class;
  95. }
  96. public boolean isSingleton() {
  97. return true;
  98. }
  99. /**
  100. * This implementation cancels the Timer, stopping all scheduled tasks.
  101. */
  102. public void destroy() {
  103. logger.info("Cancelling Timer");
  104. this.timer.cancel();
  105. }
  106. }