Class ResourcePool<T>


  • public class ResourcePool<T>
    extends java.lang.Object
    Resource pool class implements functionality for creating and maintaining a pool of reusable resources. It manages the lifecycle of underlying resources that it creates and destroys while ensuring that it enforces a ceiling on maximum concurrent resources and maximum idle resources. Users can request for new resource from this class and it will opportunistically use existing resource or create new resource and complete the request. It is callers responsibility to call close on the resource wrapper that is returned by the pool. Upon invoking close, a resource is automatically returned to the pool once its usage is done. If more resources are requested than the maximum concurrent allowed resource count, then this class will add them to a wait queue and as resources are returned to the pool, the waiting requests are fulfilled. If a returned resource is invalid, it is destroyed and a replacement resource is created to fulfill waiting requests. If there are no waiting requests, this class tries to maintain an idle pool of resources of maxidleSize. Once maximum allowed idle resources are present with it, any additional resource returned to it is discarded. The ResourcePool can be shutdown. However, the shutdown trigger does not prevent callers to attempt to create new resources as long as they have a valid handle to the resource pool. Shutdown ensures that it drains all idle resources once shutdown is triggered.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ResourcePool.CloseableResource<T>
      A closeable resource wrapper class which returns the resource back to the pool automatically once it is closed.
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourcePool​(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> tSupplier, java.util.function.Consumer<T> tDestroyer, int maxConcurrent, int maxIdle)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletableFuture<ResourcePool.CloseableResource<T>> getResource()
      Method to get a resource initialized with supplied arg.
      void shutdown()
      Shutdown the resource manager where all returned resources are closed and not put back into the idle queue of resources.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ResourcePool

        public ResourcePool​(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> tSupplier,
                            java.util.function.Consumer<T> tDestroyer,
                            int maxConcurrent,
                            int maxIdle)
    • Method Detail

      • getResource

        public java.util.concurrent.CompletableFuture<ResourcePool.CloseableResource<T>> getResource()
        Method to get a resource initialized with supplied arg. This method attempts to find an existing available resource. If not found, it submits a new waiting request for whenever a resource becomes available. A resource could become available because such a resource was returned to the pool or a new resource was created. It also opportunistically submits a request to create a new resource if required.
        Returns:
        A completableFuture which when completed will have the resource object that the caller requested.
      • shutdown

        public void shutdown()
        Shutdown the resource manager where all returned resources are closed and not put back into the idle queue of resources. It is important to note that even after shutdown is initiated, if `getresource` is invoked, it will return a resource.