KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > HouseKeeperController


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

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 JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * Schedules when to run the house keeper
19  * @version $Revision: 1.6 $, $Date: 2006/01/18 14:40:01 $
20  * @author bill
21  * @author $Author: billhorsman $ (current maintainer)
22  * @since Proxool 0.8
23  */

24 public class HouseKeeperController {
25
26     private static final Log LOG = LogFactory.getLog(HouseKeeperController.class);
27
28     private static Map JavaDoc houseKeepers = new HashMap JavaDoc();
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 JavaDoc LOCK = new Integer JavaDoc(1);
37
38     private static HouseKeeper getHouseKeeper(String JavaDoc 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     /**
47      * Get the next house keeper that needs to be run
48      * @return the house keeper to run, or null if there is nothing to do.
49      */

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 JavaDoc e) {
63                     houseKeeperIndex = 0;
64                 }
65             }
66         }
67         return houseKeeper;
68     }
69
70     protected static void sweepNow(String JavaDoc 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     /**
79      * Schedule a regular triggerSweep
80      * @param connectionPool identifies the pool
81      */

82     protected static void register(ConnectionPool connectionPool) {
83         String JavaDoc 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     /**
100      * Stop all house keeper threads.
101      */

102     protected static void shutdown() {
103         synchronized(LOCK) {
104             Iterator JavaDoc 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     /**
115      * cancel a house keeper for a pool. This doens't stop the house keeper
116      * thread.
117      * @param alias identifies the pool
118      * @throws ProxoolException if we couldn't find the house keeper (if it had
119      * already been cancelled for instance).
120      */

121     protected static void cancel(String JavaDoc alias) throws ProxoolException {
122         HouseKeeper hk = getHouseKeeper(alias);
123         houseKeepers.remove(alias);
124         houseKeeperList.remove(hk);
125     }
126
127 }
128
129
130 /*
131  Revision history:
132  $Log: HouseKeeperController.java,v $
133  Revision 1.6 2006/01/18 14:40:01 billhorsman
134  Unbundled Jakarta's Commons Logging.
135
136  Revision 1.5 2004/06/02 20:38:32 billhorsman
137  More robust round robin of house keepers.
138
139  Revision 1.4 2004/03/26 15:58:56 billhorsman
140  Fixes to ensure that house keeper and prototyper threads finish after shutdown.
141
142  Revision 1.3 2003/03/10 23:43:10 billhorsman
143  reapplied checkstyle that i'd inadvertently let
144  IntelliJ change...
145
146  Revision 1.2 2003/03/10 15:26:46 billhorsman
147  refactoringn of concurrency stuff (and some import
148  optimisation)
149
150  Revision 1.1 2003/03/05 18:42:33 billhorsman
151  big refactor of prototyping and house keeping to
152  drastically reduce the number of threads when using
153  many pools
154
155  */
Popular Tags