1 21 22 package uk.org.primrose.pool.jmx; 23 24 import uk.org.primrose.pool.jmx.*; 25 import uk.org.primrose.pool.core.*; 26 import java.util.Vector ; 27 import java.util.StringTokenizer ; 28 import java.util.Properties ; 29 import java.util.ArrayList ; 30 import java.io.PrintWriter ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.Serializable ; 34 import java.sql.Connection ; 35 36 41 public class Queue implements QueueMBean, Serializable { 42 private Pool pool; 43 private int waitingThreads; 44 private String killActiveConnectionsOverAge = "-1"; 46 private boolean queueConnectionRequests = true; 47 java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ("yyyy-MM-dd"); 49 java.util.Date logDate = new java.util.Date (); 50 String logDateString = ""; 51 String logNameOrig = null; 52 String logName = null; 53 int inc = 0; 54 int dec = 0; 55 int threadCount = 0; 56 private boolean runPooledMode; 57 private ArrayList objectMonitors = null; 58 59 public Queue() { 60 logDateString = sdf.format(logDate); 61 objectMonitors = new ArrayList (); 62 } 63 64 65 73 private String killActiveConnectionsOverAge(int age, boolean daemon) { 74 75 String ret = ""; 76 77 if (daemon) { 78 ret += "Killing connections by daemon :\n"; 79 } else { 80 ret += "Killing connections by invocation from web management page :<br>"; 81 } 82 83 Vector actCon = pool.getActiveConnections(); 84 int actConSize = actCon.size(); 85 boolean found = false; 86 87 for (int i = 0; i < actConSize; i++) { 88 try { 89 PoolConnection pc = (PoolConnection)actCon.elementAt(0); 90 long timeInUse = pc.getTimeInUse()/1000; 92 if ((int)timeInUse > age) { 93 found = true; 94 ret += ("Killed connection " +pc +" : timeInUse(" +timeInUse +"), SQL(" + pc.getSql() +")<br>\n"); 95 if (daemon) pool.log("About to kill connection " +pc +" : timeInUse(" +timeInUse +"), SQL(" + pc.getSql() +")", true); 96 97 pool.killActiveConnection(pc); 98 99 if (daemon) pool.log("Killed connection(complete) " +pc +" : timeInUse(" +timeInUse +"), SQL(" + pc.getSql() +")", true); 101 } 102 103 } catch (Exception e) {} 108 109 } 110 111 if (!found) ret += "No connections found over age " +age +" seconds."; 112 113 114 return ret; 115 116 } 117 118 119 120 121 127 public String killActiveConnectionsOverAge(int age) { 128 return killActiveConnectionsOverAge(age, false); 129 } 130 131 132 133 public void reset() { 135 this.stopPool(); 136 this.startPool(); 137 } 138 139 public void addConnection(int num) { 140 pool.setBase(pool.getBase() +num); 141 pool.fill(num); 142 } 143 144 public void startPool() { 145 if (!getAvailable()) { 146 pool.fill(getBase()); 147 if (getAvailableConnectionsSize() == getBase()) { 148 setAvailable(true); 149 } 150 } 151 startObjectMonitors(); 152 } 153 154 160 public void stopPool() { 161 pool.log("[Queue-" +this.getName() +"] SHUTTING DOWN POOL !", true); 162 stopObjectMonitors(); 163 int availSize = getAvailableConnectionsSize(); 164 165 if (availSize < getBase()) { 166 pool.getLog().println("WARNING !!! Pool elements are ACTIVE !!! " +availSize +" " +getBase()); 167 } 168 for (int i = 0; i < availSize; i++) { 169 pool.log("[Queue-" +this.getName() +"] STOPPING POOL element : " +i +" (NON-ACTIVE)", false); 170 PoolConnection pc = (PoolConnection)pool.get(); 171 pool.dump(pc); 172 pool.removeFromActiveConnections(pc); 173 } 174 175 int actSize = getActiveConnectionsSize(); 176 Vector activeConnections = pool.getActiveConnections(); 177 for (int i = 0; i < actSize; i++) { 178 PoolConnection pc = (PoolConnection)activeConnections.get(0); 179 pool.log("[Queue-" +this.getName() +"] STOPPING POOL element : " +i +" (ACTIVE)", false); 180 pool.dump(pc); 181 pool.removeFromActiveConnections(pc); 182 183 } 184 pool.log("[Queue-" +this.getName() +"] POOL STOPPED.", true); 185 186 setAvailable(false); 187 pool.setOverflow(pool.getOverflow()); 190 191 } 192 193 194 195 196 201 public int checkActiveConnectionTimes() { 202 String age = this.getKillActiveConnectionsOverAge(); 203 if (age != null && age.length() != 0 && !(age.equals("-1"))) { 204 int a = (int)Long.parseLong(age) / 1000; 205 killActiveConnectionsOverAge(a, true); 206 } 207 208 return 0; 209 } 210 211 216 public synchronized int checkIdleConnections() { 217 return pool.checkIdleConnections(); 218 } 219 220 223 public void startNewPool(Properties props) { 224 pool = new Pool(); 225 Object tmpProp = null; 226 227 tmpProp = props.getProperty("poolName"); 229 String poolName = (tmpProp == null ? "noPoolNameSet" : (String )tmpProp); 230 231 tmpProp = props.getProperty("base"); 233 int base = (tmpProp == null ? 5 : Integer.parseInt((String )tmpProp)); 234 235 tmpProp = props.getProperty("overflow"); 237 int overflow = (tmpProp == null ? 2 : Integer.parseInt((String )tmpProp)); 238 239 tmpProp = props.getProperty("log"); 241 String log = (tmpProp == null ? null : (String )tmpProp); 242 243 tmpProp = props.getProperty("idleTime"); 245 int idleTime = (tmpProp == null ? 120000 : Integer.parseInt((String )tmpProp)); 246 247 tmpProp = props.getProperty("messageLogging"); 249 boolean messageLogging = (tmpProp == null ? true : new Boolean ((String )tmpProp).booleanValue()); 250 251 tmpProp = props.getProperty("sizeLogging"); 253 boolean sizeLogging = (tmpProp == null ? true : new Boolean ((String )tmpProp).booleanValue()); 254 255 tmpProp = props.getProperty("driverClass"); 257 String driverClass = (tmpProp == null ? null : (String )tmpProp); 258 259 tmpProp = props.getProperty("driverURL"); 261 String driverURL = (tmpProp == null ? null : (String )tmpProp); 262 263 tmpProp = props.getProperty("user"); 265 String user = (tmpProp == null ? null : (String )tmpProp); 266 267 tmpProp = props.getProperty("password"); 269 String password = (tmpProp == null ? null : (String )tmpProp); 270 271 tmpProp = props.getProperty("killActiveConnectionsOverAge"); 273 String killActiveConnectionsOverAge = (tmpProp == null ? "-1" : (String )tmpProp); 274 275 tmpProp = props.getProperty("cycleConnections"); 277 int cycleConnections = (tmpProp == null ? -1 : Integer.parseInt((String )tmpProp)); 278 279 tmpProp = props.getProperty("queueConnectionRequests"); 281 boolean queueConnectionRequests = (tmpProp == null ? true : new Boolean ((String )tmpProp).booleanValue()); 282 283 tmpProp = props.getProperty("runPooledMode"); 285 boolean runPooledMode = (tmpProp == null ? true : new Boolean ((String )tmpProp).booleanValue()); 286 287 tmpProp = props.getProperty("connectionAutoCommit"); 289 boolean connectionAutoCommit = (tmpProp == null ? true : new Boolean ((String )tmpProp).booleanValue()); 290 291 tmpProp = props.getProperty("connectionTransactionIsolation"); 293 String connectionTransactionIsolation = (tmpProp == null ? null : (String )tmpProp); 294 295 tmpProp = props.getProperty("checkSQL"); 296 String checkSQL = (tmpProp == null ? null : (String )tmpProp); 297 298 299 pool.setName(poolName); 301 pool.setBase(base); 302 pool.setOverflow(overflow); 303 this.logNameOrig = log; 304 this.setUpLog(); 305 pool.setDriverClass(driverClass); 306 pool.setDriverURL(driverURL); 307 pool.setUser(user); 308 pool.setPassword(password); 309 this.setIdleTime(idleTime); 310 pool.setMessageLogging(messageLogging); 311 pool.setSizeLogging(sizeLogging); 312 this.setKillActiveConnectionsOverAge(killActiveConnectionsOverAge); 313 this.setQueueConnectionRequests(queueConnectionRequests); 314 pool.setCycleConnections(cycleConnections); 315 this.setRunPooledMode(runPooledMode); 316 this.setConnectionAutoCommit(connectionAutoCommit); 317 this.setConnectionTransactionIsolation(connectionTransactionIsolation); 318 pool.setCheckSQL(checkSQL); 319 320 pool.fill(base); 321 322 setAvailable(true); 330 } 331 332 333 public void startNewPool (String poolName, int base, int overflow, String log, 334 int idleTime, boolean messageLogging, boolean sizeLogging, 335 String driverClass, String driverURL, String user, String password, 336 String killActiveConnectionsOverAge, String cycleConnections, boolean queueConnectionRequests, 337 boolean runPooledMode, boolean connectionAutoCommit, String connectionTransactionIsolation, String checkSQL) { 338 pool = new Pool(); 339 setPoolParameters(poolName, base, overflow, log, idleTime, messageLogging, sizeLogging, 340 driverClass, driverURL, user, password, killActiveConnectionsOverAge, 341 cycleConnections, queueConnectionRequests, runPooledMode, connectionAutoCommit, connectionTransactionIsolation, checkSQL); 342 } 343 344 347 public void restartPool(String poolName, int base, int overflow, String log, 348 Integer idleTime, Boolean messageLogging, Boolean sizeLogging, 349 String driverClass, String driverURL, String user, String password, 350 String killActiveConnectionsOverAge, String cycleConnections, boolean queueConnectionRequests, 351 boolean runPooledMode, boolean connectionAutoCommit, String connectionTransactionIsolation, String checkSQL) { 352 353 setPoolParameters(poolName, base, overflow, log, idleTime.intValue(), messageLogging.booleanValue(), 354 sizeLogging.booleanValue(), driverClass, driverURL, user, password, 355 killActiveConnectionsOverAge, cycleConnections, queueConnectionRequests, 356 runPooledMode, connectionAutoCommit, connectionTransactionIsolation, checkSQL); 357 358 } 359 360 363 private void setPoolParameters(String poolName, int base, int overflow, String log, 364 int idleTime, boolean messageLogging, boolean sizeLogging, 365 String driverClass, String driverURL, String user, String password, 366 String killActiveConnectionsOverAge, String cycleConnections, boolean queueConnectionRequests, 367 boolean runPooledMode, boolean connectionAutoCommit, String connectionTransactionIsolation, String checkSQL) { 368 pool.setName(poolName); 369 pool.setBase(base); 370 pool.setOverflow(overflow); 371 this.logNameOrig = log; 372 this.setUpLog(); 373 pool.setDriverClass(driverClass); 374 pool.setDriverURL(driverURL); 375 pool.setUser(user); 376 pool.setPassword(password); 377 this.setIdleTime(idleTime); 378 pool.setMessageLogging(messageLogging); 379 pool.setSizeLogging(sizeLogging); 380 this.setKillActiveConnectionsOverAge(killActiveConnectionsOverAge); 381 this.setRunPooledMode(runPooledMode); 382 this.setQueueConnectionRequests(queueConnectionRequests); 383 if (cycleConnections == null || cycleConnections.length() == 0) { 384 pool.setCycleConnections(-1); 385 } else { 386 pool.setCycleConnections(Integer.parseInt(cycleConnections)); 387 } 388 pool.setCheckSQL(checkSQL); 389 390 pool.fill(base); 391 392 if (getAvailableConnectionsSize() == base) { 393 setAvailable(true); 394 } 395 } 396 397 private void parseLogName() { 398 if (logNameOrig == null || logNameOrig.indexOf("${") == -1) return; 399 400 String dateFormat = logNameOrig.substring(logNameOrig.indexOf("${") +2, logNameOrig.indexOf("}")); 401 String logPrefix = logNameOrig.substring(0, logNameOrig.indexOf("${")); 402 String logSuffix = logNameOrig.substring(logNameOrig.indexOf("}")+1, logNameOrig.length()); 403 java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat (dateFormat); 404 java.util.Date tmp = new java.util.Date (); 405 String formattedDate = sdf2.format(tmp); 406 407 this.logName = (logPrefix +formattedDate +logSuffix); 408 } 409 410 public String setUpLog() { 411 if (logNameOrig == null) return "0"; 413 414 if (logNameOrig.indexOf("${") == -1) { 417 try { 418 pool.setLog(new PrintWriter (new FileOutputStream (logNameOrig, true), true)); 419 } catch (IOException ioe) { 420 ioe.printStackTrace(System.err); 421 } 422 423 return "0"; 424 } 425 426 427 String tmpDateString = sdf.format(new java.util.Date ()); 432 PrintWriter log = pool.getLog(); 433 434 if (log != null) { 435 if (!tmpDateString.equals(logDateString)) { 436 this.logDateString = tmpDateString; 437 pool.log("Cycling log at : " +(new java.util.Date ()), false); 438 log.flush(); 439 log.close(); 440 parseLogName(); 441 442 try { 443 pool.setLog(new PrintWriter (new FileOutputStream (logName, true), true)); 444 } catch (IOException ioe) { 445 ioe.printStackTrace(System.err); 446 } 447 } 448 } else { 450 parseLogName(); 451 try { 452 pool.setLog(new PrintWriter (new FileOutputStream (logName, true), true)); 453 } catch (IOException ioe) { 454 ioe.printStackTrace(System.err); 455 } 456 } 457 return "0"; 458 } 459 460 474 public Connection getConnection() { 475 Connection pc = pool.get(); 476 477 if (pc != null) { 483 if (waitingThreads == 0) pool.setWaitingThreads(false); 484 return pc; 485 } else { 486 if (queueConnectionRequests == false || !pool.isAvailable()) { 487 return null; 488 } 489 waitingThreads++; 491 try { Thread.sleep(1000); } catch (InterruptedException ie) {} 492 return getConnectionWait(); 493 } 494 } 495 496 private Connection getConnectionWait() { 497 if (waitingThreads > 0) 500 pool.setWaitingThreads(true); 501 else 502 pool.setWaitingThreads(false); 503 504 Connection pc = pool.get(); 505 507 if (pc != null) { 515 waitingThreads--; 517 518 return pc; 519 } else { 520 try { Thread.sleep(1000); } catch (InterruptedException ie) {} 521 return getConnectionWait(); 522 } 523 } 524 525 526 527 530 public String getConnectionTransactionIsolation() { 531 return pool.getConnectionTransactionIsolation(); 532 } 533 534 535 538 public boolean getConnectionAutoCommit() { 539 return pool.getConnectionAutoCommit(); 540 } 541 542 543 public boolean getRunPooledMode() { 544 return pool.getRunPooledMode(); 545 } 546 547 552 public int getCycleConnections() { 553 return pool.getCycleConnections(); 554 } 555 556 559 public int getIdleTime() { 560 return pool.getIdleTime(); 561 } 562 563 567 public boolean getMessageLogging() { 568 return pool.getMessageLogging(); 569 } 570 571 575 public boolean getSizeLogging() { 576 return pool.getSizeLogging(); 577 } 578 579 583 public synchronized int getWaitingThreads() { 584 return waitingThreads; 585 } 586 587 private synchronized void setWaitingThreads(int waitingThreads) { 588 this.waitingThreads = waitingThreads; 592 } 594 595 596 600 public int getActiveConnectionsSize() { 601 return pool.getActiveConnections().size(); 602 } 603 604 608 public int getAvailableConnectionsSize() { 609 return pool.getAvailableConnections().length; 610 } 611 612 616 public boolean getAvailable() { 617 return pool.isAvailable(); 618 } 619 620 624 public String [] getActiveConnectionData() { 625 return pool.getActiveConnectionsData(); 626 } 627 628 public String [] getAvailableConnections() { 629 return pool.getAvailableConnections(); 630 } 631 632 636 public int getBase() { 637 return pool.getBase(); 638 } 639 640 641 645 public String getName() { 646 return pool.getName(); 647 } 648 649 653 public int getOverflow() { 654 return pool.getOverflow(); 655 } 656 657 661 public String getDriverURL() { 662 return pool.getDriverURL(); 663 } 664 665 669 public String getUser() { 670 return pool.getUser(); 671 } 672 673 677 public String getPassword() { 678 return pool.getPassword(); 679 } 680 681 685 public String getDriverClass() { 686 return pool.getDriverClass(); 687 } 688 689 692 public String getKillActiveConnectionsOverAge() { 693 return killActiveConnectionsOverAge; 694 } 695 696 699 public boolean getQueueConnectionRequests() { 700 return this.queueConnectionRequests; 701 } 702 703 704 705 708 public void setConnectionAutoCommit(boolean connectionAutoCommit) { 709 pool.log("INFO : Connections default to auto commit : '" +connectionAutoCommit +"'", false); 710 pool.setConnectionAutoCommit(connectionAutoCommit); 711 } 712 713 716 private void setConnectionTransactionIsolation(String connectionTransactionIsolation) { 717 pool.setConnectionTransactionIsolation(connectionTransactionIsolation); 718 } 719 720 721 public void setRunPooledMode(boolean runPooledMode) { 722 pool.log("INFO : Using pooling : '" +runPooledMode +"'", false); 723 this.runPooledMode = runPooledMode; 724 pool.setRunPooledMode(runPooledMode); 725 } 726 727 730 public void setIdleTime(int idleTime) { 731 pool.setIdleTime(idleTime); 732 } 733 734 737 public void setAvailable(boolean a) { 738 pool.setAvailable(a); 739 } 740 741 744 public void setMessageLogging(boolean messageLogging) { 745 pool.setMessageLogging(messageLogging); 746 } 747 748 751 public void setSizeLogging(boolean sizeLogging) { 752 pool.setSizeLogging(sizeLogging); 753 } 754 755 760 public void setCycleConnections(int cycleConnections) { 761 pool.setCycleConnections(cycleConnections); 762 } 763 764 767 public void setKillActiveConnectionsOverAge(String killActiveConnectionsOverAge) { 768 this.killActiveConnectionsOverAge = killActiveConnectionsOverAge; 769 } 770 771 774 public void setQueueConnectionRequests(boolean queueConnectionRequests) { 775 pool.log("INFO : Queueing Connection requests : '" +queueConnectionRequests +"'", false); 776 this.queueConnectionRequests = queueConnectionRequests; 777 } 778 779 780 public void startObjectMonitors() { 781 for (int i = 0; i < objectMonitors.size(); i++) { 782 ObjectMonitor om = (ObjectMonitor)objectMonitors.get(i); 783 om.start(); 784 } 785 } 786 787 788 public void stopObjectMonitors() { 789 for (int i = 0; i < objectMonitors.size(); i++) { 790 ObjectMonitor om = (ObjectMonitor)objectMonitors.get(i); 791 om.stop(); 792 } 793 } 794 795 public void registerNewObjectMonitor(ObjectMonitor om) { 796 objectMonitors.add(om); 797 } 798 } 799 | Popular Tags |