@Override public void init() { register("hits", () -> cache.getStatistics().cacheHitCount()); register("in_memory_hits", () -> cache.getStatistics().localHeapHitCount()); register("off_heap_hits", () -> cache.getStatistics().localOffHeapHitCount()); register("on_disk_hits", () -> cache.getStatistics().localDiskHitCount()); register("misses", () -> cache.getStatistics().cacheMissCount()); register("in_memory_misses", () -> cache.getStatistics().localHeapMissCount()); register("off_heap_misses", () -> cache.getStatistics().localOffHeapMissCount()); register("on_disk_misses", () -> cache.getStatistics().localDiskMissCount()); register("objects", () -> cache.getStatistics().getSize()); register("in_memory_objects", () -> cache.getStatistics().getLocalHeapSize()); register("off_heap_objects", () -> cache.getStatistics().getLocalOffHeapSize()); register("on_disk_objects", () -> cache.getStatistics().getLocalDiskSize()); register("mean_get_time", () -> cache.getStatistics().cacheGetOperation().latency().average().value()); register("mean_search_time", () -> cache.getStatistics().cacheSearchOperation().latency().average().value()); register("eviction_count", () -> cache.getStatistics().cacheEvictionOperation().count().value()); register("searches_per_second", () -> cache.getStatistics().cacheSearchOperation().rate().value()); register("writer_queue_size", () -> cache.getStatistics().getWriterQueueLength()); status = Status.STATUS_ALIVE; }
@Override protected Collection<Cache> loadCaches() { net.sf.ehcache.CacheManager cacheManager = getCacheManager(); Assert.notNull(cacheManager, "A backing EhCache CacheManager is required"); Status status = cacheManager.getStatus(); Assert.isTrue(Status.STATUS_ALIVE.equals(status), "An 'alive' EhCache CacheManager is required - current cache is " + status.toString()); String[] names = cacheManager.getCacheNames(); Collection<Cache> caches = new LinkedHashSet<Cache>(names.length); for (String name : names) { caches.add(new EhCacheCache(cacheManager.getEhcache(name))); } return caches; }
@Test public void testEHCacheCompatiblity() throws Exception { // get the default cache manager CacheManagerFactory factory = new DefaultCacheManagerFactory(); CacheManager manager = factory.getInstance(); assertEquals(Status.STATUS_ALIVE, manager.getStatus()); // create another unrelated cache manager Configuration conf = ConfigurationFactory.parseConfiguration(DefaultCacheManagerFactory.class.getResource("/test-ehcache.xml")); assertNotNull(conf); conf.setName("otherCache"); CacheManager other = CacheManager.create(conf); assertEquals(Status.STATUS_ALIVE, other.getStatus()); // shutdown this unrelated cache manager other.shutdown(); assertEquals(Status.STATUS_SHUTDOWN, other.getStatus()); // the default cache manager should be still running assertEquals(Status.STATUS_ALIVE, manager.getStatus()); factory.doStop(); // the default cache manger is shutdown assertEquals(Status.STATUS_SHUTDOWN, manager.getStatus()); }
private static void logEvent(Ehcache cache, Element elem, CacheEventType eventType) { int cacheApproxSize = -1; if (cache.getStatus() == Status.STATUS_ALIVE) { try { cacheApproxSize = cache.getSize(); } catch (CacheException | IllegalStateException ignored) { } } LoggingEvent loggingEvent = new LoggingEvent(Logger.FQCN, LOGGER, Level.TRACE, String.format(MSG_FORMAT, cache.getName(), cache.getStatus(), cacheApproxSize, elem.getObjectKey(), eventType.getId()), null, null); StackTraceElement[] stackFrames = new Throwable().getStackTrace(); for (int a = 1; a < stackFrames.length; a++) { if (!STACK_SKIP_CLASS_NAMES.contains(stackFrames[a].getClassName())) { stackFrames = ArrayUtils.subarray(stackFrames, a, stackFrames.length); break; } } loggingEvent.setCallerData(stackFrames); LOGGER.callAppenders(loggingEvent); }
/** * Create an {@link EhCacheCache} instance. * @param ehcache backing Ehcache instance */ public EhCacheCache(Ehcache ehcache) { Assert.notNull(ehcache, "Ehcache must not be null"); Status status = ehcache.getStatus(); Assert.isTrue(Status.STATUS_ALIVE.equals(status), "An 'alive' Ehcache is required - current cache is " + status.toString()); this.cache = ehcache; }
@Override protected Collection<Cache> loadCaches() { Status status = getCacheManager().getStatus(); if (!Status.STATUS_ALIVE.equals(status)) { throw new IllegalStateException( "An 'alive' EhCache CacheManager is required - current cache is " + status.toString()); } String[] names = getCacheManager().getCacheNames(); Collection<Cache> caches = new LinkedHashSet<Cache>(names.length); for (String name : names) { caches.add(new EhCacheCache(getCacheManager().getEhcache(name))); } return caches; }
@Bean public net.sf.ehcache.CacheManager customEhCacheCacheManager() { net.sf.ehcache.CacheManager cacheManager = mock( net.sf.ehcache.CacheManager.class); given(cacheManager.getStatus()).willReturn(Status.STATUS_ALIVE); given(cacheManager.getCacheNames()).willReturn(new String[0]); return cacheManager; }
/** * {@inheritDoc} */ public Status getStatus() { if (this.peerListener != null) { return this.peerListener.getStatus(); } return Status.STATUS_ALIVE; }
@Test public void testConfig() throws Exception { //do some routes producerTemplate1.send(templateProcessor); producerTemplate2.send(templateProcessor); // Now do some routes to let endpoints be initialized template.sendBody("direct:add1", "Hello World"); template.sendBody("direct:add2", "Hello World"); //Now should not be null CacheManager cacheManager = cmfRef.getCacheManager(); assertNotNull("CacheManager initialized", cacheManager); Cache cache = cmfRef.getCacheManager().getCache("foo"); // Is cache alive assertEquals("Is cache still alive", Status.STATUS_ALIVE, cache.getStatus()); context.stopRoute(ROUTE1_ID); // Is cache still alive? assertEquals("Is cache still alive", Status.STATUS_ALIVE, cache.getStatus()); context.stop(); // Was the cache shutdowned with context? assertEquals("Is cache still alive", Status.STATUS_SHUTDOWN, cache.getStatus()); }
@Override public void init() { ApplicationContext applicationContext = CurrentApplicationContext.CURRENT.get(); HazelcastInstance hazelcastInstance = HazelcastHelper.getHazelcastInstance(applicationContext); topic = hazelcastInstance.getTopic("cyclos.EhCacheEvictionReplication"); topic.addMessageListener(this); status = Status.STATUS_ALIVE; LOG.info("EhCache replication has started using Hazelcast"); }
/** * Constructs a new {@link ExpiryTaskExtension} for the specified cache. * * @param cache the cache on which to evict expired elements * @param timer the timer to use internally for scheduling the task * @param expiryCheckPeriodMillis the period of the eviction task */ public ExpiryTaskExtension(Ehcache cache, Timer timer, long expiryCheckPeriodMillis) { ArgumentChecker.notNull(cache, "cache"); ArgumentChecker.notNull(timer, "timer"); _cache = cache; _timer = timer; _expiryCheckPeriodMillis = expiryCheckPeriodMillis; _status = Status.STATUS_UNINITIALISED; }
@Override public void init() { if (_expiryTask == null) { _expiryTask = new ExpiryTask(); _timer.scheduleAtFixedRate(_expiryTask, _expiryCheckPeriodMillis, _expiryCheckPeriodMillis); } _status = Status.STATUS_ALIVE; }
@Override public void dispose() throws CacheException { if (_expiryTask != null) { _expiryTask.cancel(); _expiryTask = null; } _status = Status.STATUS_SHUTDOWN; }
public static CacheStatus from(Status status) { if (Status.STATUS_ALIVE.equals(status)) { return ALIVE; } else if (Status.STATUS_UNINITIALISED.equals(status)) { return UNINITIALISED; } else if (Status.STATUS_SHUTDOWN.equals(status)) { return SHUTDOWN; } return UNKNOWN; }
@Override protected Collection<Cache> loadCaches() { Assert.notNull(this.cacheManager, "A backing EhCache CacheManager is required"); Status status = this.cacheManager.getStatus(); Assert.isTrue(Status.STATUS_ALIVE.equals(status), "An 'alive' EhCache CacheManager is required - current cache is " + status.toString()); String[] names = this.cacheManager.getCacheNames(); Collection<Cache> caches = new LinkedHashSet<Cache>(names.length); for (String name : names) { caches.add(new LegendCache(this, this.cacheManager.getEhcache(name))); } return caches; }
@Test public void getMissingCacheDefault() { final Ehcache ehcache = mock(Ehcache.class); when(ehcache.getStatus()).thenReturn(Status.STATUS_ALIVE); when(cacheManager.getEhcache(anyString())).thenReturn(ehcache); assertNotNull(autoCreatingEhCacheCacheManager.getMissingCache("SomeName")); }
@Override protected Collection<Cache> loadCaches() { Assert.notNull(this.cacheManager, "A backing EhCache CacheManager is required"); Status status = this.cacheManager.getStatus(); Assert.isTrue(Status.STATUS_ALIVE.equals(status), "An 'alive' EhCache CacheManager is required - current cache is " + status.toString()); String[] names = this.cacheManager.getCacheNames(); Collection<Cache> caches = new LinkedHashSet<Cache>(names.length); for (String name : names) { caches.add(new EhCacheCache(this.cacheManager.getEhcache(name))); } return caches; }
/** * Constructor * * @param cacheManager * @param cache */ public EhCache(Ehcache cache) { Assert.notNull(cache, "Ehcache must not be null"); Status status = cache.getStatus(); Assert.isTrue(Status.STATUS_ALIVE.equals(status), "An 'alive' Ehcache is required - current cache is " + status.toString()); this.cache = cache; }
public void shutdown() { if (cacheManager != null && cacheManager.getStatus() == Status.STATUS_ALIVE) { cacheManager.shutdown(); } }
@Override public Status getStatus() { return null; }
public CacheLifecycleListener(Cache cache, MetricRegistry metricRegistry) { this.cache = cache; this.metricRegistry = metricRegistry; this.status = Status.STATUS_UNINITIALISED; }
@Override public void dispose() throws CacheException { registeredMetrics.forEach(metricName -> metricRegistry.remove(metricName)); status = Status.STATUS_SHUTDOWN; }
@Override public Status getStatus() { return status; }
/** * Run an ehcache based client, against the Terracotta Server * */ public static void main(String[] args) throws IOException { String terracottaServerUrl = System.getenv("TERRACOTTA_SERVER_URL"); if(terracottaServerUrl == null || terracottaServerUrl.trim().equals("")) { System.out.println("The environment variable TERRACOTTA_SERVER_URL was not set; using terracotta:9510 as the cluster url."); terracottaServerUrl = "terracotta:9510"; } System.out.println("**** Programmatically configure an instance, configured to connect to : " + terracottaServerUrl + " ****"); Configuration managerConfiguration = new Configuration() .name("myCacheManager") .terracotta(new TerracottaClientConfiguration().url(terracottaServerUrl)) .cache(new CacheConfiguration() .name("myCache") .maxEntriesLocalHeap(50) .copyOnRead(true) .eternal(true) .terracotta(new TerracottaConfiguration()) ); CacheManager manager = CacheManager.create(managerConfiguration); try { Cache myCache = manager.getCache("myCache"); //myCache is now ready. Random random = new Random(); if (myCache.getSize() > 0) { System.out.println("**** We found some data in the cache ! I guess some other client inserted data in BigMemory ! **** "); } System.out.println("**** Starting inserting / getting elements **** "); while (!Thread.currentThread().isInterrupted() && manager.getStatus() == Status.STATUS_ALIVE) { // indexes spread between 0 and 999 int index = random.nextInt(1000); if (random.nextInt(10) < 3 && myCache.getSize() < 1000) { // put String value = new BigInteger(1024 * 128 * (1 + random.nextInt(10)), random).toString(16); System.out.println("Inserting at key " + index + " String of size : " + value.length() + " bytes"); myCache.put(new Element(index, value)); // construct a big string of 256k data } else { // get Element element = myCache.get(index); System.out.println("Getting key " + index + (element == null ? ", that was a miss" : ", THAT WAS A HIT !")); } Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } finally { if (manager != null) { manager.shutdown(); } } }
@Override public Status getStatus() { // TODO Auto-generated method stub return null; }
/** * {@inheritDoc} */ public Status getStatus() { return cacheBackend.getStatus(); }
@Test public void testConfig() throws Exception { producerTemplate.send(new Processor() { public void process(Exchange exchange) throws Exception { exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8"); Message in = exchange.getIn(); in.setHeader(CacheConstants.CACHE_OPERATION, CacheConstants.CACHE_OPERATION_ADD); in.setHeader(CacheConstants.CACHE_KEY, "greeting"); in.setBody("Hello World"); } }); CacheManager cm = cacheEndpoint.getCacheManagerFactory().getInstance(); Cache cache = cm.getCache(cacheEndpoint.getConfig().getCacheName()); Set<CacheEventListener> ehcacheEventListners = cache.getCacheEventNotificationService().getCacheEventListeners(); List<CacheLoader> cacheLoaders = cache.getRegisteredCacheLoaders(); CacheEventListenerRegistry configuredEventRegistry = cacheEndpoint.getConfig().getEventListenerRegistry(); CacheLoaderRegistry configuredLoaderRegistry = cacheEndpoint.getConfig().getCacheLoaderRegistry(); //Test if CacheEventListenerRegistry was referenced correctly assertEquals("CacheEventListenerRegistry size", 1, configuredEventRegistry.size()); //Expecting 1 loader to be configured via spring assertEquals("configuredLoaderRegistry size", 1, configuredLoaderRegistry.size()); //Expecting 1 listener added by us: TestCacheEventListener assertEquals("Number of registered listeners", 1, ehcacheEventListners.size()); assertEquals("Number of registered loaders", 1, cacheLoaders.size()); //Is our TestCacheEventListener really invoked? int puts = 0; for (Object listener : ehcacheEventListners) { if (listener instanceof TestCacheEventListener) { puts = ((TestCacheEventListener)listener).getNumberOfPuts(); break; } } assertEquals("TestCacheEventListener put invocations", 1, puts); //Is cache loader initialized by ehcache assertEquals("loader initialized", cacheLoaders.get(0).getStatus(), Status.STATUS_ALIVE); }
public TestLoader() { status = Status.STATUS_UNINITIALISED; }
@Override public void dispose() throws CacheException { status = Status.STATUS_SHUTDOWN; }
@Override public void init() { status = Status.STATUS_ALIVE; }
@Override public void dispose() throws CacheException { // Topic is already disposed on hazelcast shutdown status = Status.STATUS_SHUTDOWN; }
/** * @return the {@link Status} of the manager */ public Status getStatus() { return status; }
@Override public Status getStatus() { return _status; }
@Override public Status getStatus() { init(); return super.getStatus(); }