KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > resourcepool > BasicResourcePoolFactory


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.resourcepool;
25
26 import java.util.*;
27 import com.mchange.v2.async.*;
28
29 public class BasicResourcePoolFactory extends ResourcePoolFactory
30 {
31     public static BasicResourcePoolFactory createNoEventSupportInstance( int num_task_threads )
32     { return createNoEventSupportInstance( null, null, num_task_threads ); }
33
34     public static BasicResourcePoolFactory createNoEventSupportInstance( AsynchronousRunner taskRunner,
35                                      Timer timer )
36     { return createNoEventSupportInstance( taskRunner, timer, ResourcePoolFactory.DEFAULT_NUM_TASK_THREADS ); }
37
38
39     private static BasicResourcePoolFactory createNoEventSupportInstance( AsynchronousRunner taskRunner,
40                                       Timer timer,
41                                       int default_num_task_threads )
42     {
43     return new BasicResourcePoolFactory( taskRunner,
44                          timer,
45                          default_num_task_threads,
46                          true );
47     }
48
49     int start = -1; //default to min
50
int min = 1;
51     int max = 12;
52     int inc = 3;
53     int retry_attempts = -1; //by default, retry acquisitions forever
54
int retry_delay = 1000; //1 second
55
long idle_resource_test_period = -1; //milliseconds, by default we don't test idle resources
56
long max_age = -1; //milliseconds, by default resources never expire
57
long max_idle_time = -1; //milliseconds, by default resources never expire
58
long excess_max_idle_time = -1; //milliseconds, by default resources never expire
59
long destroy_overdue_resc_time = -1; //milliseconds
60
long expiration_enforcement_delay = -1; //automatic, we come up with a reasonable default based on time params
61

62     boolean break_on_acquisition_failure = true;
63     boolean debug_store_checkout_stacktrace = false;
64
65     AsynchronousRunner taskRunner;
66     boolean taskRunner_is_external;
67
68     RunnableQueue asyncEventQueue;
69     boolean asyncEventQueue_is_external;
70
71     Timer timer;
72     boolean timer_is_external;
73
74     int default_num_task_threads;
75
76     Set liveChildren;
77
78
79
80     //OLD
81
// Set rqUsers = null;
82
// SimpleRunnableQueue rq = null;
83

84 // Set timerUsers = null;
85
// Timer timer = null;
86
//END OLD
87

88     BasicResourcePoolFactory()
89     { this( null, null, null ); }
90
91     BasicResourcePoolFactory( AsynchronousRunner taskRunner,
92                   RunnableQueue asyncEventQueue,
93                   Timer timer )
94     { this ( taskRunner, asyncEventQueue, timer, DEFAULT_NUM_TASK_THREADS ); }
95
96     BasicResourcePoolFactory( int num_task_threads )
97     { this ( null, null, null, num_task_threads ); }
98
99     BasicResourcePoolFactory( AsynchronousRunner taskRunner,
100                   Timer timer,
101                   int default_num_task_threads,
102                   boolean no_event_support)
103     {
104     this( taskRunner, null, timer, default_num_task_threads );
105     if (no_event_support)
106         asyncEventQueue_is_external = true; //if it's null, and external, it simply won't exist...
107
}
108
109     BasicResourcePoolFactory( AsynchronousRunner taskRunner,
110                   RunnableQueue asyncEventQueue,
111                   Timer timer,
112                   int default_num_task_threads)
113     {
114     this.taskRunner = taskRunner;
115     this.taskRunner_is_external = ( taskRunner != null );
116
117     this.asyncEventQueue = asyncEventQueue;
118     this.asyncEventQueue_is_external = ( asyncEventQueue != null );
119
120     this.timer = timer;
121     this.timer_is_external = ( timer != null );
122
123     this.default_num_task_threads = default_num_task_threads;
124     }
125
126     private void createThreadResources()
127     {
128     if (! taskRunner_is_external )
129         {
130         //taskRunner = new RoundRobinAsynchronousRunner( default_num_task_threads, true );
131
taskRunner = new ThreadPoolAsynchronousRunner( default_num_task_threads, true );
132         if (! asyncEventQueue_is_external)
133             asyncEventQueue = ((Queuable) taskRunner).asRunnableQueue();
134         }
135     if (! asyncEventQueue_is_external)
136         asyncEventQueue = new CarefulRunnableQueue( true, false );
137     if (! timer_is_external )
138         timer = new Timer( true );
139
140     this.liveChildren = new HashSet();
141     }
142
143     private void destroyThreadResources()
144     {
145     if (! taskRunner_is_external )
146         {
147         taskRunner.close();
148         taskRunner = null;
149         }
150     if (! asyncEventQueue_is_external )
151         {
152         asyncEventQueue.close();
153         asyncEventQueue = null;
154         }
155     if (! timer_is_external )
156         {
157         timer.cancel();
158         timer = null;
159         }
160
161     this.liveChildren = null;
162     }
163
164 // synchronized RunnableQueue getSharedRunnableQueue( BasicResourcePool pool )
165
// {
166
// if (rqUsers == null)
167
// {
168
// rqUsers = new HashSet();
169
// rq = new SimpleRunnableQueue(true);
170
// }
171
// rqUsers.add( pool );
172
// return rq;
173
// }
174

175 // synchronized Timer getTimer( BasicResourcePool pool )
176
// {
177
// if (timerUsers == null)
178
// {
179
// timerUsers = new HashSet();
180
// timer = new Timer( true );
181
// }
182
// timerUsers.add( pool );
183
// return timer;
184
// }
185

186     synchronized void markBroken( BasicResourcePool pool )
187     {
188     //System.err.println("markBroken -- liveChildren: " + liveChildren);
189
if (liveChildren != null) //keep this method idempotent!
190
{
191         liveChildren.remove( pool );
192         if (liveChildren.isEmpty())
193             destroyThreadResources();
194         }
195 // rqUsers.remove( pool );
196
// if (rqUsers.size() == 0)
197
// {
198
// rqUsers = null;
199
// rq.close();
200
// rq = null;
201
// }
202

203 // timerUsers.remove( pool );
204
// if (timerUsers.size() == 0)
205
// {
206
// timerUsers = null;
207
// timer.cancel();
208
// timer = null;
209
// }
210
}
211     
212     /**
213      * If start is less than min, it will
214      * be ignored, and the pool will start
215      * with min.
216      */

217     public synchronized void setStart( int start )
218     throws ResourcePoolException
219     { this.start = start; }
220
221     public synchronized int getStart()
222     throws ResourcePoolException
223     { return start; }
224
225     public synchronized void setMin( int min )
226     throws ResourcePoolException
227     { this.min = min; }
228
229     public synchronized int getMin()
230     throws ResourcePoolException
231     { return min; }
232
233     public synchronized void setMax( int max )
234     throws ResourcePoolException
235     { this.max = max; }
236
237     public synchronized int getMax()
238     throws ResourcePoolException
239     { return max; }
240
241     public synchronized void setIncrement( int inc )
242     throws ResourcePoolException
243     { this.inc = inc; }
244
245     public synchronized int getIncrement()
246     throws ResourcePoolException
247     { return inc; }
248
249     public synchronized void setAcquisitionRetryAttempts( int retry_attempts )
250     throws ResourcePoolException
251     { this.retry_attempts = retry_attempts; }
252
253     public synchronized int getAcquisitionRetryAttempts()
254     throws ResourcePoolException
255     { return retry_attempts; }
256
257     public synchronized void setAcquisitionRetryDelay( int retry_delay )
258     throws ResourcePoolException
259     { this.retry_delay = retry_delay; }
260
261     public synchronized int getAcquisitionRetryDelay()
262     throws ResourcePoolException
263     { return retry_delay; }
264
265     public synchronized void setIdleResourceTestPeriod( long test_period )
266     { this.idle_resource_test_period = test_period; }
267
268     public synchronized long getIdleResourceTestPeriod()
269     { return idle_resource_test_period; }
270
271     public synchronized void setResourceMaxAge( long max_age )
272     throws ResourcePoolException
273     { this.max_age = max_age; }
274
275     public synchronized long getResourceMaxAge()
276     throws ResourcePoolException
277     { return max_age; }
278
279     public synchronized void setResourceMaxIdleTime( long millis )
280     throws ResourcePoolException
281     { this.max_idle_time = millis; }
282
283     public synchronized long getResourceMaxIdleTime()
284     throws ResourcePoolException
285     { return max_idle_time; }
286
287     public synchronized void setExcessResourceMaxIdleTime( long millis )
288     throws ResourcePoolException
289     { this.excess_max_idle_time = millis; }
290
291     public synchronized long getExcessResourceMaxIdleTime()
292     throws ResourcePoolException
293     { return excess_max_idle_time; }
294
295     public synchronized long getDestroyOverdueResourceTime()
296     throws ResourcePoolException
297     { return destroy_overdue_resc_time; }
298
299     public synchronized void setDestroyOverdueResourceTime( long millis )
300     throws ResourcePoolException
301     { this.destroy_overdue_resc_time = millis; }
302
303     public synchronized void setExpirationEnforcementDelay( long expiration_enforcement_delay )
304     throws ResourcePoolException
305     { this.expiration_enforcement_delay = expiration_enforcement_delay; }
306
307     public synchronized long getExpirationEnforcementDelay()
308     throws ResourcePoolException
309     { return expiration_enforcement_delay; }
310
311     public synchronized void setBreakOnAcquisitionFailure( boolean break_on_acquisition_failure )
312     throws ResourcePoolException
313     { this.break_on_acquisition_failure = break_on_acquisition_failure; }
314
315     public synchronized boolean getBreakOnAcquisitionFailure()
316     throws ResourcePoolException
317     { return break_on_acquisition_failure; }
318
319     public synchronized void setDebugStoreCheckoutStackTrace( boolean debug_store_checkout_stacktrace )
320     throws ResourcePoolException
321     { this.debug_store_checkout_stacktrace = debug_store_checkout_stacktrace; }
322
323     public synchronized boolean getDebugStoreCheckoutStackTrace()
324     throws ResourcePoolException
325     { return debug_store_checkout_stacktrace; }
326
327     public synchronized ResourcePool createPool(ResourcePool.Manager mgr)
328     throws ResourcePoolException
329     {
330     if (liveChildren == null)
331         createThreadResources();
332     //System.err.println("Created liveChildren: " + liveChildren);
333
ResourcePool child = new BasicResourcePool( mgr,
334                             start,
335                             min,
336                             max,
337                             inc,
338                             retry_attempts,
339                             retry_delay,
340                             idle_resource_test_period,
341                             max_age,
342                             max_idle_time,
343                             excess_max_idle_time,
344                             destroy_overdue_resc_time,
345                             expiration_enforcement_delay,
346                             break_on_acquisition_failure,
347                             debug_store_checkout_stacktrace,
348                             taskRunner,
349                             asyncEventQueue,
350                             timer,
351                             this );
352     liveChildren.add( child );
353     return child;
354     }
355 }
356
357
358
359
360
361
362
363
Popular Tags