Java Code Examples for javax.cache.spi.CachingProvider#close()

The following examples show how to use javax.cache.spi.CachingProvider#close() . 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: NotSerializableTest.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
@Test
public void run()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache("default", new MutableConfiguration<String, NotSerializableAndImHappyWithIt>().setStoreByValue(false));
    final Cache<String, NotSerializableAndImHappyWithIt> cache = cacheManager.getCache("default");
    assertFalse(cache.containsKey("foo"));
    cache.put("foo", new NotSerializableAndImHappyWithIt("bar"));
    assertTrue(cache.containsKey("foo"));
    assertEquals("bar", cache.get("foo").name);
    cache.remove("foo");
    assertFalse(cache.containsKey("foo"));
    cache.close();
    cacheManager.close();
    cachingProvider.close();
}
 
Example 2
Source File: CachingProviderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void closeCacheManagerByURIAndClassLoader() {
  CachingProvider provider = Caching.getCachingProvider();

  ClassLoader loader1 = CLASS_LOADER;
  CacheManager manager1 = provider.getCacheManager(provider.getDefaultURI(), loader1, null);

  ClassLoader loader2 = new MyClassLoader(CLASS_LOADER);
  CacheManager manager2 = provider.getCacheManager(provider.getDefaultURI(), loader2, null);

  provider.close(manager1.getURI(), loader1);
  provider.close(manager2.getURI(), loader2);

  assertNotSame(manager1, provider.getCacheManager(provider.getDefaultURI(), loader1, null));
  assertNotSame(manager2, provider.getCacheManager(provider.getDefaultURI(), loader2, null));
}
 
Example 3
Source File: CachingProviderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void closeCacheManagersByClassLoader() {
  CachingProvider provider = Caching.getCachingProvider();

  ClassLoader loader1 = CLASS_LOADER;
  CacheManager manager1 = provider.getCacheManager(provider.getDefaultURI(), loader1, null);

  ClassLoader loader2 = new MyClassLoader(CLASS_LOADER);
  CacheManager manager2 = provider.getCacheManager(provider.getDefaultURI(), loader2, null);

  provider.close(loader1);
  provider.close(loader2);

  assertNotSame(manager1, provider.getCacheManager(provider.getDefaultURI(), loader1, null));
  assertNotSame(manager2, provider.getCacheManager(provider.getDefaultURI(), loader2, null));
}
 
Example 4
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Close all CacheManagers from a CachingProvider, each CacheManager being
 * based on a different ClassLoader.
 */
@Test
public void closeAllCacheManagers() throws Exception {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  URI uri = provider.getDefaultURI();

  ClassLoader loader1 = new MyClassLoader(contextLoader);
  CacheManager manager1 = provider.getCacheManager(uri, loader1);

  ClassLoader loader2 = new MyClassLoader(contextLoader);
  CacheManager manager2 = provider.getCacheManager(uri, loader2);

  ClassLoader loader3 = new MyClassLoader(contextLoader);
  CacheManager manager3 = provider.getCacheManager(uri, loader3);

  provider.close();

  assertNotSame(manager1, provider.getCacheManager(uri, loader1));
  assertNotSame(manager2, provider.getCacheManager(uri, loader2));
  assertNotSame(manager3, provider.getCacheManager(uri, loader3));
}
 
Example 5
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Closing a single CacheManager from a CachingProvider when there are
 * multiple available across different ClassLoaders.
 */
@Test
public void closeCacheManager() throws Exception {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  URI uri = provider.getDefaultURI();

  ClassLoader loader1 = new MyClassLoader(contextLoader);
  CacheManager manager1 = provider.getCacheManager(uri, loader1);

  ClassLoader loader2 = new MyClassLoader(contextLoader);
  CacheManager manager2 = provider.getCacheManager(uri, loader2);

  ClassLoader loader3 = new MyClassLoader(contextLoader);
  CacheManager manager3 = provider.getCacheManager(uri, loader3);

  provider.close(manager2.getURI(), loader2);

  assertSame(manager1, provider.getCacheManager(uri, loader1));
  assertNotSame(manager2, provider.getCacheManager(uri, loader2));
  assertSame(manager3, provider.getCacheManager(uri, loader3));
}
 
Example 6
Source File: CacheTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void accessExpiry() throws InterruptedException
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(),
            Thread.currentThread().getContextClassLoader(),
            cachingProvider.getDefaultProperties());
    final Cache<Integer, Integer> cache = cacheManager.createCache(
            "test",
            new MutableConfiguration<Integer, Integer>()
                    .setStoreByValue(false)
                    .setStatisticsEnabled(true)
                    .setManagementEnabled(true)
                    .setTypes(Integer.class, Integer.class)
                    .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, 500))));

    try {
        cache.put(1, 2);
        cache.get(1);
        Thread.sleep(650);
        assertFalse(cache.containsKey(1));
        cache.put(1, 2);
        for (int i = 0; i < 3; i++) { // we update the last access to force the idle time and lastaccess to be synced
            Thread.sleep(250);
            assertTrue("iteration: " + Integer.toString(i), cache.containsKey(1));
        }
        assertTrue(cache.containsKey(1));
        Thread.sleep(650);
        assertFalse(cache.containsKey(1));
    } finally {
        cacheManager.close();
        cachingProvider.close();
    }
}
 
Example 7
Source File: CacheTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void getPut()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final Cache<String, String> cache = cacheManager.createCache("default", new MutableConfiguration<String, String>());
    assertFalse(cache.containsKey("foo"));
    cache.put("foo", "bar");
    assertTrue(cache.containsKey("foo"));
    assertEquals("bar", cache.get("foo"));
    cache.remove("foo");
    assertFalse(cache.containsKey("foo"));
    cachingProvider.close();
}
 
