Beispiel #1
0
 public void AddObject()
 {
     lock (sync)
     {
         passiveObjects.Add(PoolableObjectFactory.MakeObject());
     }
 }
Beispiel #2
0
 public object BorrowObject()
 {
     lock (sync)
     {
         if (closed)
         {
             throw new InvalidOperationException("The pool is closed.");
         }
         object useable = null;
         IEnumerator <object> objects          = passiveObjects.GetEnumerator();
         List <object>        destroyedObjects = new List <object>();
         while (objects.MoveNext() && useable == null)
         {
             PoolableObjectFactory.ActivateObject(objects.Current);
             if (PoolableObjectFactory.ValidateObject(objects.Current))
             {
                 useable = objects.Current;
             }
             else
             {
                 PoolableObjectFactory.DestroyObject(objects.Current);
                 destroyedObjects.Add(objects.Current);
             }
         }
         passiveObjects.RemoveAll(o => destroyedObjects.Contains(o));
         if (useable == null)
         {
             useable = PoolableObjectFactory.MakeObject();
             PoolableObjectFactory.ActivateObject(useable);
         }
         activeObjects.Add(useable);
         return(useable);
     }
 }
Beispiel #3
0
 protected override ObjectPool <Object> MakeEmptyPool(PoolableObjectFactory <Object> factory)
 {
     if (this.GetType() != typeof(TestBaseObjectPool))
     {
         Assert.Fail("Subclasses of TestBaseObjectPool must reimplement this method.");
     }
     throw new NotSupportedException("BaseObjectPool isn't a complete implementation.");
 }
Beispiel #4
0
        /**
         * <p>Create a new <tt>StackObjectPool</tt> using the specified <code>factory</code> to create new instances,
         * capping the number of "sleeping" instances to <code>maxIdle</code>, and initially allocating a container
         * capable of containing at least <code>initIdleCapacity</code> instances.  The pool is not pre-populated.
         * The <code>initIdleCapacity</code> parameter just determines the initial size of the underlying
         * container, which can increase beyond this value if <code>maxIdle &gt; initIdleCapacity.</code></p>
         *
         * <p>Negative values of <code>maxIdle</code> are ignored (i.e., the pool is created using
         * {@link #DEFAULT_MAX_SLEEPING}) as are non-positive values for <code>initIdleCapacity.</code>
         *
         * @param factory the {@link PoolableObjectFactory} used to populate the pool
         * @param maxIdle cap on the number of "sleeping" instances in the pool
         * @param initIdleCapacity initial size of the pool (this specifies the size of the container,
         *             it does not cause the pool to be pre-populated.)
         */
        public StackObjectPool(PoolableObjectFactory <T> factory, int maxIdle, int initIdleCapacity)
        {
            _factory     = factory;
            _maxSleeping = (maxIdle < 0 ? DEFAULT_MAX_SLEEPING : maxIdle);
            int initcapacity = (initIdleCapacity < 1 ? DEFAULT_INIT_SLEEPING_CAPACITY : initIdleCapacity);

            _pool = new java.util.Stack <T>();
            _pool.ensureCapacity(initcapacity > _maxSleeping ? _maxSleeping : initcapacity);
        }
 public SoftReferenceObjectPool(PoolableObjectFactory <T> factory, int initSize)
 {//throws Exception, IllegalArgumentException {
     if (factory == null)
     {
         throw new java.lang.IllegalArgumentException("factory required to prefill the pool.");
     }
     _pool    = new java.util.ArrayList <java.lang.refj.SoftReference <T> >(initSize);
     _factory = factory;
     PoolUtils.prefill(this, initSize);
 }
Beispiel #6
0
 public ObjectPool(PoolableObjectFactory <T> generator)
 {
     if (generator == null)
     {
         throw new ArgumentNullException("generator");
     }
     this.open      = true;
     this.rwlock    = new ReaderWriterLock();
     this.pool      = new ConcurrentQueue <T>();
     this.generator = generator;
 }
