1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.logicalcobwebs.proxool.util.FastArrayList; 11 12 import java.util.HashMap ; 13 import java.util.List ; 14 import java.util.Map ; 15 import java.util.Iterator ; 16 17 24 public class HouseKeeperController { 25 26 private static final Log LOG = LogFactory.getLog(HouseKeeperController.class); 27 28 private static Map houseKeepers = new HashMap (); 29 30 private static List houseKeeperList = new FastArrayList(); 31 32 private static int houseKeeperIndex = 0; 33 34 private static List houseKeeperThreads = new FastArrayList(); 35 36 private static final Object LOCK = new Integer (1); 37 38 private static HouseKeeper getHouseKeeper(String alias) throws ProxoolException { 39 final HouseKeeper houseKeeper = (HouseKeeper) houseKeepers.get(alias); 40 if (houseKeeper == null) { 41 throw new ProxoolException("Tried to use an unregistered house keeper '" + alias + "'"); 42 } 43 return houseKeeper; 44 } 45 46 50 protected static HouseKeeper getHouseKeeperToRun() { 51 HouseKeeper houseKeeper = null; 52 synchronized (LOCK) { 53 for (int i = 0; i < houseKeeperList.size(); i++) { 54 HouseKeeper hk = null; 55 try { 56 hk = (HouseKeeper) houseKeeperList.get(houseKeeperIndex); 57 if (hk.isSweepDue()) { 58 houseKeeper = hk; 59 break; 60 } 61 houseKeeperIndex++; 62 } catch (IndexOutOfBoundsException e) { 63 houseKeeperIndex = 0; 64 } 65 } 66 } 67 return houseKeeper; 68 } 69 70 protected static void sweepNow(String alias) { 71 try { 72 getHouseKeeper(alias).sweep(); 73 } catch (ProxoolException e) { 74 LOG.error("Couldn't run house keeper for " + alias, e); 75 } 76 } 77 78 82 protected static void register(ConnectionPool connectionPool) { 83 String alias = connectionPool.getDefinition().getAlias(); 84 LOG.debug("Registering '" + alias + "' house keeper"); 85 HouseKeeper houseKeeper = new HouseKeeper(connectionPool); 86 synchronized (LOCK) { 87 houseKeepers.put(alias, houseKeeper); 88 houseKeeperList.add(houseKeeper); 89 90 if (houseKeeperThreads.size() == 0) { 91 HouseKeeperThread hkt = new HouseKeeperThread("HouseKeeper"); 92 LOG.debug("Starting a house keeper thread"); 93 hkt.start(); 94 houseKeeperThreads.add(hkt); 95 } 96 } 97 } 98 99 102 protected static void shutdown() { 103 synchronized(LOCK) { 104 Iterator i = houseKeeperThreads.iterator(); 105 while (i.hasNext()) { 106 HouseKeeperThread hkt = (HouseKeeperThread) i.next(); 107 LOG.info("Stopping " + hkt.getName() + " thread"); 108 hkt.cancel(); 109 } 110 houseKeeperThreads.clear(); 111 } 112 } 113 114 121 protected static void cancel(String alias) throws ProxoolException { 122 HouseKeeper hk = getHouseKeeper(alias); 123 houseKeepers.remove(alias); 124 houseKeeperList.remove(hk); 125 } 126 127 } 128 129 130 | Popular Tags |