1 16 17 package org.springframework.scheduling.commonj; 18 19 import java.util.Iterator ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 23 import javax.naming.NamingException ; 24 25 import commonj.timers.Timer; 26 import commonj.timers.TimerManager; 27 28 import org.springframework.beans.factory.DisposableBean; 29 import org.springframework.beans.factory.FactoryBean; 30 import org.springframework.beans.factory.InitializingBean; 31 import org.springframework.context.Lifecycle; 32 import org.springframework.jndi.JndiLocatorSupport; 33 34 57 public class TimerManagerFactoryBean extends JndiLocatorSupport 58 implements FactoryBean, InitializingBean, DisposableBean, Lifecycle { 59 60 private TimerManager timerManager; 61 62 private String timerManagerName; 63 64 private boolean shared = false; 65 66 private ScheduledTimerListener[] scheduledTimerListeners; 67 68 private final List timers = new LinkedList (); 69 70 71 79 public void setTimerManager(TimerManager timerManager) { 80 this.timerManager = timerManager; 81 } 82 83 90 public void setTimerManagerName(String timerManagerName) { 91 this.timerManagerName = timerManagerName; 92 } 93 94 118 public void setShared(boolean shared) { 119 this.shared = shared; 120 } 121 122 130 public void setScheduledTimerListeners(ScheduledTimerListener[] scheduledTimerListeners) { 131 this.scheduledTimerListeners = scheduledTimerListeners; 132 } 133 134 135 139 public void afterPropertiesSet() throws NamingException { 140 if (this.timerManager == null) { 141 if (this.timerManagerName == null) { 142 throw new IllegalArgumentException ("Either 'timerManager' or 'timerManagerName' must be specified"); 143 } 144 this.timerManager = (TimerManager) lookup(this.timerManagerName, TimerManager.class); 145 } 146 147 for (int i = 0; i < this.scheduledTimerListeners.length; i++) { 148 ScheduledTimerListener scheduledTask = this.scheduledTimerListeners[i]; 149 Timer timer = null; 150 if (scheduledTask.isOneTimeTask()) { 151 timer = this.timerManager.schedule(scheduledTask.getTimerListener(), scheduledTask.getDelay()); 152 } 153 else { 154 if (scheduledTask.isFixedRate()) { 155 timer = this.timerManager.scheduleAtFixedRate( 156 scheduledTask.getTimerListener(), scheduledTask.getDelay(), scheduledTask.getPeriod()); 157 } 158 else { 159 timer = this.timerManager.schedule( 160 scheduledTask.getTimerListener(), scheduledTask.getDelay(), scheduledTask.getPeriod()); 161 } 162 } 163 this.timers.add(timer); 164 } 165 } 166 167 168 172 public Object getObject() { 173 return this.timerManager; 174 } 175 176 public Class getObjectType() { 177 return (this.timerManager != null ? this.timerManager.getClass() : TimerManager.class); 178 } 179 180 public boolean isSingleton() { 181 return true; 182 } 183 184 185 189 193 public void start() { 194 if (!this.shared) { 195 this.timerManager.resume(); 196 } 197 } 198 199 203 public void stop() { 204 if (!this.shared) { 205 this.timerManager.suspend(); 206 } 207 } 208 209 215 public boolean isRunning() { 216 return (!this.timerManager.isSuspending() && !this.timerManager.isStopping()); 217 } 218 219 220 224 230 public void destroy() { 231 for (Iterator it = this.timers.iterator(); it.hasNext();) { 233 Timer timer = (Timer) it.next(); 234 try { 235 timer.cancel(); 236 } 237 catch (Throwable ex) { 238 logger.warn("Could not cancel CommonJ Timer", ex); 239 } 240 } 241 this.timers.clear(); 242 243 if (!this.shared) { 245 this.timerManager.stop(); 247 } 248 } 249 250 } 251 | Popular Tags |