public void close(Handler<AsyncResult<Void>> handler) { deleteCacheDir(handler); if (shutdownHook != null) { // May throw IllegalStateException if called from other shutdown hook so ignore that try { Runtime.getRuntime().removeShutdownHook(shutdownHook); } catch (IllegalStateException ignore) { } } }
private void setupCacheDir() { String cacheDirName = CACHE_DIR_BASE + "/file-cache-" + UUID.randomUUID().toString(); cacheDir = new File(cacheDirName); if (!cacheDir.mkdirs()) { throw new IllegalStateException("Failed to create cache dir"); } // Add shutdown hook to delete on exit shutdownHook = new Thread( () -> { CountDownLatch latch = new CountDownLatch(1); deleteCacheDir(ar -> latch.countDown()); try { latch.await(10, TimeUnit.SECONDS); } catch (Exception ignore) { } }); Runtime.getRuntime().addShutdownHook(shutdownHook); }