Example 8
Source File: CachingProviderTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void createCacheMgr()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    assertNotNull(cachingProvider.getCacheManager());
    cachingProvider.close();
}
 
Example 9
Source File: ImmediateExpiryTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void immediate()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache("default",
            new MutableConfiguration<>()
                    .setExpiryPolicyFactory(
                            new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new CreatedExpiryPolicy(Duration.ZERO))));
    final Cache<String, String> cache = cacheManager.getCache("default");
    assertFalse(cache.containsKey("foo"));
    cache.put("foo", "bar");
    assertFalse(cache.containsKey("foo"));
    cachingProvider.close();
}
 
Example 10
Source File: CachingProviderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void closeCachingProvider() {
  CachingProvider provider = Caching.getCachingProvider();

  ClassLoader loader1 = CLASS_LOADER;
  CacheManager manager1 = provider.getCacheManager(provider.getDefaultURI(), loader1, null);

  ClassLoader loader2 = new MyClassLoader(CLASS_LOADER);
  CacheManager manager2 = provider.getCacheManager(provider.getDefaultURI(), loader2, null);

  provider.close();

  assertNotSame(manager1, provider.getCacheManager(provider.getDefaultURI(), loader1, null));
  assertNotSame(manager2, provider.getCacheManager(provider.getDefaultURI(), loader2, null));
}
 
Example 11
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to close CacheManagers using URIs and/or ClassLoaders that don't
 * have associated CacheManagers.
 */
@Test
public void closeClassLoader() throws Exception {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  URI uri = provider.getDefaultURI();

  ClassLoader loader1 = new MyClassLoader(contextLoader);
  CacheManager manager1 = provider.getCacheManager(uri, loader1);

  ClassLoader loader2 = new MyClassLoader(contextLoader);
  CacheManager manager2 = provider.getCacheManager(uri, loader2);

  ClassLoader loader3 = new MyClassLoader(contextLoader);
  CacheManager manager3 = provider.getCacheManager(uri, loader3);

  provider.close(contextLoader);
  provider.close(provider.getDefaultURI(), contextLoader);
  provider.close(provider.getDefaultURI(), contextLoader);
  provider.close(provider.getDefaultURI(), contextLoader);

  assertSame(manager1, provider.getCacheManager(uri, loader1));
  assertSame(manager2, provider.getCacheManager(uri, loader2));
  assertSame(manager3, provider.getCacheManager(uri, loader3));
}
 
Example 12
Source File: JCacheSetup.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
public void releaseCachingProvider(@Disposes final CachingProvider provider) {
    provider.close(); // will close manager as well
}
 
Example 13
Source File: InternalCacheRule.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description)
{
    return new Statement()
    {
        @Override
        public void evaluate() throws Throwable
        {
            final CachingProvider provider = Caching.getCachingProvider();
            final CacheManager manager = provider.getCacheManager();
            try
            {
                Field cache = null;
                CompleteConfiguration<?, ?> config = null;
                for (final Field f : test.getClass().getDeclaredFields())
                {
                    if (Cache.class.isAssignableFrom(f.getType()))
                    {
                        f.setAccessible(true);
                        cache = f;
                    }
                    else if (Configuration.class.isAssignableFrom(f.getType()))
                    {
                        f.setAccessible(true);
                        config = (CompleteConfiguration<?, ?>) f.get(test);
                    }
                }
                if (cache != null)
                {
                    if (config == null)
                    {
                        throw new IllegalStateException("Define a Configuration field");
                    }
                    cache.set(test, manager.createCache(cache.getName(), config));
                }
                base.evaluate();
            }
            finally
            {
                manager.close();
                provider.close();
            }
        }
    };
}
 
Example 14
Source File: CacheTest.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Test
public void loader()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache("default", new CompleteConfiguration<Object, Object>()
    {
        /**
         * 
         */
        private static final long serialVersionUID = -4598329777808827966L;

        @Override
        public boolean isReadThrough()
        {
            return true;
        }

        @Override
        public boolean isWriteThrough()
        {
            return false;
        }

        @Override
        public boolean isStatisticsEnabled()
        {
            return false;
        }

        @Override
        public boolean isManagementEnabled()
        {
            return false;
        }

        @Override
        public Iterable<CacheEntryListenerConfiguration<Object, Object>> getCacheEntryListenerConfigurations()
        {
            return null;
        }

        @Override
        public Factory<CacheLoader<Object, Object>> getCacheLoaderFactory()
        {
            return () -> new CacheLoader<Object, Object>()
            {
                @Override
                public Object load(Object key) throws CacheLoaderException
                {
                    return "super";
                }

                @Override
                public Map<Object, Object> loadAll(Iterable<?> keys) throws CacheLoaderException
                {
                    return null;
                }
            };
        }

        @Override
        public Factory<CacheWriter<? super Object, ? super Object>> getCacheWriterFactory()
        {
            return null;
        }

        @Override
        public Factory<ExpiryPolicy> getExpiryPolicyFactory()
        {
            return null;
        }

        @Override
        public Class<Object> getKeyType()
        {
            return Object.class;
        }

        @Override
        public Class<Object> getValueType()
        {
            return Object.class;
        }

        @Override
        public boolean isStoreByValue()
        {
            return false;
        }
    });
    final Cache<String, String> cache = cacheManager.getCache("default");
    assertEquals("super", cache.get("lazilyLoaded"));
    cachingProvider.close();
}