Class AbstractPoolingContainer<T>

  • All Implemented Interfaces:
    PoolingContainer<T>

    public abstract class AbstractPoolingContainer<T>
    extends Object
    implements PoolingContainer<T>
    This class will pool resources up to the passedLimit. It will never block and instead will always allocate a new resource if there are none in the pool.
    • Field Detail

      • allocatedResources

        protected final AtomicLong allocatedResources
    • Constructor Detail

      • AbstractPoolingContainer

        public AbstractPoolingContainer​(int itemsToPool)
        Pool up to itemsToPool resources. If the internal pool is already full, then additional ones will be thrown away.

        If items is Integer.MAX_VALUE, then we will use an unbounded queue with ConcurrentLinkedQueue which should have better performance because it is lock-free

        Parameters:
        itemsToPool - must be at least 1
    • Method Detail

      • getAllocatedResources

        public long getAllocatedResources()
      • getMaxPoolSize

        public int getMaxPoolSize()
      • createNewPooledResource

        @Nonnull
        protected abstract T createNewPooledResource()
        This should create a new resource. It should be non-null.
      • cleanupForDiscard

        protected void cleanupForDiscard​(T discardedResource)
        This will be called after a resource is done being used but the pool is full and it will be discarded

        This should not throw because it is called in finally blocks.

      • cleanupForReturnToPool

        protected void cleanupForReturnToPool​(T resourceToReturn)
        This will be called after a resource is done being used and will be returned to the pool so it maybe used again.

        cleanupForDiscard(Object) may be called for this resource after this returns if the pool was already full or shutdownPooling() has been called.

        This should not throw because it is called in finally blocks.

      • runWithPooledResource

        public <V> V runWithPooledResource​(com.google.common.base.Function<T,​V> f)
        This will run the provided function with a pooled resource. createNewPooledResource() will be called if it cannot get one from the pool. This function is not allowed to hold onto this resource after it completes. If it does, there will be undefined behavior.
        Specified by:
        runWithPooledResource in interface PoolingContainer<T>
        Returns:
        whatever the passed function returns
      • getResource

        protected final T getResource()
        This should always be followed by a try/finally
      • returnResource

        protected final void returnResource​(T resource)
        This method should only be called in the finally block of a try/finally
      • discardCurrentPool

        protected void discardCurrentPool()
        This method is used when we still want to continue to pool, but we think that the current pooled resources are no longer good to use.