LoggingEventCache implements IFlushAndPublish { public static boolean shutDown() throws InterruptedException { boolean success = false; if (null != instance) { try { ExecutorService executorService = executorServiceRef.getAndSet(null); if (null != executorService) { System.out.println("LoggingEventCache: shutting down"); executorService.shutdown(); boolean terminated = executorService.awaitTermination( SHUTDOWN_TIMEOUT_SECS, TimeUnit.SECONDS ); System.out.println( String.format("LoggingEventCache: Executor service terminated within timeout: %s", terminated) ); success = terminated; } if (null != instance.cacheMonitor){ instance.cacheMonitor.shutDown(); } } finally { instance = null; } } return success; } LoggingEventCache(String cacheName, IBufferMonitor<T> cacheMonitor,
                             IBufferPublisher<T> cachePublisher); static boolean shutDown(); String getCacheName(); void add(T event); @Override Future<Boolean> flushAndPublish(boolean useCurrentThread); static final String PUBLISH_THREAD_NAME; }
@Test public void testShutdown() { final TestMonitor monitor = new TestMonitor(); try { LoggingEventCache<String> inst = new LoggingEventCache<>(CACHE_NAME, monitor, new PublisherStub()); LoggingEventCache.shutDown(); Assert.assertTrue("Monitor is shut down when LoggingEventCache is shut down", monitor.isShutdown); } catch (Exception e) { e.printStackTrace(); Assert.fail("Failing on error"); } }