Search in sources :

Example 1 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project redisson by redisson.

the class JCacheTest method testExpiration.

@Test
public void testExpiration() throws InterruptedException, IllegalArgumentException, URISyntaxException, FailedToStartRedisException, IOException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)));
    config.setStoreByValue(true);
    URI configUri = getClass().getResource("redisson-jcache.json").toURI();
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager(configUri, null).createCache("test", config);
    CountDownLatch latch = new CountDownLatch(1);
    String key = "123";
    ExpiredListener clientListener = new ExpiredListener(latch, key, "90");
    MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(clientListener), null, true, true);
    cache.registerCacheEntryListener(listenerConfiguration);
    cache.put(key, "90");
    Assert.assertNotNull(cache.get(key));
    latch.await();
    Assert.assertNull(cache.get(key));
    cache.close();
    runner.stop();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) Duration(javax.cache.expiry.Duration) RedisRunner(org.redisson.RedisRunner) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) MutableConfiguration(javax.cache.configuration.MutableConfiguration) BaseTest(org.redisson.BaseTest) Test(org.junit.Test)

Example 2 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project hazelcast by hazelcast.

the class CacheContextTest method cacheEntryListenerCountIncreasedAndDecreasedCorrectly.

protected void cacheEntryListenerCountIncreasedAndDecreasedCorrectly(DecreaseType decreaseType) {
    final String CACHE_NAME = "MyCache";
    final String CACHE_NAME_WITH_PREFIX = "/hz/" + CACHE_NAME;
    CachingProvider provider = initAndGetCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();
    CacheEntryListenerConfiguration<String, String> cacheEntryListenerConfig = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(new TestListener()), null, true, true);
    CompleteConfiguration<String, String> cacheConfig = new MutableConfiguration<String, String>();
    Cache<String, String> cache = cacheManager.createCache(CACHE_NAME, cacheConfig);
    cache.registerCacheEntryListener(cacheEntryListenerConfig);
    final CacheService cacheService1 = getCacheService(hazelcastInstance1);
    final CacheService cacheService2 = getCacheService(hazelcastInstance2);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertNotNull(cacheService1.getCacheContext(CACHE_NAME_WITH_PREFIX));
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertNotNull(cacheService2.getCacheContext(CACHE_NAME_WITH_PREFIX));
        }
    });
    final CacheContext cacheContext1 = cacheService1.getCacheContext(CACHE_NAME_WITH_PREFIX);
    final CacheContext cacheContext2 = cacheService2.getCacheContext(CACHE_NAME_WITH_PREFIX);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, cacheContext1.getCacheEntryListenerCount());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, cacheContext2.getCacheEntryListenerCount());
        }
    });
    switch(decreaseType) {
        case DEREGISTER:
            cache.deregisterCacheEntryListener(cacheEntryListenerConfig);
            break;
        case SHUTDOWN:
            driverInstance.getLifecycleService().shutdown();
            break;
        case TERMINATE:
            driverInstance.getLifecycleService().terminate();
            break;
        default:
            throw new IllegalArgumentException("Unsupported decrease type: " + decreaseType);
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(0, cacheContext1.getCacheEntryListenerCount());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(0, cacheContext2.getCacheEntryListenerCount());
        }
    });
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheContext(com.hazelcast.cache.impl.CacheContext) MutableConfiguration(javax.cache.configuration.MutableConfiguration) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) CacheService(com.hazelcast.cache.impl.CacheService)

Example 3 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project hazelcast by hazelcast.

the class CacheResourceTest method testCloseableCacheListener.

@Test
public void testCloseableCacheListener() {
    CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(factory.newHazelcastInstance());
    CacheManager cacheManager = provider.getCacheManager();
    CloseableListener listener = new CloseableListener();
    Factory<CloseableListener> listenerFactory = FactoryBuilder.factoryOf(listener);
    CompleteConfiguration<Object, Object> configuration = new CacheConfig().addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration(listenerFactory, null, true, false));
    Cache<Object, Object> cache = cacheManager.createCache("test", configuration);
    cache.close();
    assertTrue("CloseableListener.close() should be called when cache is closed!", listener.closed);
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project hazelcast by hazelcast.

