1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.tasks; 26 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 31 import org.objectweb.cjdbc.common.exceptions.SQLExceptionFactory; 32 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 33 34 42 public abstract class AbstractTask 43 { 44 52 53 private int totalNb; 54 55 56 private int nbToComplete; 57 58 59 private int executionStarted; 60 61 private int success = 0; 62 63 private int failed = 0; 64 65 private ArrayList exceptions = null; 66 67 private boolean hasTid = false; 69 70 private ResultSet generatedKeysResultSet; 72 73 private boolean timeoutExpired = false; 75 76 79 80 87 public AbstractTask(int nbToComplete, int totalNb) 88 { 89 this.nbToComplete = nbToComplete; 90 this.totalNb = totalNb; 91 success = 0; 92 failed = 0; 93 executionStarted = 0; 94 } 95 96 99 100 106 public void execute(BackendWorkerThread backendThread) throws SQLException 107 { 108 synchronized (this) 109 { 110 if (timeoutExpired && (executionStarted == 0)) 115 return; 116 this.executionStarted++; 117 } 118 executeTask(backendThread); 119 } 122 123 129 public abstract void executeTask(BackendWorkerThread backendThread) 130 throws SQLException ; 131 132 135 public synchronized void notifySuccess() 136 137 { 138 success++; 139 140 if ((success == nbToComplete) || (success + failed == totalNb)) 142 { 143 if (failed > 0) 144 notifyAll(); else 146 notify(); 147 } 148 } 149 150 159 public synchronized void notifyCompletion() 160 { 161 totalNb--; 162 if (success + failed == totalNb) 164 { 165 notifyAll(); } 167 } 168 169 185 public synchronized boolean notifyFailure(BackendWorkerThread backendThread, 186 long timeout, Exception e) throws SQLException 187 { 188 failed++; 189 190 if (exceptions == null) 192 exceptions = new ArrayList (); 193 if (e instanceof SQLException ) 194 { 195 SQLException sqlEx = (SQLException ) e; 196 exceptions.add(SQLExceptionFactory.getSQLException(sqlEx, 197 "BackendThread " + backendThread.getBackend().getName() + " failed (" 198 + sqlEx.getLocalizedMessage() + ")")); 199 } 200 else 201 exceptions.add(new SQLException ("BackendThread " 202 + backendThread.getBackend().getName() + " failed (" 203 + e.getLocalizedMessage() + ")").initCause(e)); 204 205 if (success + failed == totalNb) 207 { 208 notifyAll(); } 210 else 211 { 212 try 213 { wait(timeout); 215 } 216 catch (InterruptedException ie) 217 { 218 throw (SQLException ) new SQLException ( 219 "Wait interrupted() in failed task of backend " 220 + backendThread.getBackend().getName() + " (" 221 + e.getLocalizedMessage() + ")").initCause(e); 222 } 223 } 224 return success > 0; 225 } 226 227 231 236 public ArrayList getExceptions() 237 { 238 return exceptions; 239 } 240 241 246 public synchronized int getExecutionStarted() 247 { 248 return executionStarted; 249 } 250 251 256 public int getFailed() 257 { 258 return failed; 259 } 260 261 266 public int getNbToComplete() 267 { 268 return nbToComplete; 269 } 270 271 276 public int getSuccess() 277 { 278 return success; 279 } 280 281 287 public int getTotalNb() 288 { 289 return totalNb; 290 } 291 292 298 public void setTotalNb(int totalNb) 299 { 300 this.totalNb = totalNb; 301 } 302 303 311 public synchronized boolean hasCompleted() 312 { 313 return ((success >= nbToComplete) || (success + failed == totalNb)); 314 } 315 316 322 public synchronized boolean hasFullyCompleted() 323 { 324 return success + failed == totalNb; 325 } 326 327 334 public boolean hasTid() 335 { 336 return hasTid; 337 } 338 339 346 public void setHasTid(boolean hasTid) 347 { 348 this.hasTid = hasTid; 349 } 350 351 356 public ResultSet getGeneratedKeysResultSet() 357 { 358 return generatedKeysResultSet; 359 } 360 361 366 public void setGeneratedKeysResultSet(ResultSet generatedKeysResultSet) 367 { 368 this.generatedKeysResultSet = generatedKeysResultSet; 369 } 370 371 380 public synchronized boolean setExpiredTimeout() 381 { 382 this.timeoutExpired = true; 383 return executionStarted == 0; 384 } 385 386 } 387 | Popular Tags |