public void cacheEntryListenerDeregistered( String name, CacheEntryListenerConfiguration cacheEntryListenerConfiguration) { CacheConfig cacheConfig = getCacheConfig(name); if (cacheConfig == null) { throw new IllegalStateException("CacheConfig does not exist for cache " + name); } cacheConfig.removeCacheEntryListenerConfiguration(cacheEntryListenerConfiguration); }
protected CacheOperationProvider getOperationProvider(String name) { ICacheService service = getService(CacheService.SERVICE_NAME); CacheConfig cacheConfig = service.getCacheConfig(name); if (cacheConfig == null) { throw new CacheNotExistsException( "Cache config for cache " + name + " has not been created yet !"); } return service.getCacheOperationProvider(name, cacheConfig.getInMemoryFormat()); }
// Issue https://github.com/hazelcast/hazelcast/issues/5865 @Test public void testCompletionTestByPuttingAndRemovingFromDifferentNodes() throws InterruptedException { String cacheName = "simpleCache"; CacheManager cacheManager1 = cachingProvider1.getCacheManager(); CacheManager cacheManager2 = cachingProvider2.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); Cache<Integer, String> cache1 = cacheManager1.createCache(cacheName, config); Cache<Integer, String> cache2 = cacheManager2.getCache(cacheName); assertNotNull(cache1); assertNotNull(cache2); Integer key1 = 1; String value1 = "value1"; cache1.put(key1, value1); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertEquals(1, listener.created.get()); } }); Integer key2 = 2; String value2 = "value2"; cache1.put(key2, value2); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertEquals(2, listener.created.get()); } }); Set<Integer> keys = new HashSet<Integer>(); keys.add(key1); keys.add(key2); cache2.removeAll(keys); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertEquals(2, listener.removed.get()); } }); }
@Override public CacheConfig createCacheConfigIfAbsent(CacheConfig config) { final CacheConfig localConfig = configs.putIfAbsent(config.getNameWithPrefix(), config); if (localConfig == null) { if (config.isStatisticsEnabled()) { setStatisticsEnabled(config, config.getNameWithPrefix(), true); } if (config.isManagementEnabled()) { setManagementEnabled(config, config.getNameWithPrefix(), true); } } return localConfig; }
public CacheReplicationOperation(CachePartitionSegment segment, int replicaIndex) { data = new HashMap<String, Map<Data, CacheRecord>>(); Iterator<ICacheRecordStore> iter = segment.cacheIterator(); while (iter.hasNext()) { ICacheRecordStore cacheRecordStore = iter.next(); CacheConfig cacheConfig = cacheRecordStore.getConfig(); if (cacheConfig.getAsyncBackupCount() + cacheConfig.getBackupCount() >= replicaIndex) { data.put(cacheRecordStore.getName(), cacheRecordStore.getReadOnlyRecords()); } } configs = new ArrayList<CacheConfig>(segment.getCacheConfigs()); }
@Override protected <K, V> boolean createConfigOnPartition(CacheConfig<K, V> cacheConfig) { try { int partitionId = clientContext.getPartitionService().getPartitionId(cacheConfig.getNameWithPrefix()); CacheCreateConfigRequest request = new CacheCreateConfigRequest(cacheConfig, true, partitionId); final Future future = clientContext .getInvocationService() .invokeOnKeyOwner(request, cacheConfig.getNameWithPrefix()); return (Boolean) clientContext.getSerializationService().toObject(future.get()); } catch (Exception e) { throw ExceptionUtil.rethrow(e); } }
@Override public void setManagementEnabled( CacheConfig cacheConfig, String cacheNameWithPrefix, boolean enabled) { cacheConfig = cacheConfig != null ? cacheConfig : configs.get(cacheNameWithPrefix); if (cacheConfig != null) { final String cacheManagerName = cacheConfig.getUriString(); cacheConfig.setManagementEnabled(enabled); if (enabled) { final CacheMXBeanImpl mxBean = new CacheMXBeanImpl(cacheConfig); MXBeanUtil.registerCacheObject(mxBean, cacheManagerName, cacheConfig.getName(), false); } else { MXBeanUtil.unregisterCacheObject(cacheManagerName, cacheConfig.getName(), false); deleteCacheStat(cacheNameWithPrefix); } } }
protected AbstractBaseCacheProxy(CacheConfig cacheConfig, CacheDistributedObject delegate) { this.name = cacheConfig.getName(); this.cacheConfig = cacheConfig; this.delegate = delegate; this.nodeEngine = delegate.getNodeEngine(); this.serializationService = this.nodeEngine.getSerializationService(); if (cacheConfig.getCacheLoaderFactory() != null) { final Factory<CacheLoader> cacheLoaderFactory = cacheConfig.getCacheLoaderFactory(); cacheLoader = cacheLoaderFactory.create(); } else { cacheLoader = null; } asyncListenerRegistrations = new ConcurrentHashMap<CacheEntryListenerConfiguration, String>(); syncListenerRegistrations = new ConcurrentHashMap<CacheEntryListenerConfiguration, String>(); syncLocks = new ConcurrentHashMap<Integer, CountDownLatch>(); }
@Override public void run() throws Exception { final ICacheService service = getService(); CacheConfig cacheConfig = service.getCacheConfig(name); if (cacheConfig == null) { cacheConfig = service.findCacheConfig(simpleName); if (cacheConfig != null) { cacheConfig.setManagerPrefix(name.substring(0, name.lastIndexOf(simpleName))); CacheConfig existingCacheConfig = service.putCacheConfigIfAbsent(cacheConfig); if (existingCacheConfig != null) { cacheConfig = existingCacheConfig; } } } response = cacheConfig; }
protected AbstractClientInternalCacheProxy( CacheConfig cacheConfig, ClientContext clientContext, HazelcastClientCacheManager cacheManager) { super(cacheConfig, clientContext); this.cacheManager = cacheManager; this.nearCacheManager = clientContext.getNearCacheManager(); this.asyncListenerRegistrations = new ConcurrentHashMap<CacheEntryListenerConfiguration, String>(); this.syncListenerRegistrations = new ConcurrentHashMap<CacheEntryListenerConfiguration, String>(); this.syncLocks = new ConcurrentHashMap<Integer, CountDownLatch>(); initNearCache(); if (nearCache != null) { this.statistics = new ClientCacheStatisticsImpl(System.currentTimeMillis(), nearCache.getNearCacheStats()); } else { this.statistics = new ClientCacheStatisticsImpl(System.currentTimeMillis()); } this.statisticsEnabled = cacheConfig.isStatisticsEnabled(); }
@Override protected <K, V> void addCacheConfigIfAbsentToLocal(CacheConfig<K, V> cacheConfig) { configs.putIfAbsent(cacheConfig.getNameWithPrefix(), cacheConfig); }
private CacheConfig newCacheConfig(String cacheName, String mergePolicy) { CacheConfig cacheConfig = new CacheConfig(); cacheConfig.setName(cacheName); cacheConfig.setMergePolicy(mergePolicy); return cacheConfig; }
@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."); }