/** * Hazelcast config config. * * @return the config */ @Bean public Config hazelcastConfig() { return new Config().setProperty("hazelcast.jmx", "true") .addMapConfig(new MapConfig("spring-boot-admin-application-store").setBackupCount(1) .setEvictionPolicy(EvictionPolicy.NONE)) .addListConfig(new ListConfig("spring-boot-admin-event-store").setBackupCount(1) .setMaxSize(1000)); }
// Build the config from xml file you have in the classpath Config config = new XmlConfigBuilder().build(); MapStoreConfig msc = new MapStoreConfig.. msc.setFactoryImplementation(new StoreClientFactory()); MapConfig mapConfig = new MapConfig.. mapConfig.setMapStoreConfig(msc); config.addMapConfig(mapconfig); // init Hazelcast with this configuration // make sure you call init fore any other Hazelcast operation!! Hazelcast.init(config); // then access to your map Map map = Hazelcast.getMap("mymap");
@Override public void modifyConfiguration(final Config config) { Arrays.asList(CacheConstants.PLAYER_S_AND_SID_CACHE, CacheConstants.PLAYER_ID_CACHE, CacheConstants.PLAYER_MD5_CACHE, CacheConstants.GAME_ID_CACHE).forEach(cache -> { MapConfig mc = new MapConfig(cache); mc.setMaxIdleSeconds(FIVE_MINUTES); config.addMapConfig(mc); }); } }
public static void main(String[] args) throws InterruptedException { Config config = new Config(); config.addMapConfig(new MapConfig("default").setTimeToLiveSeconds(5)); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); Map<Integer, String> map = hazelcastInstance.getMap("default"); map.put(0, "Number Zero"); // 0 overrides the map config and make the entry not expirable ((IMap<Integer, String>) map).put(1, "Number One", 0, SECONDS); System.out.println("Entry 0: " + map.get(0) + ", Entry 1: " + map.get(1)); Thread.sleep(SECONDS.toMillis(5)); if (map.get(0) != null) { System.err.println("Entry 0 is still alive!"); } if (map.get(1) != null) { System.out.println("Entry 1 is still alive!"); } System.exit(0); }
public static void main(String[] args) throws InterruptedException { Config config = new Config(); config.addMapConfig(new MapConfig("default").setMaxIdleSeconds(5)); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); Map<Integer, String> map = hazelcastInstance.getMap("default"); map.put(0, "Number Zero"); // 0 overrides the map config and make the entry not expirable ((IMap<Integer, String>) map).put(1, "Number One", 0, SECONDS, 0, SECONDS); System.out.println("Entry 0: " + map.get(0) + ", Entry 1: " + map.get(1)); Thread.sleep(SECONDS.toMillis(5)); if (map.get(0) != null) { System.err.println("Entry 0 is still alive!"); } if (map.get(1) != null) { System.out.println("Entry 1 is still alive!"); } System.exit(0); }
/** * Initializes a Vert.x Hazelcast instance for testing. */ public static void initialize() { if (!isHazelcastCluster()) { Hazelcast.newHazelcastInstance(new Config().addMapConfig(new MapConfig().setName("subs"))); } }
static <K, V> IMap<K, V> getMap(HazelcastInstance instance, String mapFQName, StreamConfig streamConfig, MapStoreConfig mapStoreConfig) { final MapConfig mapConfig = new MapConfig(mapFQName) .setBackupCount(streamConfig.getSyncReplicas()) .setAsyncBackupCount(streamConfig.getAsyncReplicas()) .setMapStoreConfig(mapStoreConfig); instance.getConfig().addMapConfig(mapConfig); return instance.getMap(mapFQName); } }
private MapConfig convertAndStoreMapConfig(String mapName, CacheSettings newSettings, Config config, MapConfig baseConfig) { MapConfig newConfig = new MapConfig(baseConfig); newConfig.setName(mapName); newConfig.setStatisticsEnabled(true); new HazelcastMapConfigConfigurator().configureMapConfig(newSettings, newConfig, hazelcast.getPartitionService().getPartitions().size()); config.addMapConfig(newConfig); return newConfig; }
Config config = new Config(); .... MapConfig mapCfg = new MapConfig(); mapCfg.setName("default"); mapCfg.setBackupCount(2); mapCfg.getMaxSizeConfig().setSize(10000); mapCfg.setTimeToLiveSeconds(300); MapStoreConfig mapStoreCfg = new MapStoreConfig(); mapStoreCfg.setClassName(MyMapStore.class.getName()).setEnabled(true); mapCfg.setMapStoreConfig(mapStoreCfg); // use near cache if needed NearCacheConfig nearCacheConfig = new NearCacheConfig(); nearCacheConfig.setMaxSize(1000).setMaxIdleSeconds(120).setTimeToLiveSeconds(300); mapCfg.setNearCacheConfig(nearCacheConfig); config.addMapConfig(mapCfg); Hazelcast.newHazelcastInstance(config);
@Bean public TicketRegistry ticketRegistry() { val hz = casProperties.getTicket().getRegistry().getHazelcast(); val factory = new HazelcastConfigurationFactory(); ticketCatalog.getIfAvailable().findAll().stream() .map(TicketDefinition::getProperties) .peek(p -> LOGGER.debug("Created Hazelcast map configuration for [{}]", p)) .map(p -> factory.buildMapConfig(hz, p.getStorageName(), p.getStorageTimeout())) .forEach(m -> hazelcastInstance.getIfAvailable().getConfig().addMapConfig(m)); val r = new HazelcastTicketRegistry(hazelcastInstance.getIfAvailable(), ticketCatalog.getIfAvailable(), hz.getPageSize()); r.setCipherExecutor(CoreTicketUtils.newTicketRegistryCipherExecutor(hz.getCrypto(), "hazelcast")); return r; }
public static void main(String[] args) { MergePolicyConfig mergePolicyConfig = new MergePolicyConfig() .setPolicy(MapEntryCostsMergePolicy.class.getName()); MapConfig mapConfig = new MapConfig("default") .setMergePolicyConfig(mergePolicyConfig); ReplicatedMapConfig replicatedMapConfig = new ReplicatedMapConfig("default") .setMergePolicyConfig(mergePolicyConfig); Config config = new Config() .addMapConfig(mapConfig) .addReplicatedMapConfig(replicatedMapConfig); try { HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); // this works, since it's an IMap hazelcastInstance.getMap("myMap"); try { hazelcastInstance.getReplicatedMap("myReplicatedMap"); } catch (InvalidConfigurationException expected) { System.out.println("The configured merge policy is just suitable for IMap: " + expected.getMessage()); } } finally { Hazelcast.shutdownAll(); } } }
public static void main(String[] args) throws Exception { // for a custom merge policy we have to provide the FQCN, not just the simple classname MergePolicyConfig mergePolicyConfig = new MergePolicyConfig() .setPolicy(UserContextMergePolicy.class.getName()); MapConfig mapConfig = new MapConfig() .setName(MAP_NAME) .setInMemoryFormat(InMemoryFormat.BINARY) .setBackupCount(1) .setAsyncBackupCount(0) .setMergePolicyConfig(mergePolicyConfig); // we use the user context to provide access to our TruthProvider in the merge policy ConcurrentMap<String, Object> userContext = new ConcurrentHashMap<String, Object>(); userContext.put(TRUTH_PROVIDER_ID, new ExampleTruthProvider()); final Config config = new Config() //.setProperty("hazelcast.logging.type", "none") .addMapConfig(mapConfig) .setUserContext(userContext); HazelcastInstanceFactory.newHazelcastInstance(config); Hazelcast.shutdownAll(); }
private void setupMapConfig(String name, int size) { MapConfig cfg = new MapConfig(NODE_CACHE); cfg.setMaxSizeConfig(new MaxSizeConfig(size, MaxSizeConfig.MaxSizePolicy.PER_PARTITION)); cfg.setAsyncBackupCount(1); cfg.setBackupCount(0); cfg.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); cfg.setMaxIdleSeconds(600); // 10 minutes cfg.setTimeToLiveSeconds(3600); // 1 hour hcConfiguration.addMapConfig(cfg); }
private void setupMapConfig(String name, int size) { MapConfig cfg = new MapConfig(NODE_CACHE); cfg.setMaxSizeConfig(new MaxSizeConfig(size, MaxSizeConfig.MaxSizePolicy.PER_PARTITION)); cfg.setAsyncBackupCount(1); cfg.setBackupCount(0); cfg.setEvictionPolicy(MapConfig.EvictionPolicy.LRU); cfg.setMaxIdleSeconds(600); // 10 minutes cfg.setTimeToLiveSeconds(3600); // 1 hour hcConfiguration.addMapConfig(cfg); }
public static void main(String[] args) { // for a custom merge policy we have to provide the FQCN, not just the simple classname MergePolicyConfig mergePolicyConfig = new MergePolicyConfig() .setPolicy(ComposedHitsAndCreationTimeMergePolicy.class.getName()); MapConfig mapConfig = new MapConfig() .setName(MAP_NAME) .setInMemoryFormat(InMemoryFormat.BINARY) .setBackupCount(1) .setAsyncBackupCount(0) .setMergePolicyConfig(mergePolicyConfig); final Config config = new Config() //.setProperty("hazelcast.logging.type", "none") .addMapConfig(mapConfig); HazelcastInstanceFactory.newHazelcastInstance(config); Hazelcast.shutdownAll(); } }
@Test public void givenMemberHasClassLoaderConfigured_whenObjectIsStored_thenClassLoaderWillBeUsed() throws Exception { String mapName = randomMapName(); Config config = new Config(); SubZero.useAsGlobalSerializer(config); ClassLoader spyingClassLoader = createSpyingClassLoader(); config.setClassLoader(spyingClassLoader); config.addMapConfig(new MapConfig(mapName).setInMemoryFormat(OBJECT)); HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config); IMap<Integer, Object> myMap = member.getMap(mapName); myMap.put(0, new MyClass()); verify(spyingClassLoader).loadClass("info.jerrinot.subzero.ClassLoadingTest$MyClass"); }
public static Config newConfig() { MapConfig mapConfig = new MapConfig(); mapConfig.setName("default"); mapConfig.setInMemoryFormat(InMemoryFormat.NATIVE); MemorySize memorySize = new MemorySize(128, MemoryUnit.MEGABYTES); NativeMemoryConfig memoryConfig = new NativeMemoryConfig(); memoryConfig.setEnabled(true); memoryConfig.setSize(memorySize); memoryConfig.setAllocatorType(NativeMemoryConfig.MemoryAllocatorType.STANDARD); Config config = new Config(); config.addMapConfig(mapConfig); config.setNativeMemoryConfig(memoryConfig); config.setLicenseKey(ENTERPRISE_LICENSE_KEY); return config; } }
public static void main(String[] args) throws ParseException { MapAttributeConfig mapAttributeConfig = new MapAttributeConfig(); mapAttributeConfig.setName("limbName"); mapAttributeConfig.setExtractor("com.test.portable.LimbNameExtractor"); MapConfig mapConfig = new MapConfig(); mapConfig.setName("people"); mapConfig.addMapAttributeConfig(mapAttributeConfig); Config config = new Config(); config.getSerializationConfig().addPortableFactory(PersonFactory.FACTORY_ID, new PersonFactory()); config.addMapConfig(mapConfig); HazelcastInstance hz = Hazelcast.newHazelcastInstance(config); IMap<Integer, Person> map = hz.getMap("people"); map.put(1, new Person("Georg", limb("left-leg"), limb("right-leg"))); map.put(2, new Person("Peter", limb("left-hand"), limb("right-hand"))); map.put(3, new Person("Hans", limb("left-finger"), limb("right-finger"))); map.put(4, new Person("Stefanie", limb("left-arm"), limb("right-arm"))); // we're using a custom attribute 'limbName' which is provided by the 'LimbNameExtractor' Set<Person> people = (Set<Person>) map.values(Predicates.equal("limbName", "left-arm")); System.out.println("People: " + people); Hazelcast.shutdownAll(); }
public static Config newConfig() { MaxSizeConfig maxSizeConfig = new MaxSizeConfig(); maxSizeConfig.setMaxSizePolicy(PER_NODE); maxSizeConfig.setSize(MAX_ENTRY_COUNT); MapConfig mapConfig = new MapConfig(); mapConfig.setName("default"); mapConfig.setInMemoryFormat(InMemoryFormat.NATIVE); mapConfig.setEvictionPolicy(EvictionPolicy.LRU); mapConfig.setMaxSizeConfig(maxSizeConfig); mapConfig.setMinEvictionCheckMillis(0); MemorySize memorySize = new MemorySize(128, MemoryUnit.MEGABYTES); NativeMemoryConfig memoryConfig = new NativeMemoryConfig(); memoryConfig.setEnabled(true); memoryConfig.setSize(memorySize); memoryConfig.setAllocatorType(NativeMemoryConfig.MemoryAllocatorType.STANDARD); Config config = new Config(); config.addMapConfig(mapConfig); config.setNativeMemoryConfig(memoryConfig); config.setLicenseKey(ENTERPRISE_LICENSE_KEY); return config; } }
private static Config newConfig() { NearCacheConfig nearCacheConfig = new NearCacheConfig(); EvictionConfig evictionConfig = nearCacheConfig.getEvictionConfig(); evictionConfig.setMaximumSizePolicy(USED_NATIVE_MEMORY_PERCENTAGE); evictionConfig.setSize(90); nearCacheConfig.setInMemoryFormat(NATIVE); nearCacheConfig.setInvalidateOnChange(true); nearCacheConfig.setCacheLocalEntries(true); nearCacheConfig.setName("default"); MapConfig mapConfig = new MapConfig(); mapConfig.setName("default"); mapConfig.setNearCacheConfig(nearCacheConfig); NativeMemoryConfig memoryConfig = new NativeMemoryConfig(); memoryConfig.setEnabled(true); memoryConfig.setSize(new MemorySize(128, MEGABYTES)); memoryConfig.setAllocatorType(STANDARD); Config config = new Config(); config.addMapConfig(mapConfig); config.setNativeMemoryConfig(memoryConfig); config.setLicenseKey(ENTERPRISE_LICENSE_KEY); return config; } }