1 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; int min = 1; 51 int max = 12; 52 int inc = 3; 53 int retry_attempts = -1; int retry_delay = 1000; long idle_resource_test_period = -1; long max_age = -1; long max_idle_time = -1; long excess_max_idle_time = -1; long destroy_overdue_resc_time = -1; long expiration_enforcement_delay = -1; 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 84 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; } 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 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 175 186 synchronized void markBroken( BasicResourcePool pool ) 187 { 188 if (liveChildren != null) { 191 liveChildren.remove( pool ); 192 if (liveChildren.isEmpty()) 193 destroyThreadResources(); 194 } 195 203 } 211 212 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 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 |