Search in sources :

Example 71 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheHazelcastInstanceAwareTest method test_inject_hazelcastInstance_to_cacheLoader_if_itIs_hazelcastInstanceAware.

@Test
public void test_inject_hazelcastInstance_to_cacheLoader_if_itIs_hazelcastInstanceAware() {
    long id1 = generateUniqueHazelcastInjectionId();
    long id2 = generateUniqueHazelcastInjectionId();
    CacheConfig<Integer, Integer> cacheConfig = createCacheConfig(CACHE_NAME);
    cacheConfig.setCacheLoaderFactory(new HazelcastInstanceAwareCacheLoaderFactory(id1, id2));
    HazelcastInstance hazelcastInstance = createInstance();
    CacheManager cacheManager = createCachingProvider(hazelcastInstance).getCacheManager();
    Cache<Integer, Integer> cache = cacheManager.createCache(CACHE_NAME, cacheConfig);
    cache.put(1, 1);
    assertEquals("Hazelcast instance has not been injected into cache loader factory!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id1));
    assertEquals("Hazelcast instance has not been injected into cache loader!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id2));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 72 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheThroughHazelcastInstanceTest method whenThereIsCacheConfigAndCreatedByCacheManager_thenReturnsSameCache.

private void whenThereIsCacheConfigAndCreatedByCacheManager_thenReturnsSameCache(boolean getCache) {
    HazelcastInstance instance = createInstance();
    CachingProvider cachingProvider = createCachingProvider(instance);
    CacheManager cacheManager = cachingProvider.getCacheManager();
    Cache cache1 = cacheManager.createCache(CACHE_NAME, createCacheConfig(CACHE_NAME));
    assertNotNull(cache1);
    Cache cache2 = retrieveCache(instance, getCache);
    assertNotNull(cache2);
    // verify that they are same cache instance
    assertTrue(cache1 == cache2);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceCacheManager(com.hazelcast.instance.HazelcastInstanceCacheManager) ICacheManager(com.hazelcast.core.ICacheManager) HazelcastCacheManager(com.hazelcast.cache.HazelcastCacheManager) CacheManager(javax.cache.CacheManager) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) HazelcastCachingProvider(com.hazelcast.cache.HazelcastCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) Cache(javax.cache.Cache) ICache(com.hazelcast.cache.ICache)

Example 73 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheStatsTest method doTestForOwnedEntryCount.

private void doTestForOwnedEntryCount(boolean useBackups, boolean triggerMigration) {
    final String cacheName = randomName();
    ICache<Integer, String> cache;
    CacheStatistics[] allStats;
    HazelcastInstance instance2 = null;
    if (useBackups) {
        // Create the second instance to store data as backup.
        instance2 = getHazelcastInstance();
        CachingProvider cp = getCachingProvider(instance2);
        CacheManager cm = cp.getCacheManager();
        cache = createCache(cacheName);
        ICache<Integer, String> c = cm.getCache(cacheName).unwrap(ICache.class);
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics(), c.getLocalCacheStatistics() };
    } else {
        cache = createCache(cacheName);
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics() };
    }
    final int ENTRY_COUNT = 100;
    for (int i = 0; i < ENTRY_COUNT; i++) {
        cache.put(i, "Value-" + i);
    }
    assertOwnedEntryCount(ENTRY_COUNT, allStats);
    if (triggerMigration && instance2 != null) {
        // Shutdown the second instance to trigger migration so first instance will be owner of all partitions.
        instance2.shutdown();
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics() };
        assertOwnedEntryCount(ENTRY_COUNT, allStats);
    }
    for (int i = 0; i < 10; i++) {
        cache.remove(i);
    }
    assertOwnedEntryCount(ENTRY_COUNT - 10, allStats);
    for (int i = 10; i < ENTRY_COUNT; i++) {
        cache.remove(i);
    }
    assertOwnedEntryCount(0, allStats);
    for (int i = 0; i < ENTRY_COUNT; i++) {
        cache.put(i, "Value-" + i);
    }
    assertOwnedEntryCount(ENTRY_COUNT, allStats);
    if (triggerMigration) {
        // Create the second instance to trigger migration
        // so the second instance will be owner of some partitions
        // and the first instance will lose ownership of some instances.
        instance2 = getHazelcastInstance();
        CachingProvider cp = getCachingProvider(instance2);
        CacheManager cm = cp.getCacheManager();
        ICache<Integer, String> c = cm.getCache(cacheName).unwrap(ICache.class);
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics(), c.getLocalCacheStatistics() };
        assertOwnedEntryCount(ENTRY_COUNT, allStats);
    }
    cache.clear();
    assertOwnedEntryCount(0, allStats);
    for (int i = 0; i < ENTRY_COUNT; i++) {
        cache.put(i, "Value-" + i);
    }
    assertOwnedEntryCount(ENTRY_COUNT, allStats);
    cache.destroy();
    assertOwnedEntryCount(0, allStats);
}
Also used : CacheStatistics(com.hazelcast.cache.CacheStatistics) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) CachingProvider(javax.cache.spi.CachingProvider)

