Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic property setting for maps in Hazelcast?

Is there a way to programmatically set the "time to live" property (or in fact, any property) for a distributed map in Hazelcast?

I want to avoid having to change the Hazelcast config XML for this.

I am using Hazelcast version 1.7.1

like image 464
david webber Avatar asked Feb 24 '10 09:02

david webber


1 Answers

You can set everything programmatically in Hazelcast starting with 1.7.1 version. You need to create a Config object and pass it.

Here is how you do it in Hazelcast 1.7.1

If you are using Hazelcast static methods to get map, like Hazelcast.getMap("myMapName") then this is the way:

//You need to do this once on each JVM(Hazelcast node) at the begining  
Config myConfig = new Config();
Map<String, MapConfig> myHazelcastMapConfigs = myConfig.getMapMapConfigs();
MapConfig myMapConfig = new MapConfig();
myMapConfig.setName("myMapName");
myMapConfig.setTimeToLiveSeconds(1000);
myHazelcastMapConfigs.put("myMapName", myMapConfig);
Hazelcast.init(myConfig);

But if you are creating Hazelcast instances with Hazelcast.newHazelcastInstance then pass the config to this method. then get map from the instance. This way you can create multiple hazelcast instances in same JVM. Here is the code

HazelcastInstance h = Hazelcast.newHazelcastInstance(myConfig);

h.getMap("myMapName");

In hazelcast latest version creating the config object is even simpler:

Config config = new XmlConfigBuilder().build();
config.getMapConfig("myMapName").setTimeToLiveSeconds(10000);

By the way Hazelcast 1.8.1 final is about to release. I suggest you to switch to that version.

Cheers...

like image 137
Fuad Malikov Avatar answered Nov 15 '22 07:11

Fuad Malikov