KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Controls the {@link Prototyper prototypers}
15  * @version $Revision: 1.10 $, $Date: 2006/01/18 14:40:01 $
16  * @author bill
17  * @author $Author: billhorsman $ (current maintainer)
18  * @since Proxool 0.8
19  */

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 JavaDoc 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     /**
41      * Trigger prototyping immediately. Runs inside a new Thread so
42      * control returns as quick as possible. You should call this whenever
43      * you suspect that building more connections might be a good idea.
44      * @param alias
45      */

46     protected static void triggerSweep(String JavaDoc alias) {
47         try {
48             // Ensure that we're not in the process of shutting down the pool
49
ConnectionPool cp = ConnectionPoolManager.getInstance().getConnectionPool(alias);
50             try {
51                 cp.acquirePrimaryReadLock();
52                 cp.getPrototyper().triggerSweep();
53             } catch (InterruptedException JavaDoc 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             // If we are currently sweeping this will cause it to loop through
66
// once more
67
keepSweeping = true;
68             // If we aren't already started then this will start a new sweep
69
if (prototyperThread != null) {
70                 prototyperThread.doNotify();
71             }
72         } catch (IllegalMonitorStateException JavaDoc 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 JavaDoc e) {
78             // Totally expected. Should happen all the time. Just means that
79
// we are already sweeping.
80
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     /**
95      * Stop all house keeper threads
96      */

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 /*
110  Revision history:
111  $Log: PrototyperController.java,v $
112  Revision 1.10 2006/01/18 14:40:01 billhorsman
113  Unbundled Jakarta's Commons Logging.
114
115  Revision 1.9 2004/04/05 22:54:57 billhorsman
116  Check if notify thread has been shutdown before triggering it.
117
118  Revision 1.8 2004/03/26 15:58:56 billhorsman
119  Fixes to ensure that house keeper and prototyper threads finish after shutdown.
120
121  Revision 1.7 2004/03/25 22:02:15 brenuart
122  First step towards pluggable ConnectionBuilderIF & ConnectionValidatorIF.
123  Include some minor refactoring that lead to deprecation of some PrototyperController methods.
124
125  Revision 1.5 2003/03/10 23:43:11 billhorsman
126  reapplied checkstyle that i'd inadvertently let
127  IntelliJ change...
128
129  Revision 1.4 2003/03/10 16:28:02 billhorsman
130  removed debug trace
131
132  Revision 1.3 2003/03/10 15:26:47 billhorsman
133  refactoringn of concurrency stuff (and some import
134  optimisation)
135
136  Revision 1.2 2003/03/06 12:43:32 billhorsman
137  removed paranoid debug
138
139  Revision 1.1 2003/03/05 18:42:33 billhorsman
140  big refactor of prototyping and house keeping to
141  drastically reduce the number of threads when using
142  many pools
143
144  */
Popular Tags