Example 74 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class ICacheDataStructureAdapterTest method setUp.

@Before
public void setUp() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    HazelcastInstance hazelcastInstance = factory.newHazelcastInstance();
    HazelcastServerCachingProvider cachingProvider = createCachingProvider(hazelcastInstance);
    CacheManager cacheManager = cachingProvider.getCacheManager();
    CacheConfig<Integer, String> cacheConfig = new CacheConfig<Integer, String>();
    cache = (ICache<Integer, String>) cacheManager.createCache("CacheDataStructureAdapterTest", cacheConfig);
    adapter = new ICacheDataStructureAdapter<Integer, String>(cache);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheConfig(com.hazelcast.config.CacheConfig) Before(org.junit.Before)

Example 75 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class ClientManagerTest method testMultiClusterMultipleClients.

@Test
public void testMultiClusterMultipleClients() throws MalformedURLException, URISyntaxException {
    final String cacheName = "test";
    final String key1 = "key1";
    final String valuecm1 = "Value-is-cm1";
    final String valuecm2 = "Value-is-cm2";
    final HazelcastClientCachingProvider cachingProvider = new HazelcastClientCachingProvider();
    final CacheManager cm1 = cachingProvider.getCacheManager(uri1, null);
    final CacheManager cm2 = cachingProvider.getCacheManager(uri2, null);
    final CacheConfig<String, String> cacheConfig = new CacheConfig<String, String>();
    final Cache<String, String> cache1 = cm1.createCache(cacheName, cacheConfig);
    final Cache<String, String> cache2 = cm2.createCache(cacheName, cacheConfig);
    cache1.put(key1, valuecm1);
    cache2.put(key1, valuecm2);
    assertEquals(valuecm1, cache1.get(key1));
    assertEquals(valuecm2, cache2.get(key1));
    cachingProvider.close(uri1, null);
    cachingProvider.close(uri2, null);
    //        cm1.close();
    //        cm2.close();
    final CacheManager cm11 = cachingProvider.getCacheManager(uri1, null);
    final Cache<String, String> cache11 = cm11.getCache(cacheName);
    assertEquals(valuecm1, cache11.get(key1));
    cm11.close();
}
Also used : HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CacheManager (javax.cache.CacheManager)105 Test (org.junit.Test)63 QuickTest (com.hazelcast.test.annotation.QuickTest)56 CachingProvider (javax.cache.spi.CachingProvider)48 ParallelTest (com.hazelcast.test.annotation.ParallelTest)41 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)40 HazelcastInstance (com.hazelcast.core.HazelcastInstance)34 CacheConfig (com.hazelcast.config.CacheConfig)32 AssertTask (com.hazelcast.test.AssertTask)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 MutableConfiguration (javax.cache.configuration.MutableConfiguration)17 URI (java.net.URI)16 Cache (javax.cache.Cache)15 HazelcastCacheManager (com.hazelcast.cache.HazelcastCacheManager)13 HazelcastClientCachingProvider (com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)12 ICache (com.hazelcast.cache.ICache)10 Properties (java.util.Properties)10 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)9 ClientConfig (com.hazelcast.client.config.ClientConfig)8 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)7