private void createShutdownHook() {
    // Add a shutdown hook for cleanup, if there are elements remaining in the cache which were not cleaned up.
    // This is the best effort approach. Ignore any error while doing so. Notice that most of the clients
    // would get cleaned up via either the removalListener or the close() call, only the active clients
    // that are in the cache or expired but being used in other threads wont get cleaned. The following code will only
    // clean the active cache ones. The ones expired from cache but being hold by other threads are in the mercy
    // of finalize() being called.
    Thread cleanupHiveClientShutdownThread = new Thread() {
      @Override
      public void run() {
        LOG.debug("Cleaning up hive client cache in ShutDown hook");
        cleanupHandle.cancel(false); // Cancel the maintenance thread.
        closeAllClientsQuietly();
      }
    };

    ShutdownHookManager.addShutdownHook(cleanupHiveClientShutdownThread);
  }
Create the cleanup handle. In addition to cleaning up every cleanupInterval, we add
a slight offset, so that the very first time it runs, it runs with a slight delay, so
as to catch any other connections that were closed when the first timeout happened.
As a result, the time we can expect an unused connection to be reaped is
5 seconds after the first timeout, and then after that, it'll check for whether or not
it can be cleaned every max(DEFAULT_HIVE_CACHE_EXPIRY_TIME_SECONDS,timeout) seconds
hcatalog/core/src/main/java/org/apache/hive/hcatalog/common/HiveClientCache.java