| 1 17 18 package org.sape.carbon.services.threadpool; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import org.sape.carbon.core.exception.ExceptionUtility; 24 25 35 public class TaskInfoImpl implements TaskInfo { 36 private Log log = LogFactory.getLog(this.getClass()); 37 38 private Runnable task; 39 private TaskStatusEnum status = TaskStatusEnum.PENDING; 40 private Throwable taskFailureCause = null; 41 private String taskName; 42 private String threadPoolName; 43 private TaskCallback callback; 44 45 protected TaskInfoImpl( 46 Runnable task, 47 String taskName, 48 TaskCallback callback, 49 String threadPoolName) { 50 51 this.task = task; 52 this.taskName = taskName; 53 this.callback = callback; 54 this.threadPoolName = threadPoolName; 55 56 if (log.isTraceEnabled()) { 57 log.trace( 58 "Queuing task, thread pool [" + 59 this.threadPoolName + 60 "], task [" + 61 this.taskName + 62 "]"); 63 } 64 } 65 66 public synchronized Throwable getFailureCause() { 67 return this.taskFailureCause; 68 } 69 70 public synchronized TaskStatusEnum getStatus() { 71 return this.status; 72 } 73 74 public String getTaskName() { 75 return this.taskName; 76 } 77 78 public synchronized void waitUntilExecuted() 79 throws InterruptedException { 80 81 waitUntilExecuted(0); 82 } 83 84 public synchronized void waitUntilExecuted(long timeout) 85 throws InterruptedException { 86 87 if (this.status == TaskStatusEnum.PENDING || 88 this.status == TaskStatusEnum.EXECUTING) { 89 90 this.wait(timeout); 91 } 92 } 93 94 101 public void setFailure(Throwable cause) { 102 if (log.isInfoEnabled()) { 103 log.info( 104 "Task failed, thread pool [" + 105 this.threadPoolName + 106 "], task [" + 107 this.taskName + 108 "], failure cause [" + 109 ExceptionUtility.printStackTracesToString(cause) + 110 "]"); 111 } 112 113 synchronized (this) { 114 this.status = TaskStatusEnum.FAILED; 115 this.taskFailureCause = cause; 116 this.notifyAll(); 117 } 118 119 callCallback(false); 120 } 121 122 127 public void setSuccess() { 128 if (log.isTraceEnabled()) { 129 log.trace( 130 "Task executed successfully, thread pool [" + 131 this.threadPoolName + 132 "], task [" + 133 this.taskName + 134 "]"); 135 } 136 137 synchronized (this) { 138 this.status = TaskStatusEnum.SUCCESS; 139 this.notifyAll(); 140 } 141 142 callCallback(true); 143 } 144 145 149 public synchronized void setExecuting() { 150 if (log.isTraceEnabled()) { 151 log.trace( 152 "Executing task, thread pool [" + 153 this.threadPoolName + 154 "], task [" + 155 this.taskName + 156 "]"); 157 } 158 159 this.status = TaskStatusEnum.EXECUTING; 160 } 161 162 163 public Runnable getTask() { 164 return this.task; 165 } 166 167 public synchronized String toString() { 168 StringBuffer buf = new StringBuffer (); 169 170 buf.append("Task ["); 171 buf.append(this.taskName == null ? "Unnamed Task" : this.taskName); 172 buf.append("], current state ["); 173 buf.append(this.status); 174 buf.append("]"); 175 176 if (this.taskFailureCause != null) { 177 buf.append(", failure cause: "); 178 buf.append(ExceptionUtility.printStackTracesToString( 179 this.taskFailureCause)); 180 } 181 return buf.toString(); 182 } 183 184 185 private void callCallback(boolean success) { 186 if (this.callback != null) { 187 188 if (log.isTraceEnabled()) { 189 log.trace( 190 "Notifying callback, thread pool [" + 191 this.threadPoolName + 192 "], task [" + 193 this.taskName + 194 "]"); 195 } 196 197 try { 198 if (success) { 199 this.callback.taskSucceeded(this); 200 } else { 201 this.callback.taskFailed(this); 202 } 203 } catch (RuntimeException re) { 204 if (log.isErrorEnabled()) { 205 log.error( 206 "Caught RuntimeException in callback, thread pool [" + 207 this.threadPoolName + 208 "], task [" + 209 this.taskName + 210 "]", 211 re); 212 } 213 } 214 } 215 } 216 217 } 218 | Popular Tags |