Java Code Examples for javax.cache.configuration.MutableConfiguration#setStatisticsEnabled()

The following examples show how to use javax.cache.configuration.MutableConfiguration#setStatisticsEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testManagementDisabledOverriddenFromTemplate() throws Exception {
  CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml")
          .toURI(),
      provider.getDefaultClassLoader());

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(false);
  configuration.setStatisticsEnabled(false);

  Cache<Long, String> cache = cacheManager.createCache("enables-mbeans", configuration);

  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);
  assertThat(eh107Configuration.isManagementEnabled(), is(true));
  assertThat(eh107Configuration.isStatisticsEnabled(), is(true));

  assertThat(isMbeanRegistered("enables-mbeans", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("enables-mbeans", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 2
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testManagementEnabledOverriddenFromTemplate() throws Exception {
  CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml")
          .toURI(),
      provider.getDefaultClassLoader());

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(true);
  configuration.setStatisticsEnabled(true);

  Cache<Long, String> cache = cacheManager.createCache("disables-mbeans", configuration);

  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);
  assertThat(eh107Configuration.isManagementEnabled(), is(false));
  assertThat(eh107Configuration.isStatisticsEnabled(), is(false));

  assertThat(isMbeanRegistered("disables-mbeans", MBEAN_MANAGEMENT_TYPE), is(false));
  assertThat(isMbeanRegistered("disables-mbeans", MBEAN_STATISTICS_TYPE), is(false));
}
 
Example 3
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void basicJsr107StillWorks() throws Exception {
  CacheManager cacheManager = provider.getCacheManager();

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(true);
  configuration.setStatisticsEnabled(true);

  Cache<Long, String> cache = cacheManager.createCache("cache", configuration);
  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);

  assertThat(eh107Configuration.isManagementEnabled(), is(true));
  assertThat(eh107Configuration.isStatisticsEnabled(), is(true));

  assertThat(isMbeanRegistered("cache", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("cache", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 4
Source File: CacheManagerManagementTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void testCacheStatisticsOffThenOnThenOff() throws Exception {
  MutableConfiguration configuration = new MutableConfiguration();
  configuration.setStatisticsEnabled(false);
  cacheManager.createCache("cache1", configuration);
  cacheManager.createCache("cache2", configuration);
  Set<? extends ObjectName> names = mBeanServer.queryNames(new ObjectName("javax.cache:*"), null);
  Assert.assertTrue(names.size() == 0);

  configuration.setStatisticsEnabled(true);
  cacheManager.createCache("cache3", configuration);
  cacheManager.createCache("cache4", configuration);
  assertThat(mBeanServer.queryNames(new ObjectName("javax.cache:*"), null), hasSize(2));

  cacheManager.enableStatistics("cache3", false);
  assertThat(mBeanServer.queryNames(new ObjectName("javax.cache:*"), null), hasSize(1));

  cacheManager.enableStatistics("cache3", true);
  assertThat(mBeanServer.queryNames(new ObjectName("javax.cache:*"), null), hasSize(2));
}
 
Example 5
Source File: CacheMBStatisticsBeanTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@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");
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));

    Map<Long, String> map = new HashMap<Long, String>();
    map.put(2L, "goodbye");
    map.put(3L, "world");
    cache.putAll(map);
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));
}
 
Example 6
Source File: CachingPrincipalAttributesRepository.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
/**
 * Prep cache configuration.
 *
 * @param expiryDuration the expiry duration
 * @return the mutable configuration
 */
protected static MutableConfiguration<String, Map<String, Object>> createCacheConfiguration(final Duration expiryDuration) {
    final MutableConfiguration<String, Map<String, Object>> config = new MutableConfiguration<>();
    config.setStatisticsEnabled(true);
    config.setManagementEnabled(true);
    config.setStoreByValue(true);
    config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(expiryDuration));
    return config;
}
 
Example 7
Source File: TestCollector.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/** Test.
 * @throws SchedulerException e */
