1 17 package org.apache.excalibur.mpool; 18 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Random ; 22 23 import org.apache.commons.collections.StaticBucketMap; 24 import org.apache.excalibur.event.Sink; 25 import org.apache.excalibur.event.command.RepeatedCommand; 26 27 36 public class DefaultPoolManager implements PoolManager 37 { 38 private final Random m_keyGenerator; 39 private final Map m_keyMap = new StaticBucketMap(); 40 private final Map m_factoryMap = new StaticBucketMap(); 41 42 public DefaultPoolManager() 43 { 44 this( null ); 45 } 46 47 public DefaultPoolManager( final Sink commandSink ) 48 { 49 m_keyGenerator = new Random (); 50 51 if( null != commandSink ) 52 { 53 try 54 { 55 commandSink.enqueue( new PoolManagerCommand( m_keyMap ) ); 56 } 57 catch( Exception e ) 58 { 59 } 61 } 62 } 63 64 67 public Pool getManagedPool( ObjectFactory factory, int initialEntries ) 68 throws Exception 69 { 70 ManagablePool pool = (ManagablePool)m_factoryMap.get( factory ); 71 72 if( null == pool ) 73 { 74 final long poolKey = getKey(); 75 pool = new VariableSizePool( factory, initialEntries, poolKey ); 76 m_keyMap.put( pool, new Long ( poolKey ) ); 77 m_factoryMap.put( factory, pool ); 78 } 79 80 return pool; 81 } 82 83 86 private final long getKey() 87 { 88 return m_keyGenerator.nextLong(); 89 } 90 91 private static final class PoolManagerCommand implements RepeatedCommand 92 { 93 private final Map m_map; 94 private final int m_min = 4; 95 private final int m_max = 256; 96 private final int m_grow = 4; 97 98 protected PoolManagerCommand( Map map ) 99 { 100 m_map = map; 101 } 102 103 public long getDelayInterval() 104 { 105 return 10 * 1000L; 106 } 107 108 public long getRepeatInterval() 109 { 110 return 10 * 1000L; 111 } 112 113 public int getNumberOfRepeats() 114 { 115 return 0; 116 } 117 118 public void execute() 119 throws Exception 120 { 121 Iterator i = m_map.keySet().iterator(); 122 123 while( i.hasNext() ) 124 { 125 ManagablePool pool = (ManagablePool)i.next(); 126 long key = ( (Long )m_map.get( pool ) ).longValue(); 127 int size = pool.size( key ); 128 129 if( size < m_min ) 130 { 131 pool.grow( m_grow, key ); 132 } 133 134 if( size > m_max ) 135 { 136 pool.shrink( m_grow, key ); 137 } 138 } 139 } 140 } 141 } 142 | Popular Tags |