Beispiel #7
0
 protected static T GetFromPool(PoolableObjectFactory factory = null)
 {
     if (ObjectPool.TryDequeue(out T result))
     {
         return(result);
     }
     else
     {
         if (factory == null)
         {
             return(default(T));
         }
         return(factory.Invoke());
     }
 }
Beispiel #8
0
 public void setFactory(PoolableObjectFactory <T> factory)
 {//throws IllegalStateException {
     lock (this)
     {
         assertOpen();
         if (0 < getNumActive())
         {
             throw new java.lang.IllegalStateException("Objects are already active");
         }
         else
         {
             clear();
             _factory = factory;
         }
     }
 }
Beispiel #9
0
 /**
  * Create a new GenericObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
  * @param whenExhaustedAction the action to take when the pool is exhausted.
  * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
  * @param maxIdle the maximum number of idle objects in my pool.
  * @param minIdle the minimum number of idle objects in my pool.
  * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
  * @param testOnReturn whether to validate objects after they are returned to the returnObject.
  * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
  * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread.
  * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
  * @param testWhileIdle whether or not to validate objects in the idle object eviction thread.
  * @param softMinEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool.
  * @param lifo whether or not objects are returned in last-in-first-out order from the idle object pool.
  * @since Pool 1.4
  * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean, long, boolean)
  */
 public GenericObjectPoolFactory(PoolableObjectFactory <T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, bool testOnBorrow, bool testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, bool testWhileIdle, long softMinEvictableIdleTimeMillis, bool lifo)
 {
     _maxIdle                        = maxIdle;
     _minIdle                        = minIdle;
     _maxActive                      = maxActive;
     _maxWait                        = maxWait;
     _whenExhaustedAction            = whenExhaustedAction;
     _testOnBorrow                   = testOnBorrow;
     _testOnReturn                   = testOnReturn;
     _testWhileIdle                  = testWhileIdle;
     _timeBetweenEvictionRunsMillis  = timeBetweenEvictionRunsMillis;
     _numTestsPerEvictionRun         = numTestsPerEvictionRun;
     _minEvictableIdleTimeMillis     = minEvictableIdleTimeMillis;
     _softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
     _lifo    = lifo;
     _factory = factory;
 }
Beispiel #10
0
 /**
  * Create a new StackObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param maxIdle cap on the number of "sleeping" instances in the pool.
  * @param initIdleCapacity - initial size of the pool (this specifies the size of the container,
  * it does not cause the pool to be pre-populated.)
  */
 public StackObjectPoolFactory(PoolableObjectFactory <T> factory, int maxIdle, int initIdleCapacity)
 {
     _factory      = factory;
     _maxSleeping  = maxIdle;
     _initCapacity = initIdleCapacity;
 }
Beispiel #11
0
 public virtual void setFactory(PoolableObjectFactory <T> factory)
 {// throws IllegalStateException, UnsupportedOperationException {
     throw new java.lang.UnsupportedOperationException();
 }
Beispiel #12
0
 /**
  * Create a new GenericObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
  * @param whenExhaustedAction the action to take when the pool is exhausted.
  * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
  * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
  * @param testOnReturn whether to validate objects after they are returned to the returnObject.
  * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, boolean, boolean)
  */
 public GenericObjectPoolFactory(PoolableObjectFactory <T> factory, int maxActive, byte whenExhaustedAction, long maxWait, bool testOnBorrow, bool testOnReturn)
     :
     this(factory, maxActive, whenExhaustedAction, maxWait, GenericObjectPool <T> .DEFAULT_MAX_IDLE, GenericObjectPool <T> .DEFAULT_MIN_IDLE, testOnBorrow, testOnReturn, GenericObjectPool <T> .DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, GenericObjectPool <T> .DEFAULT_NUM_TESTS_PER_EVICTION_RUN, GenericObjectPool <T> .DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, GenericObjectPool <T> .DEFAULT_TEST_WHILE_IDLE)
 {
 }
