1 17 18 package org.sape.carbon.services.scheduler; 19 20 import java.util.Timer ; 21 import java.util.TimerTask ; 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.InvocationTargetException ; 24 25 import org.sape.carbon.core.component.Component; 26 import org.sape.carbon.core.component.FunctionalInterface; 27 import org.sape.carbon.core.config.InvalidConfigurationException; 28 import org.sape.carbon.core.exception.ExceptionUtility; 29 import org.sape.carbon.core.exception.IllegalStateException; 30 31 import org.sape.carbon.services.threadpool.ThreadPool; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 52 public abstract class AbstractTask { 53 54 57 private static Log log = LogFactory.getLog(AbstractTask.class); 58 59 60 private FunctionalInterface schedulableComponent; 61 62 private String scheduledMethodName; 63 64 private Method scheduledMethod; 65 66 private ThreadPool threadPool; 67 68 74 protected AbstractTask( 75 BaseTaskConfiguration taskConfiguration, 76 ThreadPool threadPool) { 77 78 this.schedulableComponent = 79 taskConfiguration.getSchedulableComponent(); 80 this.scheduledMethodName = taskConfiguration.getScheduledMethod(); 81 82 if (this.schedulableComponent == null) { 83 throw new InvalidConfigurationException( 84 this.getClass(), 85 taskConfiguration.getConfigurationName(), 86 "SchedulableComponent", 87 "SchedulableComponent must be specified"); 88 } 89 90 if (!(this.schedulableComponent instanceof Schedulable)) { 91 92 if (this.scheduledMethodName == null) { 93 94 throw new InvalidConfigurationException( 95 this.getClass(), 96 taskConfiguration.getConfigurationName(), 97 "ScheduledMethodName", 98 "If a scheduled component is not an instance of "+ 99 Schedulable.class.getName() + " you must provide " + 100 "the name of a no-argument method to executed."); 101 } 102 } 103 104 if (this.scheduledMethodName != null) { 105 try { 106 this.scheduledMethod = 107 this.schedulableComponent.getClass().getMethod( 108 this.scheduledMethodName,new Class [] {}); 109 } catch (NoSuchMethodException nsme) { 110 throw new InvalidConfigurationException( 111 this.getClass(), 112 taskConfiguration.getConfigurationName(), 113 "ScheduledMethodName", 114 "The provided scheduled method name [" + 115 this.scheduledMethodName + "] was not found to be a " + 116 "no argument method on the configured component [" + 117 ((Component)this.schedulableComponent).getComponentName() + "]."); 118 } 119 } 120 121 this.threadPool = threadPool; 122 } 123 124 132 public abstract void schedule(Timer timer); 133 134 140 protected TimerTask getTask() { 141 InternalTask componentExecutor; 142 143 if (this.scheduledMethod != null) { 144 componentExecutor = new InternalTask(this.schedulableComponent, this.scheduledMethod); 145 } else { 146 componentExecutor = new InternalTask(this.schedulableComponent); 147 } 148 149 if (this.threadPool == null) { 150 return componentExecutor; 151 } else { 152 String taskName = ((Component) this.schedulableComponent).getComponentName(); 153 if (this.scheduledMethodName != null) { 154 taskName = taskName + "." + this.scheduledMethodName; 155 } 156 157 return new TaskQueuer( 158 this.threadPool, 159 componentExecutor, 160 taskName); 161 } 162 } 163 164 168 private static class InternalTask extends TimerTask { 169 170 private FunctionalInterface schedulableComponent; 171 172 private Method method; 173 174 179 public InternalTask(FunctionalInterface schedulableComponent) { 180 this.schedulableComponent = schedulableComponent; 181 } 182 183 public InternalTask(FunctionalInterface schedulableComponent, Method method) { 184 this.schedulableComponent = schedulableComponent; 185 this.method = method; 186 } 187 188 191 public void run() { 192 try { 193 if (log.isInfoEnabled()) { 194 log.info("Running scheduled component [" 195 + ((Component) schedulableComponent).getComponentName() 196 + "]"); 197 } 198 199 if (this.method == null) { 200 ((Schedulable)schedulableComponent).runScheduledTask(); 201 } else { 202 this.method.invoke(this.schedulableComponent, new Object [] {}); 203 } 204 205 } catch (UnrecoverableTaskException ute) { 206 cancel(); 208 if (log.isErrorEnabled()) { 209 log.error("Caught UnrecoverableTaskException while " 210 + "running, scheduled task, task canceled: " 211 + ExceptionUtility.printStackTracesToString(ute)); 212 } 213 } catch (InvocationTargetException ite) { 214 if (log.isWarnEnabled()) { 215 log.warn("Caught Throwable while running scheduled task, " 216 + "task has not been canceled: " 217 + ExceptionUtility.printStackTracesToString(ite.getTargetException())); 218 } 219 } catch (Throwable t) { 220 if (log.isWarnEnabled()) { 221 log.warn("Caught Throwable while running scheduled task, " 222 + "task has not been canceled: " 223 + ExceptionUtility.printStackTracesToString(t)); 224 } 225 } 226 } 227 } 228 229 private static class TaskQueuer extends TimerTask { 230 private ThreadPool threadPool; 231 private InternalTask task; 232 private String taskName; 233 234 public TaskQueuer(ThreadPool threadPool, InternalTask task, String taskName) { 235 this.threadPool = threadPool; 236 this.task = task; 237 this.taskName = taskName; 238 } 239 240 public void run() { 241 this.threadPool.execute(this.task, this.taskName); 242 } 243 244 245 } 246 } 247 | Popular Tags |