@Test
public void testToString() throws SchedulerException {
	final Collector collector = createCollectorWithOneCounter();
	assertToStringNotEmpty("collector", collector);
	assertToStringNotEmpty("java", new JavaInformations(null, false));
	assertToStringNotEmpty("thread",
			new ThreadInformations(Thread.currentThread(),
					Arrays.asList(Thread.currentThread().getStackTrace()), 100, 1000, false,
					Parameters.getHostAddress()));
	assertToStringNotEmpty("session", new SessionInformations(new SessionTestImpl(true), true));
	assertToStringNotEmpty("memory", new MemoryInformations());
	CacheManager.getInstance().addCache("testToString");
	try {
		assertToStringNotEmpty("cache", new CacheInformations(
				CacheManager.getInstance().getEhcache("testToString"), false));
	} finally {
		CacheManager.getInstance().shutdown();
	}
	final MutableConfiguration<Object, Object> conf = new MutableConfiguration<Object, Object>();
	conf.setManagementEnabled(true);
	conf.setStatisticsEnabled(true);
	Caching.getCachingProvider().getCacheManager().createCache("cache", conf);
	try {
		assertToStringNotEmpty("cache", new JCacheInformations("cache"));
	} finally {
		Caching.getCachingProvider().getCacheManager().close();
	}
	final Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
	final JobDetail job = new JobDetail("job", null, JobTestImpl.class);
	assertToStringNotEmpty("job", new JobInformations(job, null, scheduler));
	assertToStringNotEmpty("connectionInfos", new ConnectionInformations());
}
 
Example 8
Source File: OpenJPAJCacheDataCacheManager.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
Cache<Object, Object> getOrCreateCache(final String prefix, final String entity)
{
    final String internalName = prefix + entity;
    Cache<Object, Object> cache = cacheManager.getCache(internalName);
    if (cache == null)
    {
        final Properties properties = cacheManager.getProperties();
        final MutableConfiguration<Object, Object> configuration = new MutableConfiguration<Object, Object>()
                .setStoreByValue("true".equalsIgnoreCase(properties.getProperty("jcache.store-by-value", "false")));

        configuration.setReadThrough("true".equals(properties.getProperty("jcache.read-through", "false")));
        configuration.setWriteThrough("true".equals(properties.getProperty("jcache.write-through", "false")));
        if (configuration.isReadThrough())
        {
            configuration.setCacheLoaderFactory(new FactoryBuilder.ClassFactory<CacheLoader<Object, Object>>(properties.getProperty("jcache.cache-loader-factory")));
        }
        if (configuration.isWriteThrough())
        {
            configuration.setCacheWriterFactory(new FactoryBuilder.ClassFactory<CacheWriter<Object, Object>>(properties.getProperty("jcache.cache-writer-factory")));
        }
        final String expirtyPolicy = properties.getProperty("jcache.expiry-policy-factory");
        if (expirtyPolicy != null)
        {
            configuration.setExpiryPolicyFactory(new FactoryBuilder.ClassFactory<ExpiryPolicy>(expirtyPolicy));
        }
        else
        {
            configuration.setExpiryPolicyFactory(new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new CreatedExpiryPolicy(Duration.FIVE_MINUTES)));
        }
        configuration.setManagementEnabled("true".equals(properties.getProperty("jcache.management-enabled", "false")));
        configuration.setStatisticsEnabled("true".equals(properties.getProperty("jcache.statistics-enabled", "false")));

        cache = cacheManager.createCache(internalName, configuration);
    }
    return cache;
}
 
Example 9
Source File: JCacheAdapter.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <C extends Configuration<K, V>> C getConfiguration(Class<C> _class) {
  if (CompleteConfiguration.class.isAssignableFrom(_class)) {
    MutableConfiguration<K, V> cfg = new MutableConfiguration<K, V>();
    cfg.setTypes(keyType, valueType);
    cfg.setStatisticsEnabled(jmxStatisticsEnabled);
    cfg.setManagementEnabled(jmxEnabled);
    cfg.setStoreByValue(storeByValue);
    Collection<CacheEntryListenerConfiguration<K,V>> _listenerConfigurations = eventHandling.getAllListenerConfigurations();
    for (CacheEntryListenerConfiguration<K,V> _listenerConfig : _listenerConfigurations) {
      cfg.addCacheEntryListenerConfiguration(_listenerConfig);
    }
    return (C) cfg;
  }
  return (C) new Configuration<K, V>() {
    @Override
    public Class<K> getKeyType() {
      return keyType;
    }

    @Override
    public Class<V> getValueType() {
      return valueType;
    }

    @Override
    public boolean isStoreByValue() {
      return storeByValue;
    }
  };
}
 
Example 10
Source File: TestMonitoringFilter.java    From javamelody with Apache License 2.0 4 votes vote down vote up
/** Test.
 * @throws ServletException e
 * @throws IOException e */
