1 22 23 package org.jboss.aspects.asynchronous.concurrent; 24 25 import EDU.oswego.cs.dl.util.concurrent.Callable; 26 import EDU.oswego.cs.dl.util.concurrent.FutureResult; 27 import EDU.oswego.cs.dl.util.concurrent.TimeoutException; 28 import org.jboss.aspects.asynchronous.AsynchronousConstants; 29 import org.jboss.aspects.asynchronous.AsynchronousParameters; 30 import org.jboss.aspects.asynchronous.AsynchronousUserTask; 31 import org.jboss.aspects.asynchronous.ProcessingTime; 32 import org.jboss.aspects.asynchronous.ThreadManagerResponse; 33 import org.jboss.aspects.asynchronous.common.ThreadManagerResponseImpl; 34 35 import java.lang.reflect.InvocationTargetException ; 36 37 41 42 public final class AsynchronousTaskImpl 43 44 implements AsynchronousConstants, org.jboss.aspects.asynchronous.concurrent.AsynchronousTask 45 { 46 47 private long _timeout = 0; 48 49 private AsynchronousParameters _inputParametersImpl = null; 50 51 private AsynchronousUserTask _oneInstance = null; 52 53 private FutureResult _futureResult = null; 54 55 private Callable _callable = null; 56 57 private String _id = null; 58 59 AsynchronousTaskImpl(String id, 60 61 String taskImpl, 62 63 AsynchronousParameters inputParametersImpl, 64 65 long timeout) 66 { 67 68 this._id = id; 69 70 _futureResult = new FutureResult(); 71 72 _timeout = timeout; 73 74 _inputParametersImpl = inputParametersImpl; 75 76 try 77 { 78 79 Class aClass = Class.forName(taskImpl); 80 81 _oneInstance = (AsynchronousUserTask) aClass.newInstance(); 82 83 } 84 catch (Exception e) 85 { 86 e.printStackTrace(); 87 88 } 89 90 } 91 92 AsynchronousTaskImpl(String id, 93 94 AsynchronousUserTask userTask, 95 96 AsynchronousParameters inputParametersImpl, 97 98 long timeout) 99 { 100 101 this._id = id; 102 103 _futureResult = new FutureResult(); 104 105 _timeout = timeout; 106 107 _oneInstance = userTask; 108 109 _inputParametersImpl = inputParametersImpl; 110 111 } 112 113 public AsynchronousParameters getInputParameters() 114 { 115 116 return _inputParametersImpl; 117 118 } 119 120 public long getTimeout() 121 { 122 123 return _timeout; 124 125 } 126 127 public AsynchronousUserTask getTask() 128 { 129 130 return _oneInstance; 131 132 } 133 134 public ThreadManagerResponse getResponse() 135 { 136 137 try 138 { 139 140 if (isDone()) 141 142 return (ThreadManagerResponse) _futureResult.get(); 143 144 else 145 { 146 147 Object value = _futureResult.get(); 148 149 if (value != null) 150 151 return (ThreadManagerResponse) value; 152 153 else 154 155 return new ThreadManagerResponseImpl(getId(), 156 157 NOVALUE, 158 159 null, 160 161 null); 162 163 } 164 165 } 166 catch (TimeoutException e) 167 { 168 169 return new ThreadManagerResponseImpl(getId(), 170 171 TIMEOUT, 172 173 e.getMessage(), 174 175 e, 176 177 getStartingTime(), 178 179 getEndingTime()); 180 181 } 182 catch (InterruptedException e) 183 { 184 185 return new ThreadManagerResponseImpl(getId(), 186 187 INTERRUPTED, 188 189 e.getMessage(), 190 191 e, 192 193 getStartingTime(), 194 195 getEndingTime()); 196 197 } 198 catch (InvocationTargetException e) 199 { 200 201 int errorCode = INVOCATION; 202 203 if (e.getTargetException() 204 205 instanceof EDU.oswego.cs.dl.util.concurrent.TimeoutException) 206 207 errorCode = TIMEOUT; 208 209 return new ThreadManagerResponseImpl(getId(), 210 211 errorCode, 212 213 e.getTargetException().getMessage(), 214 215 e.getTargetException(), 216 217 getStartingTime(), 218 219 getEndingTime()); 220 221 } 222 223 } 224 225 public long getStartingTime() 226 { 227 228 return ((ProcessingTime) _callable).getStartingTime(); 229 230 } 231 232 public long getEndingTime() 233 { 234 235 return ((ProcessingTime) _callable).getEndingTime(); 236 237 } 238 239 240 public boolean isDone() 241 { 242 243 return _futureResult.isReady(); 244 245 } 246 247 public Runnable add() throws Exception 248 { 249 250 try 251 { 252 253 if (_timeout == 0) 254 255 _callable = 256 257 new AdapterTask(getId(), 258 259 _inputParametersImpl, 260 261 _oneInstance); 262 263 else 264 265 _callable = 266 267 new TimedCallableImpl(new AdapterTask(getId(), 268 269 _inputParametersImpl, 270 271 _oneInstance), 272 273 _timeout); 274 275 return _futureResult.setter(_callable); 276 277 278 } 279 catch (Exception e) 280 { 281 282 283 throw e; 284 285 } 286 287 } 288 289 public String getId() 290 { 291 292 return _id; 293 294 } 295 296 } 297 298 | Popular Tags |