문제

I'm using a map store to persist my hazelcast distributed map in a database.

In my test case, I start three hazelcast instances, each configured the same way:

Config cfg = new Config();
cfg.setInstanceName("name");
hazelcast = Hazelcast.newHazelcastInstance(cfg);    
MapConfig mapConfig = new MapConfig("myMapName");
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setClassName(MyMapStore.class.getName());
mapStoreConfig.setWriteDelaySeconds(0);
mapStoreConfig.setEnabled(true);
mapConfig.setMapStoreConfig(mapStoreConfig);
mapConfig.setBackupCount(2);
hazelcast.getConfig().addMapConfig(mapConfig);
IMap myMap = hazelcast.getMap("myMapName");

However, when I add values to the map, only the first Cluster member writes into the database, the MapStoreConfig is just set to Default values on each other node. But, if I change the code to the following, it works:

Config cfg = new Config();
cfg.setInstanceName("name");
MapConfig mapConfig = new MapConfig("myMapName");
MapStoreConfig mapStoreConfig = new MapStoreConfig();
mapStoreConfig.setClassName(MyMapStore.class.getName());
mapStoreConfig.setWriteDelaySeconds(0);
mapStoreConfig.setEnabled(true);
mapConfig.setMapStoreConfig(mapStoreConfig);
mapConfig.setBackupCount(2);
cfg.addMapConfig(mapConfig);
hazelcast = Hazelcast.newHazelcastInstance(cfg);
IMap myMap = hazelcast.getMap("myMapName");

Seems like the line hazelcast.getConfig().addMapConfig(mapConfig); is ignored. Tested with hazelcast v3.1.5.

도움이 되었습니까?

해결책

To my understanding, the Config instance should be considered only as a sort of "template", which is used to create one instance, but may not be used to afterwards modify this instance. Although, according to a quick websearch and a look at the documentation, this is not stated explicitly, but

  1. all examples that I have seen so far follow the pattern of FIRST creating and the complete Config, and THEN creating the corresponding instance and
  2. the addMapConfig method (in https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/config/Config.java ) does not involve any notification mechanism. So there would at least have to be some mechanism to inform the Hazelcast instance about the changed config, but I think that such a mechanism does not exist.

From what I have read in the source code ( e.g. in https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/instance/Node.java and https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/java/com/hazelcast/instance/HazelcastInstanceImpl.java ), the Config is only read at construction time, and it does not seem to be possible to change the configuration after the Hazelcast instance has been created.

다른 팁

The ability to configure Hazelcast at runtime is convenient, but very misleading. I use Spring, so I've always had crystal clarity about the concept of the configuration. In most systems (Hazelcast being no exception) configuration should be specified before instantiating the "engine".

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top