Beispiel #13
0
 /**
  * Create a new GenericObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
  * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int)
  */
 public GenericObjectPoolFactory(PoolableObjectFactory <T> factory, int maxActive) :
     this(factory, maxActive, GenericObjectPool <T> .DEFAULT_WHEN_EXHAUSTED_ACTION, GenericObjectPool <T> .DEFAULT_MAX_WAIT, GenericObjectPool <T> .DEFAULT_MAX_IDLE, GenericObjectPool <T> .DEFAULT_MIN_IDLE, GenericObjectPool <T> .DEFAULT_TEST_ON_BORROW, GenericObjectPool <T> .DEFAULT_TEST_ON_RETURN, GenericObjectPool <T> .DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, GenericObjectPool <T> .DEFAULT_NUM_TESTS_PER_EVICTION_RUN, GenericObjectPool <T> .DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, GenericObjectPool <T> .DEFAULT_TEST_WHILE_IDLE)
 {
 }
Beispiel #14
0
 /**
  * Create a new GenericObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param config a non-<code>null</code> GenericObjectPool<T>.Config describing the configuration.
  * @throws NullPointerException when config is <code>null</code>.
  * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, GenericObjectPool<T>.Config)
  */
 public GenericObjectPoolFactory(PoolableObjectFactory <T> factory, GenericObjectPool <T> .Config config) :// throws NullPointerException {
     this(factory, config.maxActive, config.whenExhaustedAction, config.maxWait, config.maxIdle, config.minIdle, config.testOnBorrow, config.testOnReturn, config.timeBetweenEvictionRunsMillis, config.numTestsPerEvictionRun, config.minEvictableIdleTimeMillis, config.testWhileIdle, config.softMinEvictableIdleTimeMillis, config.lifo)
 {
 }
 protected abstract ObjectPoolFactory <Object> MakeFactory(PoolableObjectFactory <Object> objectFactory);
Beispiel #16
0
 /**
  * Create a new GenericObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
  * @param whenExhaustedAction the action to take when the pool is exhausted.
  * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
  * @param maxIdle the maximum number of idle objects in my pool.
  * @param minIdle the minimum number of idle objects in my pool.
  * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
  * @param testOnReturn whether to validate objects after they are returned to the returnObject.
  * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
  * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread.
  * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
  * @param testWhileIdle whether or not to validate objects in the idle object eviction thread.
  * @param softMinEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool.
  * @since Pool 1.3
  * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean, long)
  */
 public GenericObjectPoolFactory(PoolableObjectFactory <T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, bool testOnBorrow, bool testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, bool testWhileIdle, long softMinEvictableIdleTimeMillis)
     :
     this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, minIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle, softMinEvictableIdleTimeMillis, GenericObjectPool <T> .DEFAULT_LIFO)
 {
 }
Beispiel #17
0
 /// <summary>
 /// Create an ObjectPool with the specified factory.  The pool should be in a default
 /// configuration and conform to the expected behaviors described in ObjectPool.
 /// Generally speaking there should be no limits on the various object counts.
 /// throws NotSupportedException if the pool being tested does not follow pool contracts.
 /// </summary>
 protected abstract ObjectPool <Object> MakeEmptyPool(PoolableObjectFactory <Object> factory);
Beispiel #18
0
 /**
  * Create a new <tt>SimpleObjectPool</tt> using the specified <i>factory</i> to create new instances,
  * capping the number of "sleeping" instances to <i>maxIdle</i>.
  *
  * @param factory the {@link PoolableObjectFactory} used to populate the pool
  * @param maxIdle cap on the number of "sleeping" instances in the pool
  */
 public StackObjectPool(PoolableObjectFactory <T> factory, int maxIdle) :
     this(factory, maxIdle, DEFAULT_INIT_SLEEPING_CAPACITY)
 {
 }
 public GenericObjectPool(PoolableObjectFactory <T> factory) :
     this(factory, new GenericObjectPoolConfig())
 {
 }
 /**
  * Create a <code>SoftReferenceObjectPool</code> with the specified factory.
  *
  * @param factory object factory to use.
  */
 public SoftReferenceObjectPool(PoolableObjectFactory <T> factory)
 {
     _pool    = new java.util.ArrayList <java.lang.refj.SoftReference <T> >();
     _factory = factory;
 }
 public GenericObjectPool(PoolableObjectFactory <T> factory, GenericObjectPoolConfig config) : base()
 {
     this.factory = factory;
     Config       = config;
 }
 protected abstract ObjectPoolFactory<Object> MakeFactory(PoolableObjectFactory<Object> objectFactory);
