1 22 package org.jboss.util.threadpool; 23 24 import org.jboss.logging.Logger; 25 26 32 public class BasicTaskWrapper implements TaskWrapper 33 { 34 35 private static final Logger log = Logger.getLogger(BasicTaskWrapper.class); 36 37 38 public static final int TASK_NOT_ACCEPTED = 0; 39 40 41 public static final int TASK_ACCEPTED = 1; 42 43 44 public static final int TASK_STARTED = 2; 45 46 47 public static final int TASK_COMPLETED = 3; 48 49 50 public static final int TASK_REJECTED = -1; 51 52 53 public static final int TASK_STOPPED = -2; 54 55 56 private int state = TASK_NOT_ACCEPTED; 57 58 59 private Object stateLock = new Object (); 60 61 62 private Task task; 63 64 65 private String taskString; 66 67 68 private long startTime; 69 70 71 private long startTimeout; 72 73 74 private long completionTimeout; 75 76 77 private int priority; 78 79 80 private int waitType; 81 82 83 private Thread runThread; 84 85 88 protected BasicTaskWrapper() 89 { 90 } 91 92 98 public BasicTaskWrapper(Task task) 99 { 100 setTask(task); 101 } 102 103 public int getTaskWaitType() 104 { 105 return waitType; 106 } 107 108 public int getTaskPriority() 109 { 110 return priority; 111 } 112 113 public long getTaskStartTimeout() 114 { 115 return startTimeout; 116 } 117 118 public long getTaskCompletionTimeout() 119 { 120 return completionTimeout; 121 } 122 123 public void acceptTask() 124 { 125 synchronized (stateLock) 126 { 127 if (state != TASK_NOT_ACCEPTED) 129 return; 130 } 131 132 if (taskAccepted()) 134 state = TASK_ACCEPTED; 135 else 136 state = TASK_REJECTED; 137 138 synchronized (stateLock) 140 { 141 stateLock.notifyAll(); 142 } 143 } 144 145 public void rejectTask(RuntimeException e) 146 { 147 synchronized (stateLock) 148 { 149 state = TASK_REJECTED; 150 stateLock.notifyAll(); 151 } 152 taskRejected(e); 153 } 154 155 public boolean isComplete() 156 { 157 return state == TASK_COMPLETED; 158 } 159 160 public void stopTask() 161 { 162 boolean started; 163 synchronized (stateLock) 164 { 165 started = (state == TASK_STARTED); 166 state = TASK_STOPPED; 167 } 168 if (started) 169 { 170 if( runThread != null ) 172 { 173 runThread.interrupt(); 174 } 175 taskStop(); 176 } 177 else if( runThread != null && runThread.isInterrupted() ) 178 { 179 182 runThread.stop(); 183 } 184 } 185 186 public void waitForTask() 187 { 188 switch (waitType) 189 { 190 case Task.WAIT_FOR_START: 191 { 192 boolean interrupted = false; 193 synchronized (stateLock) 194 { 195 while (state == TASK_NOT_ACCEPTED || state == TASK_ACCEPTED) 196 { 197 try 198 { 199 stateLock.wait(); 200 } 201 catch (InterruptedException e) 202 { 203 interrupted = true; 204 } 205 } 206 if (interrupted) 207 Thread.currentThread().interrupt(); 208 return; 209 } 210 } 211 default: 212 { 213 return; 214 } 215 } 216 } 217 218 221 public void run() 222 { 223 this.runThread = Thread.currentThread(); 225 226 long runTime = getElapsedTime(); 228 if (startTimeout > 0l && runTime >= startTimeout) 229 { 230 taskRejected(new StartTimeoutException("Start Timeout exceeded for task " + taskString)); 231 return; 232 } 233 234 boolean stopped = false; 236 synchronized (stateLock) 237 { 238 if (state == TASK_STOPPED) 239 { 240 stopped = true; 241 } 242 else 243 { 244 state = TASK_STARTED; 245 taskStarted(); 246 if (waitType == Task.WAIT_FOR_START) 247 stateLock.notifyAll(); 248 } 249 } 250 if (stopped) 251 { 252 taskRejected(new TaskStoppedException("Task stopped for task " + taskString)); 253 return; 254 } 255 256 Throwable throwable = null; 258 try 259 { 260 task.execute(); 261 } 262 catch (Throwable t) 263 { 264 throwable = t; 265 } 266 267 taskCompleted(throwable); 269 270 synchronized (stateLock) 272 { 273 state = TASK_COMPLETED; 274 if (waitType == Task.WAIT_FOR_COMPLETE) 275 stateLock.notifyAll(); 276 } 277 } 278 279 284 protected void setTask(Task task) 285 { 286 if (task == null) 287 throw new IllegalArgumentException ("Null task"); 288 this.task = task; 289 this.taskString = task.toString(); 290 this.startTime = System.currentTimeMillis(); 291 this.waitType = task.getWaitType(); 292 this.priority = task.getPriority(); 293 this.startTimeout = task.getStartTimeout(); 294 this.completionTimeout = task.getCompletionTimeout(); 295 } 296 297 302 protected boolean taskAccepted() 303 { 304 try 305 { 306 task.accepted(getElapsedTime()); 307 return true; 308 } 309 catch (Throwable t) 310 { 311 log.warn("Unexpected error during 'accepted' for task: " + taskString, t); 312 return false; 313 } 314 } 315 316 322 protected boolean taskRejected(RuntimeException e) 323 { 324 try 325 { 326 task.rejected(getElapsedTime(), e); 327 return true; 328 } 329 catch (Throwable t) 330 { 331 log.warn("Unexpected error during 'rejected' for task: " + taskString, t); 332 if (e != null) 333 log.warn("Original reason for rejection of task: " + taskString, e); 334 return false; 335 } 336 } 337 338 343 protected boolean taskStarted() 344 { 345 try 346 { 347 task.started(getElapsedTime()); 348 return true; 349 } 350 catch (Throwable t) 351 { 352 log.warn("Unexpected error during 'started' for task: " + taskString, t); 353 return false; 354 } 355 } 356 357 363 protected boolean taskCompleted(Throwable throwable) 364 { 365 try 366 { 367 task.completed(getElapsedTime(), throwable); 368 return true; 369 } 370 catch (Throwable t) 371 { 372 log.warn("Unexpected error during 'completed' for task: " + taskString, t); 373 if (throwable != null) 374 log.warn("Original error during 'run' for task: " + taskString, throwable); 375 return false; 376 } 377 } 378 379 384 protected boolean taskStop() 385 { 386 try 387 { 388 task.stop(); 389 return true; 390 } 391 catch (Throwable t) 392 { 393 log.warn("Unexpected error during 'stop' for task: " + taskString, t); 394 return false; 395 } 396 } 397 398 403 protected long getElapsedTime() 404 { 405 return System.currentTimeMillis() - startTime; 406 } 407 408 413 protected String getStateString() 414 { 415 switch (state) 416 { 417 case TASK_NOT_ACCEPTED: 418 return "NOT_ACCEPTED"; 419 case TASK_REJECTED: 420 return "REJECTED"; 421 case TASK_ACCEPTED: 422 return "ACCEPTED"; 423 case TASK_STARTED: 424 return "STARTED"; 425 case TASK_STOPPED: 426 return "STOPPED"; 427 case TASK_COMPLETED: 428 return "COMPLETED"; 429 default: 430 return "???"; 431 } 432 } 433 } 434 | Popular Tags |