@Test
public void testDoMonitoringWithParts() throws ServletException, IOException {
	final Map<HttpParameter, String> parameters = new HashMap<HttpParameter, String>();

	parameters.put(HttpParameter.PART, HttpPart.CURRENT_REQUESTS.getName());
	monitoring(parameters);
	parameters.put(HttpParameter.PART, HttpPart.THREADS.getName());
	monitoring(parameters);
	parameters.put(HttpParameter.PART, HttpPart.THREADS_DUMP.getName());
	monitoring(parameters);
	final File hsErrPidFile = new File("./hs_err_pid12345.log");
	try {
		hsErrPidFile.createNewFile();
		parameters.put(HttpParameter.PART, HttpPart.CRASHES.getName());
		monitoring(parameters, false);
		parameters.put(HttpParameter.PATH, hsErrPidFile.getAbsolutePath().replace('\\', '/'));
		monitoring(parameters, false);
		parameters.put(HttpParameter.PATH, "unknown");
		monitoring(parameters, false);
		parameters.remove(HttpParameter.PATH);
	} finally {
		hsErrPidFile.delete();
	}
	parameters.put(HttpParameter.PART, HttpPart.CACHE_KEYS.getName());
	final String cacheName = getClass().getName();
	CacheManager.getInstance().addCache(cacheName);
	parameters.put(HttpParameter.CACHE_ID, cacheName);
	monitoring(parameters);
	CacheManager.getInstance().getCache(cacheName).put(new Element("1", "value"));
	monitoring(parameters);
	parameters.put(HttpParameter.FORMAT, "htmlbody");
	monitoring(parameters);
	setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, "false");
	monitoring(parameters);
	CacheManager.getInstance().removeCache(cacheName);
	parameters.remove(HttpParameter.CACHE_ID);
	parameters.remove(HttpParameter.FORMAT);
	setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, TRUE);

	parameters.put(HttpParameter.PART, HttpPart.JCACHE_KEYS.getName());
	final MutableConfiguration<Object, Object> conf = new MutableConfiguration<Object, Object>();
	conf.setManagementEnabled(true);
	conf.setStatisticsEnabled(true);
	Caching.getCachingProvider().getCacheManager().createCache(cacheName, conf);
	Caching.getCachingProvider().getCacheManager().createCache(cacheName + "2", conf);
	parameters.put(HttpParameter.CACHE_ID, cacheName);
	monitoring(parameters);
	Caching.getCachingProvider().getCacheManager().getCache(cacheName).put("1", "value");
	monitoring(parameters);
	parameters.put(HttpParameter.FORMAT, "htmlbody");
	monitoring(parameters);
	setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, "false");
	monitoring(parameters);
	Caching.getCachingProvider().getCacheManager().destroyCache(cacheName);
	parameters.remove(HttpParameter.CACHE_ID);
	parameters.remove(HttpParameter.FORMAT);
	setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, TRUE);

	parameters.put(HttpParameter.PART, HttpPart.JNLP.getName());
	monitoring(parameters);
	setProperty(Parameter.JAVAMELODY_SWING_URL, "http://dummy");
	monitoring(parameters);
	parameters.put(HttpParameter.PART, HttpPart.DEPENDENCIES.getName());
	monitoring(parameters);
	parameters.put(HttpParameter.PART, HttpPart.COUNTER_SUMMARY_PER_CLASS.getName());
	parameters.put(HttpParameter.COUNTER, "services");
	monitoring(parameters);
	parameters.put(HttpParameter.GRAPH, "unknown service");
	monitoring(parameters);
	parameters.remove(HttpParameter.COUNTER);

	doMonitoringWithGraphPart();

	doMonitoringWithSourcePart();

	doMonitoringWithUnknownPart();
}
 
