/** Returns the object name of the management bean. */ private static ObjectName getObjectName(Cache<?, ?> cache, MBeanType type) { String cacheManagerName = sanitize(cache.getCacheManager().getURI().toString()); String cacheName = sanitize(cache.getName()); try { String name = String.format("javax.cache:type=Cache%s,CacheManager=%s,Cache=%s", type, cacheManagerName, cacheName); return new ObjectName(name); } catch (MalformedObjectNameException e) { String msg = String.format("Illegal ObjectName for cacheManager=[%s], cache=[%s]", cacheManagerName, cacheName); throw new CacheException(msg, e); } }
@Override public CacheManager getCacheManager() { return cache.getCacheManager(); }
private void checkProviders(Cache<K, GridBucketState> cache) { CacheManager cacheManager = cache.getCacheManager(); if (cacheManager == null) { return; } CachingProvider cachingProvider = cacheManager.getCachingProvider(); if (cachingProvider == null) { return; } String providerClassName = cachingProvider.getClass().getName(); incompatibleProviders.forEach((providerPrefix, recommendation) -> { if (providerClassName.startsWith(providerPrefix)) { String message = "The Cache provider " + providerClassName + " is incompatible with Bucket4j, " + recommendation; throw new UnsupportedOperationException(message); } }); }
private void checkProviders(Cache<K, GridBucketState> cache) { CacheManager cacheManager = cache.getCacheManager(); if (cacheManager == null) { return; } CachingProvider cachingProvider = cacheManager.getCachingProvider(); if (cachingProvider == null) { return; } String providerClassName = cachingProvider.getClass().getName(); incompatibleProviders.forEach((providerPrefix, recommendation) -> { if (providerClassName.startsWith(providerPrefix)) { String message = "The Cache provider " + providerClassName + " is incompatible with Bucket4j, " + recommendation; throw new UnsupportedOperationException(message); } }); }
/** {@inheritDoc} */ public String getCacheProviderName() { return "jCache:" + featuresCache.getName() + ":" + featuresCache.getCacheManager().getCachingProvider().toString(); }
/** * print all of the content of the cache, if expires or not exist you will see a null value */ protected void printContent(Cache<String, Integer> cache) { System.out.println("==============> " + cache.getName() + "@ URI:" + cache.getCacheManager().getURI() + " <====================="); for (int i = 0; i < 10; i++) { final String key = "theKey-" + i; System.out.println("Key: " + key + ", Value: " + cache.get(key)); } System.out.println("============================================================"); }
@Override public void execute(Context context) throws Exception { Cache<Integer, User> userCache = context.getUserCache(); userCache.getCacheManager().getCachingProvider().close(); System.exit(0); }
/** Returns the object name of the management bean. */ private static ObjectName getObjectName(Cache<?, ?> cache, MBeanType type) { String cacheManagerName = sanitize(cache.getCacheManager().getURI().toString()); String cacheName = sanitize(cache.getName()); try { String name = String.format("javax.cache:type=Cache%s,CacheManager=%s,Cache=%s", type, cacheManagerName, cacheName); return new ObjectName(name); } catch (MalformedObjectNameException e) { String msg = String.format("Illegal ObjectName for cacheManager=[%s], cache=[%s]", cacheManagerName, cacheName); throw new CacheException(msg, e); } }
/** * Creates an object name using the scheme * "javax.cache:type=Cache<Statistics|Configuration>,CacheManager=<cacheManagerName>,name=<cacheName>" * <p> * Implementation note: Method modeled after the JSR107 RI */ private static ObjectName calculateObjectName(javax.cache.Cache<?, ?> cache, String objectNameType) { String cacheManagerName = mbeanSafe(cache.getCacheManager().getURI().toString()); String cacheName = mbeanSafe(cache.getName()); try { /** * JSR107 There seems to be a naming mismatch. * - (correct) The RI uses "type=CacheConfiguration" and "type=CacheStatistics" * - (wrong) The API docs of CacheManager.enableManagement() specify "type=Cache" and "type=CacheStatistics" */ return new ObjectName("javax.cache:type=Cache" + objectNameType + ",CacheManager=" + cacheManagerName + ",Cache=" + cacheName); } catch (MalformedObjectNameException e) { throw new CacheException("Illegal ObjectName for Management Bean. " + "CacheManager=[" + cacheManagerName + "], Cache=[" + cacheName + "] type=[" + objectNameType + "]", e); } }
@Test public void initialise() { try { cache.getCacheManager(); } catch (IllegalStateException e) { fail("Should be able to access the CacheManager for a new Cache"); } }
@Override public void onRemoved(Iterable iterable) throws CacheEntryListenerException { for (CacheListener cl : cacheListeners) { iterable.forEach(o -> { CacheEntryEvent<Object, CacheEntry> evt = (CacheEntryEvent) o; if (evt.getKey() == null) { //clear / removeall try { cl.wouldClearCache(Class.forName(evt.getSource().getCacheManager().getURI().toString())); } catch (ClassNotFoundException e) { //TODO: Implement Handling throw new CacheEntryListenerException("Could not get type", e); } } else { cl.wouldRemoveEntryFromCache(evt.getKey(), evt.getValue(), false); } }); } }
@Test public void testCacheMXBeanManagementTurnedOff() throws Exception { cache.getCacheManager().enableManagement(cache.getName(), false); try { lookupManagementAttribute(cache, CacheConfiguration, "ReadThrough"); fail(); } catch (javax.management.InstanceNotFoundException e) { //expected. Shouldn't be there } }
/** * Creates an object name using the scheme * "javax.cache:type=Cache<Statistics|Configuration>,CacheManager=<cacheManagerName>,name=<cacheName>" */ public static ObjectName calculateObjectName(Cache cache, MBeanType type) { try { ObjectName name = new ObjectName("javax.cache:type=" + type + "," + "CacheManager=" + mbeanSafe(cache.getCacheManager().getURI().toString()) + ",Cache=" + mbeanSafe(cache.getName())); return name; } catch (MalformedObjectNameException e) { throw new CacheException(e); } }
@Before public void moreSetUp() { cache = getCacheManager().getCache(getTestCacheName(), Long.class, String.class); cache.getCacheManager().enableStatistics(cache.getName(), true); statisticsUpdateTimeoutMillis = Integer.valueOf(System.getProperty(STATISTICS_UPDATE_TIMEOUT_PROPERTY, DEFAULT_STATISTICS_UPDATE_TIMEOUT_SECONDS)); }
@Before public void moreSetUp() { cache = getCacheManager().getCache(getTestCacheName(), Long.class, String.class); cache.getCacheManager().enableStatistics(cache.getName(), true); cache.getCacheManager().enableManagement(cache.getName(), true); }
/** * Clean up the {@link CacheManager} and {@link Cache} after a test. */ @After public void onAfterEachTest() { //destroy the cache String cacheName = cache.getName(); cache.getCacheManager().destroyCache(cacheName); //close the server cacheEntryListenerServer.close(); cacheEntryListenerServer = null; cache = null; }
public static Cache getAPIContextCache() { CacheManager contextCacheManager = Caching.getCacheManager(AppMConstants.API_CONTEXT_CACHE_MANAGER) .getCache(AppMConstants.API_CONTEXT_CACHE) .getCacheManager(); if (!isContextCacheInitialized) { isContextCacheInitialized = true; return contextCacheManager.<String, Boolean> createCacheBuilder(AppMConstants.API_CONTEXT_CACHE_MANAGER) .setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration( TimeUnit.DAYS, AppMConstants.API_CONTEXT_CACHE_EXPIRY_TIME_IN_DAYS)) .setStoreByValue(false).build(); } else { return Caching.getCacheManager(AppMConstants.API_CONTEXT_CACHE_MANAGER) .getCache(AppMConstants.API_CONTEXT_CACHE); } }
@Test public void testGetCacheManager() throws Exception { String cacheName = "SampleCache"; URI uri = Caching.getCachingProvider().getDefaultURI(); ClassLoader cl1 = Thread.currentThread().getContextClassLoader(); ClassLoader cl2 = URLClassLoader.newInstance(new URL[]{}, cl1); CacheManager cacheManager1 = Caching.getCachingProvider().getCacheManager(uri, cl1); CacheManager cacheManager2 = Caching.getCachingProvider().getCacheManager(uri, cl2); assertNotSame(cacheManager1, cacheManager2); cacheManager1.createCache(cacheName, new MutableConfiguration()); Cache cache1 = cacheManager1.getCache(cacheName); cacheManager2.createCache(cacheName, new MutableConfiguration()); Cache cache2 = cacheManager2.getCache(cacheName); assertSame(cacheManager1, cache1.getCacheManager()); assertSame(cacheManager2, cache2.getCacheManager()); }
@Test public void testExpiryOnCreation() throws Exception { // close cache since need to configure cache with ExpireOnCreationPolicy CacheManager mgr = cache.getCacheManager(); mgr.destroyCache(cache.getName()); MutableConfiguration<Long, String> config = new MutableConfiguration<Long, String>(); config.setStatisticsEnabled(true); config.setTypes(Long.class, String.class); config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(ExpireOnCreationPolicy.class)); cache = mgr.createCache(getTestCacheName(), config); cache.put(1L, "hello"); assertEventually(new AssertionRunnable() { @Override public void run() throws Exception { assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts")); } }, statisticsUpdateTimeoutMillis); Map<Long, String> map = new HashMap<Long, String>(); map.put(2L, "goodbye"); map.put(3L, "world"); cache.putAll(map); assertEventually(new AssertionRunnable() { @Override public void run() throws Exception { assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts")); } }, statisticsUpdateTimeoutMillis); }
@Test public void testCustomConfiguration() throws Exception { boolean storeByValue = false; MutableConfiguration configuration = new MutableConfiguration() .setReadThrough(false).setWriteThrough(false).setStoreByValue(storeByValue) .setTypes(java.math.BigDecimal.class, java.awt.Color.class) .setManagementEnabled(true).setStatisticsEnabled(false); Cache cache = null; try { cache = getCacheManager().createCache("customCache", configuration); } catch (UnsupportedOperationException e) { storeByValue = true; configuration.setStoreByValue(storeByValue); cache = getCacheManager().createCache("customCache", configuration); } assertEquals("java.math.BigDecimal", lookupManagementAttribute(cache, CacheConfiguration, "KeyType")); assertEquals("java.awt.Color", lookupManagementAttribute(cache, CacheConfiguration, "ValueType")); assertEquals(false, lookupManagementAttribute(cache, CacheConfiguration, "ReadThrough")); assertEquals(false, lookupManagementAttribute(cache, CacheConfiguration, "WriteThrough")); assertEquals(storeByValue, lookupManagementAttribute(cache, CacheConfiguration, "StoreByValue")); assertEquals(false, lookupManagementAttribute(cache, CacheConfiguration, "StatisticsEnabled")); assertEquals(true, lookupManagementAttribute(cache, CacheConfiguration, "ManagementEnabled")); //this used to just call close() but that does not work if an implementation maintains statistics on the cluster. cache.getCacheManager().destroyCache("customCache"); }