Search in sources :

Example 26 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class IntegrationTest method ignoreMissingCacheConfiguration.

@Test
public void ignoreMissingCacheConfiguration() {
    Cache c = new Cache2kBuilder<String, String>() {
    }.manager(CacheManager.getInstance("specialCases")).name("missingConfiguration").build();
    c.close();
}
Also used : Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 27 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class IntegrationTest method typeMismatch.

@Test(expected = ConfigurationException.class)
public void typeMismatch() {
    Cache c = new Cache2kBuilder<String, String>() {
    }.manager(CacheManager.getInstance("specialCases")).name("typeMismatch").build();
    c.close();
}
Also used : Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 28 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class CacheManagerImpl method close.

/**
 * The shutdown takes place in two phases. First all caches are notified to
 * cancel their scheduled timer jobs, after that the shutdown is done. Cancelling
 * the timer jobs first is needed, because there may be cache stacking and
 * a timer job of one cache may call an already closed cache.
 *
 * <p>Rationale exception handling: Exceptions on shutdown just could silently
 * ignored, because a shutdown is done any way. Exceptions could be happened
 * long before in a parallel task, shutdown is the last point where this could
 * be propagated to the application. Silently ignoring is bad anyway, because
 * this will cause that serious problems keep undetected. The exception and
 * error handling within the cache tries everything that exceptions will be
 * routed through as early and directly as possible.
 */
@Override
public void close() {
    Iterable<Cache> _caches;
    synchronized (lock) {
        if (closing) {
            return;
        }
        _caches = cachesCopy();
        closing = true;
    }
    logPhase("close");
    List<Throwable> _suppressedExceptions = new ArrayList<Throwable>();
    for (Cache c : _caches) {
        ((InternalCache) c).cancelTimerJobs();
    }
    for (Cache c : _caches) {
        try {
            c.close();
        } catch (Throwable t) {
            _suppressedExceptions.add(t);
        }
    }
    try {
        for (CacheManagerLifeCycleListener lc : cacheManagerLifeCycleListeners) {
            lc.managerDestroyed(this);
        }
    } catch (Throwable t) {
        _suppressedExceptions.add(t);
    }
    ((Cache2kCoreProviderImpl) PROVIDER).removeManager(this);
    synchronized (lock) {
        for (Cache c : cacheNames.values()) {
            log.warn("unable to close cache: " + c.getName());
        }
    }
    eventuallyThrowException(_suppressedExceptions);
    cacheNames = null;
}
Also used : ArrayList(java.util.ArrayList) CacheManagerLifeCycleListener(org.cache2k.core.spi.CacheManagerLifeCycleListener) Cache(org.cache2k.Cache)

Example 29 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class OsgiIT method testEventPackage.

/**
 * Simple test to see whether event package is exported.
 */
@Test
public void testEventPackage() {
    CacheManager m = CacheManager.getInstance("testEventPackage");
    final AtomicInteger _count = new AtomicInteger();
    Cache<String, String> c = new Cache2kBuilder<String, String>() {
    }.manager(m).eternal(true).addListener(new CacheEntryCreatedListener<String, String>() {

        @Override
        public void onEntryCreated(final Cache<String, String> cache, final CacheEntry<String, String> entry) {
            _count.incrementAndGet();
        }
    }).build();
    c.put("abc", "123");
    assertTrue(c.containsKey("abc"));
    assertEquals("123", c.peek("abc"));
    assertEquals(1, _count.get());
    c.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryCreatedListener(org.cache2k.event.CacheEntryCreatedListener) CacheManager(org.cache2k.CacheManager) CacheEntry(org.cache2k.CacheEntry) Cache2kBuilder(org.cache2k.Cache2kBuilder) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 30 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class IllegalNamesTest method testCache.

@Test
public void testCache() {
    try {
        Cache c = Cache2kBuilder.forUnknownTypes().name(IllegalNamesTest.class.getName() + "-test-with-char-" + aChar).build();
        c.close();
        fail("expected exception for cache name with " + aChar);
    } catch (IllegalArgumentException expected) {
    /* expected */
    }
}
Also used : Cache(org.cache2k.Cache) Test(org.junit.Test)

Aggregations

Cache (org.cache2k.Cache)60 Test (org.junit.Test)49 CacheManager (org.cache2k.CacheManager)11 Cache2kBuilder (org.cache2k.Cache2kBuilder)9 CacheEntry (org.cache2k.CacheEntry)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ExceptionWrapper (org.cache2k.core.ExceptionWrapper)8 InternalCache (org.cache2k.core.InternalCache)8 CacheRule (org.cache2k.test.util.CacheRule)6 Condition (org.cache2k.test.util.Condition)6 IntCacheRule (org.cache2k.test.util.IntCacheRule)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 MBeanInfo (javax.management.MBeanInfo)3 Log (org.cache2k.core.util.Log)3 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)3 CacheEntryUpdatedListener (org.cache2k.event.CacheEntryUpdatedListener)3 ArrayList (java.util.ArrayList)2 CacheLoaderException (org.cache2k.integration.CacheLoaderException)2 Collection (java.util.Collection)1 Date (java.util.Date)1