/** * Default Constructor * * @param cache the Terracotta clustered Cache to snapshot * @param interval the interval to do the snapshots on * @param doKeySnapshotOnDedicatedThread whether the snapshots have to be done on a dedicated * thread * @param rotatingWriter the RotatingSnapshotFile to write to * @throws IllegalArgumentException if interval is less than or equal to zero */ KeySnapshotter( final Ehcache cache, final long interval, final boolean doKeySnapshotOnDedicatedThread, final RotatingSnapshotFile rotatingWriter) throws IllegalArgumentException { final Store store = new CacheStoreHelper((Cache) cache).getStore(); if (!(store instanceof TerracottaStore)) { throw new IllegalArgumentException( "Cache '" + cache.getName() + "' isn't backed by a " + TerracottaStore.class.getSimpleName() + " but uses a " + store.getClass().getName() + " instead"); } if (interval <= 0) { throw new IllegalArgumentException("Interval needs to be a positive & non-zero value"); } if (rotatingWriter == null) { throw new NullPointerException(); } this.cacheName = cache.getName(); this.rotatingWriter = rotatingWriter; this.tcStore = (TerracottaStore) store; if (doKeySnapshotOnDedicatedThread) { thread = new SnapShottingThread(this, interval, "KeySnapshotter for cache " + cacheName); thread.start(); } else { ScheduledExecutorService scheduledExecutorService = INSTANCES.get(cache.getCacheManager()); if (scheduledExecutorService == null) { scheduledExecutorService = new ScheduledThreadPoolExecutor(POOL_SIZE); final ScheduledExecutorService previous = INSTANCES.putIfAbsent(cache.getCacheManager(), scheduledExecutorService); if (previous != null) { scheduledExecutorService.shutdownNow(); scheduledExecutorService = previous; } } scheduledExecutorService.scheduleWithFixedDelay(this, interval, interval, TimeUnit.SECONDS); thread = null; } }
/** {@inheritDoc} */ public void run() { try { INSTANCES.cleanUp(); rotatingWriter.writeAll(tcStore.getLocalKeys()); onSnapshot(); } catch (Throwable e) { LOG.error("Couldn't snapshot local keySet for Cache {}", cacheName, e); } }
/** * Accessor to all known cacheManagers (which are also bound to a ScheduledExecutorService) * * @return the collection of known CacheManagers */ static Collection<CacheManager> getKnownCacheManagers() { return INSTANCES.keySet(); }