Example 11
Source File: TestAction.java    From javamelody with Apache License 2.0 4 votes vote down vote up
private void caches(final Collector collector, final String counterName, final String sessionId,
		final String threadId, final String jobId, final String cacheId) throws IOException {
	if (CacheManager.getInstance().getCache(cacheId) == null) {
		CacheManager.getInstance().addCache(cacheId);
	}
	assertNotNull("message CLEAR_CACHES", Action.CLEAR_CACHES.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_CACHE.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_CACHE.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, "inconnu"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_CACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "inconnue"));
	CacheManager.getInstance().getCache(cacheId).put(new Element("1", "value"));
	CacheManager.getInstance().getCache(cacheId).put(new Element("2", "value"));
	CacheManager.getInstance().getCache(cacheId).put(new Element(Integer.valueOf(3), "value"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_CACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, "inconnu", "inconnue"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_CACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "inconnue"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_CACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "1"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_CACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "3"));
	CacheManager.getInstance().removeCache(cacheId);

	final javax.cache.CacheManager jcacheManager = Caching.getCachingProvider()
			.getCacheManager();
	if (jcacheManager.getCache(cacheId) == null) {
		final MutableConfiguration<Object, Object> conf = new MutableConfiguration<Object, Object>();
		conf.setManagementEnabled(true);
		conf.setStatisticsEnabled(true);
		jcacheManager.createCache(cacheId, conf);
	}
	assertNotNull("message CLEAR_CACHES", Action.CLEAR_JCACHES.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_JCACHE.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_JCACHE.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, "inconnu"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_JCACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, "inconnu", "inconnue"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_JCACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "inconnue"));
	jcacheManager.getCache(cacheId).put("1", "value");
	jcacheManager.getCache(cacheId).put("2", "value");
	jcacheManager.getCache(cacheId).put(Integer.valueOf(3), "value");
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_JCACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "inconnue"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_JCACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "1"));
	assertNotNull("message CLEAR_CACHE", Action.CLEAR_JCACHE_KEY.execute(collector, null, null,
			counterName, sessionId, threadId, jobId, cacheId, "3"));
	jcacheManager.destroyCache(cacheId);
}
 
Example 12
Source File: JCacheFilter.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public void init(final FilterConfig filterConfig) throws ServletException
{
    final ClassLoader classLoader = filterConfig.getServletContext().getClassLoader();
    provider = Caching.getCachingProvider(classLoader);

    String uri = filterConfig.getInitParameter("configuration");
    if (uri == null)
    {
        uri = provider.getDefaultURI().toString();
    }
    final Properties properties = new Properties();
    for (final String key : list(filterConfig.getInitParameterNames()))
    {
        final String value = filterConfig.getInitParameter(key);
        if (value != null)
        {
            properties.put(key, value);
        }
    }
    manager = provider.getCacheManager(URI.create(uri), classLoader, properties);

    String cacheName = filterConfig.getInitParameter("cache-name");
    if (cacheName == null)
    {
        cacheName = JCacheFilter.class.getName();
    }
    cache = manager.getCache(cacheName);
    if (cache == null)
    {
        final MutableConfiguration<PageKey, Page> configuration = new MutableConfiguration<PageKey, Page>()
                .setStoreByValue(false);
        configuration.setReadThrough("true".equals(properties.getProperty("read-through", "false")));
        configuration.setWriteThrough("true".equals(properties.getProperty("write-through", "false")));
        if (configuration.isReadThrough())
        {
            configuration.setCacheLoaderFactory(new FactoryBuilder.ClassFactory<CacheLoader<PageKey, Page>>(properties.getProperty("cache-loader-factory")));
        }
        if (configuration.isWriteThrough())
        {
            configuration.setCacheWriterFactory(new FactoryBuilder.ClassFactory<CacheWriter<? super PageKey, ? super Page>>(properties.getProperty("cache-writer-factory")));
        }
        final String expirtyPolicy = properties.getProperty("expiry-policy-factory");
        if (expirtyPolicy != null)
        {
            configuration.setExpiryPolicyFactory(new FactoryBuilder.ClassFactory<ExpiryPolicy>(expirtyPolicy));
        }
        configuration.setManagementEnabled("true".equals(properties.getProperty("management-enabled", "false")));
        configuration.setStatisticsEnabled("true".equals(properties.getProperty("statistics-enabled", "false")));
        cache = manager.createCache(cacheName, configuration);
    }
}
 
Example 13
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that a cache using a {@link javax.cache.expiry.ExpiryPolicy} configured to
 * return a {@link Duration#ZERO} for newly created entries will immediately
 * expire the entries.
 */
private void expire_whenCreated(Factory<? extends ExpiryPolicy> expiryPolicyFactory) {
  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<Integer, Integer>();
  config.setTypes(Integer.class, Integer.class);
  config.setExpiryPolicyFactory(expiryPolicyFactory);
  config = extraSetup(config);
  config.setStatisticsEnabled(true);

  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  cache.put(1, 1);


  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));
  assertEquals(0, listener.getCreated());

  cache.put(1, 1);

  assertFalse(cache.remove(1));

  cache.put(1, 1);

  assertFalse(cache.remove(1, 1));

  cache.getAndPut(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.putIfAbsent(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  map.put(1, 1);
  cache.putAll(map);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.put(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.iterator().hasNext());

  cache.getAndPut(1, 1);
  assertEquals(0, listener.getCreated());
}