Beispiel #23
0
 protected ReusableObjectsPool(PoolableObjectFactory factory = null, PoolableObjectCheck check = null)
 {
     this.FromPool            = GetFromPool(factory);
     this.OnReturnToPoolCheck = check;
 }
Beispiel #24
0
 /**
  * Create a new GenericObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
  * @param whenExhaustedAction the action to take when the pool is exhausted.
  * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
  * @param maxIdle the maximum number of idle objects in my pool.
  * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int)
  */
 public GenericObjectPoolFactory(PoolableObjectFactory <T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle)
     :
     this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, GenericObjectPool <T> .DEFAULT_MIN_IDLE, GenericObjectPool <T> .DEFAULT_TEST_ON_BORROW, GenericObjectPool <T> .DEFAULT_TEST_ON_RETURN, GenericObjectPool <T> .DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, GenericObjectPool <T> .DEFAULT_NUM_TESTS_PER_EVICTION_RUN, GenericObjectPool <T> .DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, GenericObjectPool <T> .DEFAULT_TEST_WHILE_IDLE)
 {
 }
Beispiel #25
0
 /**
  * Create a new GenericObjectPoolFactory.
  *
  * @param factory the PoolableObjectFactory used by created pools.
  * @param maxActive maximum number of objects that can be borrowed from created pools at one time.
  * @param whenExhaustedAction the action to take when the pool is exhausted.
  * @param maxWait the maximum amount of time to wait for an idle object when the pool is exhausted.
  * @param maxIdle the maximum number of idle objects in my pool.
  * @param minIdle the minimum number of idle objects in my pool.
  * @param testOnBorrow whether to validate objects before they are returned by the borrowObject.
  * @param testOnReturn whether to validate objects after they are returned to the returnObject.
  * @param timeBetweenEvictionRunsMillis the number of milliseconds to sleep between examining idle objects for eviction.
  * @param numTestsPerEvictionRun the number of idle objects to examine per run within the idle object eviction thread.
  * @param minEvictableIdleTimeMillis the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction.
  * @param testWhileIdle whether or not to validate objects in the idle object eviction thread.
  * @see GenericObjectPool#GenericObjectPool(PoolableObjectFactory, int, byte, long, int, int, boolean, boolean, long, int, long, boolean)
  */
 public GenericObjectPoolFactory(PoolableObjectFactory <T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, bool testOnBorrow, bool testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, bool testWhileIdle)
     :
     this(factory, maxActive, whenExhaustedAction, maxWait, maxIdle, minIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle, GenericObjectPool <T> .DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS)
 {
 }
Beispiel #26
0
 /**
  * Create a new <tt>StackObjectPool</tt> using the specified <i>factory</i> to create new instances.
  *
  * @param factory the {@link PoolableObjectFactory} used to populate the pool
  */
 public StackObjectPool(PoolableObjectFactory <T> factory) :
     this(factory, DEFAULT_MAX_SLEEPING, DEFAULT_INIT_SLEEPING_CAPACITY)
 {
 }
Beispiel #27
0
 /// <summary>
 /// Create an ObjectPool with the specified factory.  The pool should be in a default
 /// configuration and conform to the expected behaviors described in ObjectPool.
 /// Generally speaking there should be no limits on the various object counts.
 /// throws NotSupportedException if the pool being tested does not follow pool contracts.
 /// </summary>
 protected abstract ObjectPool<Object> MakeEmptyPool(PoolableObjectFactory<Object> factory);