EndpointDiscoveryRefreshCache { public ScheduledFuture<?> loadAndScheduleEvict(final String key, final long refreshPeriod, final TimeUnit refreshPeriodTimeUnit) { return executorService.schedule(new Runnable() { @Override public void run() { evict(key); } }, refreshPeriod, refreshPeriodTimeUnit); } EndpointDiscoveryRefreshCache(CacheLoader cacheLoader); abstract URI get(K key, boolean required, URI defaultEndpoint); abstract URI put(String key, Map<String, String> endpointDetails, URI defaultEndpoint); void evict(String key); URI discoverEndpoint(String key, boolean required, URI defaultEndpoint); ScheduledFuture<URI> loadAndScheduleRefresh(final String key,
                                                       final long refreshPeriod,
                                                       final URI defaultEndpoint); ScheduledFuture<?> loadAndScheduleEvict(final String key,
                                                   final long refreshPeriod,
                                                   final TimeUnit refreshPeriodTimeUnit); void shutdown();  }
@Test public void loadAndScheduleEvict() throws Exception { CacheLoader<String, URI> cacheLoader = new CacheLoader<String, URI>() { @Override public URI load(String key) { return TEST_URI; } }; EndpointDiscoveryRefreshCache<String> endpointDiscoveryRefreshCache = new EndpointDiscoveryRefreshCache<String>(cacheLoader) { @Override public URI get(String key, boolean required, URI defaultEndpoint) { throw new UnsupportedOperationException(); } @Override public URI put(String key, Map<String, String> endpointDetails, URI defaultEndpoint) { throw new UnsupportedOperationException(); } }; endpointDiscoveryRefreshCache.cache.put("test-key", TEST_URI); endpointDiscoveryRefreshCache.loadAndScheduleEvict("test-key", 10, TimeUnit.MILLISECONDS); assertThat(endpointDiscoveryRefreshCache.cache, hasEntry("test-key", TEST_URI)); Thread.sleep(100); assertThat(endpointDiscoveryRefreshCache.cache, not(hasKey("test-key"))); }