the class BasicCacheLiteMemberTest method testCompletion.

@Test
public void testCompletion() throws InterruptedException {
    CacheManager cacheManager = liteCachingProvider.getCacheManager();
    CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    final SimpleEntryListener<Integer, String> listener = new SimpleEntryListener<Integer, String>();
    MutableCacheEntryListenerConfiguration<Integer, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<Integer, String>(FactoryBuilder.factoryOf(listener), null, true, true);
    config.addCacheEntryListenerConfiguration(listenerConfiguration);
    final Cache<Integer, String> instanceCache = cacheManager.createCache(cacheName, config);
    Integer key1 = 1;
    String value1 = "value1";
    instanceCache.put(key1, value1);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.created.get());
        }
    });
    Integer key2 = 2;
    String value2 = "value2";
    instanceCache.put(key2, value2);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.created.get());
        }
    });
    instanceCache.remove(key1);
    instanceCache.remove(key2);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, listener.removed.get());
        }
    });
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) CacheConfig(com.hazelcast.config.CacheConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with MutableCacheEntryListenerConfiguration

use of javax.cache.configuration.MutableCacheEntryListenerConfiguration in project gora by apache.

the class JCacheStore method initialize.

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) {
    super.initialize(keyClass, persistentClass, properties);
    CachingProvider cachingProvider = Caching.getCachingProvider(properties.getProperty(GORA_DEFAULT_JCACHE_PROVIDER_KEY));
    if (properties.getProperty(JCACHE_CACHE_NAMESPACE_PROPERTY_KEY) != null) {
        goraCacheNamespace = properties.getProperty(JCACHE_CACHE_NAMESPACE_PROPERTY_KEY);
    }
    try {
        this.persistentDataStore = DataStoreFactory.getDataStore(keyClass, persistentClass, new Configuration());
    } catch (GoraException ex) {
        LOG.error("Couldn't initialize persistent DataStore.", ex);
    }
    if (properties.getProperty(GORA_DEFAULT_JCACHE_PROVIDER_KEY).contains(HAZELCAST_SERVER_CACHE_PROVIDER_IDENTIFIER)) {
        Config config = new ClasspathXmlConfig(properties.getProperty(GORA_DEFAULT_JCACHE_HAZELCAST_CONFIG_KEY));
        hazelcastInstance = Hazelcast.newHazelcastInstance(config);
    } else {
        try {
            ClientConfig config = new XmlClientConfigBuilder(properties.getProperty(GORA_DEFAULT_JCACHE_HAZELCAST_CONFIG_KEY)).build();
            hazelcastInstance = HazelcastClient.newHazelcastClient(config);
        } catch (IOException ex) {
            LOG.error("Couldn't locate the client side cache provider configuration.", ex);
        }
    }
    Properties providerProperties = new Properties();
    providerProperties.setProperty(HazelcastCachingProvider.HAZELCAST_INSTANCE_NAME, hazelcastInstance.getName());
    try {
        manager = cachingProvider.getCacheManager(new URI(goraCacheNamespace), null, providerProperties);
    } catch (URISyntaxException ex) {
        LOG.error("Couldn't initialize cache manager to bounded hazelcast instance.", ex);
        manager = cachingProvider.getCacheManager();
    }
    if (((properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY) != null) && Boolean.valueOf(properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY))) || ((manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass) == null))) {
        cacheEntryList = new ConcurrentSkipListSet<>();
        cacheConfig = new CacheConfig<K, T>();
        cacheConfig.setTypes(keyClass, persistentClass);
        if (properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY) != null) {
            cacheConfig.setReadThrough(Boolean.valueOf(properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY)));
        } else {
            cacheConfig.setReadThrough(true);
        }
        if (properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY) != null) {
            cacheConfig.setWriteThrough(Boolean.valueOf(properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY)));
        } else {
            cacheConfig.setWriteThrough(true);
        }
        if (properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY) != null) {
            cacheConfig.setStoreByValue(Boolean.valueOf(properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY) != null) {
            cacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY) != null) {
            cacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_POLICY_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.valueOf(properties.getProperty(JCACHE_EVICTION_POLICY_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_MAX_SIZE_POLICY_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.valueOf(properties.getProperty(JCACHE_EVICTION_MAX_SIZE_POLICY_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EVICTION_SIZE_PROPERTY_KEY) != null) {
            cacheConfig.getEvictionConfig().setSize(Integer.valueOf(properties.getProperty(JCACHE_EVICTION_SIZE_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY) != null) {
            String expiryPolicyIdentifier = properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY);
            if (expiryPolicyIdentifier.equals(JCACHE_ACCESSED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_CREATED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_MODIFIED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ModifiedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_TOUCHED_EXPIRY_IDENTIFIER)) {
                cacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new TouchedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            }
        }
        if (properties.getProperty(HAZELCAST_CACHE_IN_MEMORY_FORMAT_PROPERTY_KEY) != null) {
            String inMemoryFormat = properties.getProperty(HAZELCAST_CACHE_IN_MEMORY_FORMAT_PROPERTY_KEY);
            if (inMemoryFormat.equals(HAZELCAST_CACHE_BINARY_IN_MEMORY_FORMAT_IDENTIFIER) || inMemoryFormat.equals(HAZELCAST_CACHE_OBJECT_IN_MEMORY_FORMAT_IDENTIFIER) || inMemoryFormat.equals(HAZELCAST_CACHE_NATIVE_IN_MEMORY_FORMAT_IDENTIFIER)) {
                cacheConfig.setInMemoryFormat(InMemoryFormat.valueOf(inMemoryFormat));
            }
        }
        cacheConfig.setCacheLoaderFactory(JCacheCacheFactoryBuilder.factoryOfCacheLoader(this.persistentDataStore, keyClass, persistentClass));
        cacheConfig.setCacheWriterFactory(JCacheCacheFactoryBuilder.factoryOfCacheWriter(this.persistentDataStore, keyClass, persistentClass));
        cache = manager.createCache(persistentClass.getSimpleName(), cacheConfig).unwrap(ICache.class);
    } else {
        cache = manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass).unwrap(ICache.class);
        this.populateLocalCacheEntrySet(cache);
    }
    cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(JCacheCacheFactoryBuilder.factoryOfEntryListener(new JCacheCacheEntryListener<K, T>(cacheEntryList)), null, true, true));
    LOG.info("JCache Gora datastore initialized successfully.");
}
Also used : CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) Configuration(org.apache.hadoop.conf.Configuration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheConfig(com.hazelcast.config.CacheConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) ClasspathXmlConfig(com.hazelcast.config.ClasspathXmlConfig) ClasspathXmlConfig(com.hazelcast.config.ClasspathXmlConfig) Duration(javax.cache.expiry.Duration) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Properties(java.util.Properties) URI(java.net.URI) GoraException(org.apache.gora.util.GoraException) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) ICache(com.hazelcast.cache.ICache) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastCachingProvider(com.hazelcast.cache.HazelcastCachingProvider) CachingProvider(javax.cache.spi.CachingProvider)

Aggregations

MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)15 Test (org.junit.Test)7 AssertTask (com.hazelcast.test.AssertTask)5 CacheConfig (com.hazelcast.config.CacheConfig)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 CacheManager (javax.cache.CacheManager)4 Duration (javax.cache.expiry.Duration)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CacheEntryListener (javax.cache.event.CacheEntryListener)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 ModifiedExpiryPolicy (javax.cache.expiry.ModifiedExpiryPolicy)3 CachingProvider (javax.cache.spi.CachingProvider)3 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)2 URI (java.net.URI)2 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2