1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.util.Iterator ; 12 13 20 public class PrototyperController { 21 22 private static final Log LOG = LogFactory.getLog(PrototyperController.class); 23 24 private static PrototyperThread prototyperThread; 25 26 private static boolean keepSweeping; 27 28 private static final String LOCK = "LOCK"; 29 30 private static void startPrototyper() { 31 if (prototyperThread == null) { 32 synchronized(LOCK) { 33 if (prototyperThread == null) { 34 prototyperThread = new PrototyperThread("Prototyper"); 35 prototyperThread.start(); 36 } 37 } 38 } 39 } 40 46 protected static void triggerSweep(String alias) { 47 try { 48 ConnectionPool cp = ConnectionPoolManager.getInstance().getConnectionPool(alias); 50 try { 51 cp.acquirePrimaryReadLock(); 52 cp.getPrototyper().triggerSweep(); 53 } catch (InterruptedException e) { 54 LOG.error("Couldn't acquire primary read lock", e); 55 } finally { 56 cp.releasePrimaryReadLock(); 57 } 58 } catch (ProxoolException e) { 59 if (LOG.isDebugEnabled()) { 60 LOG.debug("Couldn't trigger prototyper triggerSweep for '" + alias + "' - maybe it's just been shutdown"); 61 } 62 } 63 startPrototyper(); 64 try { 65 keepSweeping = true; 68 if (prototyperThread != null) { 70 prototyperThread.doNotify(); 71 } 72 } catch (IllegalMonitorStateException e) { 73 LOG.debug("Hmm", e); 74 if (Thread.activeCount() > 10 && LOG.isInfoEnabled()) { 75 LOG.info("Suspicious thread count of " + Thread.activeCount()); 76 } 77 } catch (IllegalThreadStateException e) { 78 if (LOG.isDebugEnabled()) { 81 LOG.debug("Ignoring attempt to prototype whilst already prototyping"); 82 } 83 } 84 } 85 86 public static boolean isKeepSweeping() { 87 return keepSweeping; 88 } 89 90 public static void sweepStarted() { 91 keepSweeping = false; 92 } 93 94 97 protected static void shutdown() { 98 synchronized(LOCK) { 99 if (prototyperThread != null) { 100 LOG.info("Stopping " + prototyperThread.getName() + " thread"); 101 prototyperThread.cancel(); 102 prototyperThread = null; 103 } 104 } 105 } 106 } 107 108 109 | Popular Tags |