@Bean @Qualifier("RateLimit") public ReadWriteMap<String, GridBucketState> map() { DefaultCacheManager cacheManager = new DefaultCacheManager(); cacheManager.defineConfiguration("rateLimit", new ConfigurationBuilder().build()); AdvancedCache<String, GridBucketState> cache = cacheManager.<String, GridBucketState>getCache("rateLimit").getAdvancedCache(); FunctionalMapImpl<String, GridBucketState> functionalMap = FunctionalMapImpl.create(cache); return ReadWriteMapImpl.create(functionalMap); }
@Override public void leave() { channel.close(); channel = null; cacheManager.stop(); }
public static void main(String[] args) throws Exception { // Setup up a clustered cache manager GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); // Make the default cache a replicated synchronous one ConfigurationBuilder builder = new ConfigurationBuilder(); builder.clustering().cacheMode(CacheMode.REPL_SYNC); // Initialize the cache manager DefaultCacheManager cacheManager = new DefaultCacheManager(global.build(), builder.build()); // Obtain the default cache Cache<String, String> cache = cacheManager.getCache(); // Store the current node address in some random keys for(int i=0; i < 10; i++) { cache.put(UUID.randomUUID().toString(), cacheManager.getNodeAddress()); } // Display the current cache contents for the whole cluster cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue())); // Display the current cache contents for this node cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP) .entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue())); // Stop the cache manager and release all resources cacheManager.stop(); }
manager = new DefaultCacheManager(configXml); } else { GlobalConfigurationBuilder globalConfigurationBuilder = new GlobalConfigurationBuilder(); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); globalConfigurationBuilder.read(infinispanGlobalConfigurer.getGlobalConfiguration()); } else { globalConfigurationBuilder.globalJmxStatistics().jmxDomain(DEFAULT_JMX_DOMAIN).enable(); globalConfigurationBuilder.transport().clusterName(infinispanProperties.getClusterName()); configurationCustomizers.forEach(customizer -> customizer.customize(configurationBuilder)); manager = new DefaultCacheManager(globalConfigurationBuilder.build(), configurationBuilder.build());
public static void main(String[] args) throws Exception { // Define the default cache to be transactional ConfigurationBuilder builder = new ConfigurationBuilder(); builder.transaction().transactionMode(TransactionMode.TRANSACTIONAL); // Construct a local cache manager using the configuration we have defined DefaultCacheManager cacheManager = new DefaultCacheManager(builder.build()); // Obtain the default cache Cache<String, String> cache = cacheManager.getCache(); // Obtain the transaction manager TransactionManager transactionManager = cache.getAdvancedCache().getTransactionManager(); // Perform some operations within a transaction and commit it transactionManager.begin(); cache.put("key1", "value1"); cache.put("key2", "value2"); transactionManager.commit(); // Display the current cache contents System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2")); // Perform some operations within a transaction and roll it back transactionManager.begin(); cache.put("key1", "value3"); cache.put("key2", "value4"); transactionManager.rollback(); // Display the current cache contents System.out.printf("key1 = %s\nkey2 = %s\n", cache.get("key1"), cache.get("key2")); // Stop the cache manager and release all resources cacheManager.stop(); }
public void testDefaultEnlistment() { ConfigurationBuilder builder = getLocalBuilder(); builder.transaction().transactionMode(TransactionMode.TRANSACTIONAL); dcm = new DefaultCacheManager(getGlobalConfig(), builder.build()); Cache<Object,Object> cache = dcm.getCache(); assertFalse(cache.getCacheConfiguration().transaction().useSynchronization()); assertFalse(cache.getCacheConfiguration().transaction().recovery().enabled()); cache.put("k", "v"); assertEquals("v", cache.get("k")); }
public void testConfigBuilder() { GlobalConfiguration globalConfig = new GlobalConfigurationBuilder() .transport().defaultTransport() .build(); Configuration cacheConfig = new ConfigurationBuilder().persistence().addStore(LevelDBStoreConfigurationBuilder.class).location(tmpDataDirectory) .expiredLocation(tmpExpiredDirectory).implementationType(LevelDBStoreConfiguration.ImplementationType.AUTO).build(); StoreConfiguration cacheLoaderConfig = cacheConfig.persistence().stores().get(0); assertTrue(cacheLoaderConfig instanceof LevelDBStoreConfiguration); LevelDBStoreConfiguration leveldbConfig = (LevelDBStoreConfiguration) cacheLoaderConfig; assertEquals(tmpDataDirectory, leveldbConfig.location()); assertEquals(tmpExpiredDirectory, leveldbConfig.expiredLocation()); EmbeddedCacheManager cacheManager = new DefaultCacheManager(globalConfig); cacheManager.defineConfiguration("testCache", cacheConfig); cacheManager.start(); Cache<String, String> cache = cacheManager.getCache("testCache"); cache.put("hello", "there"); cache.stop(); cacheManager.stop(); }
protected ServerCache getDefaultCache() { GlobalConfiguration gconfig = new GlobalConfigurationBuilder() .globalJmxStatistics() .allowDuplicateDomains(true) .enable() .jmxDomain("custom-cache") .build(); Configuration configuration = new ConfigurationBuilder() .eviction() .strategy(EvictionStrategy.LIRS) .maxEntries(100) .jmxStatistics().enable() .build(); EmbeddedCacheManager manager = new DefaultCacheManager(gconfig, configuration); Cache<Object, Object> c = manager.getCache("custom-cache"); return new InfinispanCache(c); }
public static StreamingMarshaller getMarshaller(StoreProperties props) { switch (getMarshallerType(props)) { case CURRENT: if (props.isTargetStore()) return null; GlobalConfigurationBuilder globalConfig = new GlobalConfigurationBuilder() .defaultCacheName(props.cacheName()); configureExternalizers(props, globalConfig.serialization()); EmbeddedCacheManager manager = new DefaultCacheManager(globalConfig.build(), new ConfigurationBuilder().build()); return manager.getCache().getAdvancedCache().getComponentRegistry().getComponent(StreamingMarshaller.class); case CUSTOM: String marshallerClass = props.get(MARSHALLER, CLASS); if (marshallerClass == null) throw new CacheConfigurationException( String.format("The property %s.%s must be set if a custom marshaller type is specified", MARSHALLER, CLASS)); try { return (StreamingMarshaller) Util.loadClass(marshallerClass, SerializationConfigUtil.class.getClassLoader()).newInstance(); } catch (IllegalAccessException | InstantiationException e) { throw new CacheConfigurationException(String.format("Unable to load StreamingMarshaller '%s' for %s store", marshallerClass, SOURCE), e); } case LEGACY: if (props.isTargetStore()) throw new CacheConfigurationException("The legacy marshaller can only be specified for source stores."); return new LegacyVersionAwareMarshaller(getExternalizersFromProps(props)); default: throw new IllegalStateException("Unexpected marshaller type"); } }
@Override protected EmbeddedCacheManager createCacheManager() throws Exception { GlobalConfigurationBuilder gcb = new GlobalConfigurationBuilder(); gcb.globalJmxStatistics().jmxDomain(JMX_DOMAIN).mBeanServerLookup(new PerThreadMBeanServerLookup()).enable(); ConfigurationBuilder dcc = TestCacheManagerFactory.getDefaultCacheConfiguration(true); dcc.transaction().autoCommit(false); dcc.memory().size(1000); dcc.jmxStatistics().enable(); server = PerThreadMBeanServerLookup.getThreadMBeanServer(); return new DefaultCacheManager(gcb.build(), dcc.build()); }
private void initTransactionManager() { Configuration conf = makeRequiredConfiguration(); // for the dummy cache disable, cache loaders if any was configured ConfigurationBuilder confBuilder = new ConfigurationBuilder().read(conf); confBuilder.loaders().clearCacheLoaders(); conf = confBuilder.build(); String DUMMY_CACHE = "dummy-cache"; logger.debug("Configuration for {} is: {}", DUMMY_CACHE, conf.toString()); try { this.cacheManager.defineConfiguration(DUMMY_CACHE, conf); Cache<?, ?> dummyCache = this.cacheManager.getCache(DUMMY_CACHE); TransactionManager tm = dummyCache.getAdvancedCache().getTransactionManager(); this.transactionManager = tm; } catch (Exception e) { logger.error("Failed to get Repository TransactionManager", e); throw new PersistenceException(e); } }
public static void main(String[] args) { // Construct a simple local cache manager with default configuration DefaultCacheManager cacheManager = new DefaultCacheManager(); // Define local cache configuration cacheManager.defineConfiguration("local", new ConfigurationBuilder().build()); // Obtain the local cache Cache<String, String> cache = cacheManager.getCache("local"); // Store a value cache.put("key", "value"); // Retrieve the value and print it out System.out.printf("key = %s\n", cache.get("key")); // Stop the cache manager and release all resources cacheManager.stop(); }
EmbeddedCacheManager cacheManager = new DefaultCacheManager(configStream, false); GlobalConfiguration globalConfigFromXml = cacheManager.getCacheManagerConfiguration(); GlobalConfigurationBuilder globalConfigBuilder = new GlobalConfigurationBuilder(); globalConfigBuilder.read(globalConfigFromXml); Configuration configFromXml = cacheManager.getDefaultCacheConfiguration(); ConfigurationBuilder configBuilder = new ConfigurationBuilder().read(configFromXml); if (tmLookup == null) { tmLookup = getTransactionManagerLookup(); configBuilder.transaction().transactionManagerLookup(tmLookup); cacheManager = new DefaultCacheManager(globalConfigBuilder.build(), configBuilder.build(), true); this.cacheManager = cacheManager;
protected Cache getDefaultCache() { EmbeddedCacheManager manager = new DefaultCacheManager(); manager.defineConfiguration("custom-cache", new ConfigurationBuilder() .eviction().strategy(EvictionStrategy.NONE).maxEntries(1000) .build()); return manager.getCache("custom-cache"); }
private EmbeddedCacheManager createCacheManager(String name1, JChannel ch1) { GlobalConfigurationBuilder gcb1 = new GlobalConfigurationBuilder(); gcb1.transport().nodeName(ch1.getName()).distributedSyncTimeout(10, SECONDS); CustomChannelLookup.registerChannel(gcb1, ch1, name1, false); ConfigurationBuilder replCfg = new ConfigurationBuilder(); replCfg.clustering().cacheMode(CacheMode.REPL_SYNC); replCfg.clustering().stateTransfer().timeout(10, SECONDS); EmbeddedCacheManager cm1 = new DefaultCacheManager(gcb1.build(), replCfg.build(), false); registerCacheManager(cm1); cm1.defineConfiguration(CACHE_NAME, replCfg.build()); return cm1; }
public static void main(String[] args) throws UnknownHostException { GlobalConfiguration globalConfig = new GlobalConfigurationBuilder().transport() .defaultTransport() .addProperty("configurationFile", "default-configs/default-jgroups-kubernetes.xml") Configuration cacheConfiguration = new ConfigurationBuilder() .clustering() .cacheMode(CacheMode.REPL_SYNC) .build(); DefaultCacheManager cacheManager = new DefaultCacheManager(globalConfig, cacheConfiguration); cacheManager.defineConfiguration("default", cacheConfiguration); Cache<String, String> cache = cacheManager.getCache("default"); scheduler.scheduleAtFixedRate(() -> { String time = Instant.now().toString(); cache.put(hostname, time); System.out.println("[" + time + "][" + hostname + "] Values from the cache: "); System.out.println(cache.entrySet()); }, 0, 2, TimeUnit.SECONDS); } catch (InterruptedException e) { scheduler.shutdown(); cacheManager.stop();
/** * Creates the embedded cache manager. * * @return the embedded cache manager. */ public static EmbeddedCacheManager createManager() { final GlobalConfigurationBuilder globalBuilder = new GlobalConfigurationBuilder(); globalBuilder.nonClusteredDefault().globalJmxStatistics() .allowDuplicateDomains(true).transport() .clusterName(CLUSTER_NAME); final GlobalConfiguration globalCfg = globalBuilder.build(); final ConfigurationBuilder builder = new ConfigurationBuilder(); builder.clustering().cacheMode(CacheMode.LOCAL).indexing().disable(); final Configuration cfg = builder.build(); final EmbeddedCacheManager manager = new DefaultCacheManager(globalCfg, cfg); return manager; }
private EmbeddedCacheManager infinispanCacheManager() { // Embedded Cluster Setting! GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder(); global.clusteredDefault() .globalJmxStatistics() .allowDuplicateDomains(true) .transport() .addProperty("configurationFile", configFile) .clusterName("metatron-discovery-v20"); ConfigurationBuilder config = new ConfigurationBuilder(); config.expiration().lifespan(5, TimeUnit.SECONDS); config.clustering().cacheMode(CacheMode.DIST_SYNC); DefaultCacheManager cacheManager = new DefaultCacheManager(global.build(), config.build()); cacheManager.addListener(new InfinispanClusterListener(2)); return cacheManager; } }
@Override public EmbeddedCacheManager get() { GlobalConfiguration config = this.configuration.get(); String defaultCacheName = config.defaultCacheName().orElse(null); // We need to create a dummy default configuration if cache has a default cache Configuration defaultConfiguration = (defaultCacheName != null) ? new ConfigurationBuilder().build() : null; EmbeddedCacheManager manager = new DefaultCacheManager(config, defaultConfiguration, false); // Undefine the default cache, if we defined one if (defaultCacheName != null) { manager.undefineConfiguration(defaultCacheName); } // Override GlobalConfigurationManager with a local implementation @SuppressWarnings("deprecation") BasicComponentRegistry registry = manager.getGlobalComponentRegistry().getComponent(BasicComponentRegistry.class); registry.replaceComponent(GlobalConfigurationManager.class.getName(), new LocalGlobalConfigurationManager(), false); registry.rewire(); manager.start(); manager.addListener(this); InfinispanLogger.ROOT_LOGGER.debugf("%s cache container started", this.name); return manager; }
public static void main(String[] args) { // Construct a simple local cache manager with default configuration DefaultCacheManager cacheManager = new DefaultCacheManager(); // Define local cache configuration cacheManager.defineConfiguration("local", new ConfigurationBuilder().build()); // Obtain the local cache Cache<String, String> cache = cacheManager.getCache("local"); // Register a listener cache.addListener(new MyListener()); // Store some values cache.put("key1", "value1"); cache.put("key2", "value2"); cache.put("key1", "newValue"); // Stop the cache manager and release all resources cacheManager.stop(); }