循环遍历从 Hazelcast 的 ClientConfig 实例创建的 mapConfig 时出现 UnsupportedOperationException
UnsupportedOperationException when looping over a mapConfig created from a ClientConfig instance of Hazelcast
请问您如何循环遍历 mapConfig 以更改从 ClientConfig class 设置的 运行 Hazelcast 的备份计数?
我已经检查了 hazelcast code examples 但在所有情况下,备份计数都是通过配置设置的
我也尝试应用 (顺便说一句,这是我自己问的)但我面临 UnsupportedOperationException 因为 Hazelcast ClientConfig 实例似乎无法在运行时更改,是否正确?
it/suggestion 是否有任何解决方法来克服它?
我有以下实现:
ClientConfig cfg = new ClientConfig();
if (hazelcastConfPath != null) {
cfg = new XmlClientConfigBuilder(hazelcastConfPath).build();
}
cfg.setProperty("hazelcast.logging.type", "slf4j");
hazelcastInstance = HazelcastClient.newHazelcastClient(cfg);
int clusterSize = hazelcastInstance.getCluster().getMembers().size();
logger.info("Cluster size: {}", clusterSize);
for (Entry<String, MapConfig> currMap : hazelcastInstance.getConfig().getMapConfigs().entrySet()) {
if (clusterSize - 1 > 1) {
try {
currMap.getValue().setBackupCount(clusterSize);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
logger.info("Map {} configured with {}", currMap.getKey(), currMap.getValue().getBackupCount());
}
和 Hazelcast XML 配置:
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast ...>
<network>
<port auto-increment="false">${hazelcast.port}</port>
</network>
<management-center enabled="true" update-interval="3">
${hazelcast.mancenter.url}
</management-center>
<group>
<name>${group.name}</name>
<password>${group.password}</password>
</group>
<map name="*token">
<max-idle-seconds>120</max-idle-seconds>
<backup-count>1</backup-count>
<eviction-policy>LRU</eviction-policy>
</map>
<map name="*actionSessionRedirUrlMap">
<max-idle-seconds>30</max-idle-seconds>
<backup-count>1</backup-count>
<eviction-policy>LRU</eviction-policy>
</map>
</hazelcast>
这样做,我遇到了 UnsupportedOperationException,这似乎是不可能的:'(
java.lang.UnsupportedOperationException: Client config object only supports adding new data structure configurations
at com.hazelcast.client.impl.clientside.ClientDynamicClusterConfig.getMapConfigs(ClientDynamicClusterConfig.java:591)
...
非常感谢,
杰拉尔多·内托
您可以通过管理中心 (https://docs.hazelcast.org/docs/management-center/latest/manual/html/index.html#deploying-and-starting) 或通过使用名称相同但备份计数不同的新 MapConfig 启动精简会员来实现此目的。请参阅下面的示例:
MapConfig mapConfig = new MapConfig("map_a");
mapConfig.setBackupCount(3);
Config config = new Config();
config.setLiteMember(true);
config.addMapConfig(mapConfig);
HazelcastInstance lite = Hazelcast.newHazelcastInstance(config);
请问您如何循环遍历 mapConfig 以更改从 ClientConfig class 设置的 运行 Hazelcast 的备份计数?
我已经检查了 hazelcast code examples 但在所有情况下,备份计数都是通过配置设置的
我也尝试应用
我有以下实现:
ClientConfig cfg = new ClientConfig();
if (hazelcastConfPath != null) {
cfg = new XmlClientConfigBuilder(hazelcastConfPath).build();
}
cfg.setProperty("hazelcast.logging.type", "slf4j");
hazelcastInstance = HazelcastClient.newHazelcastClient(cfg);
int clusterSize = hazelcastInstance.getCluster().getMembers().size();
logger.info("Cluster size: {}", clusterSize);
for (Entry<String, MapConfig> currMap : hazelcastInstance.getConfig().getMapConfigs().entrySet()) {
if (clusterSize - 1 > 1) {
try {
currMap.getValue().setBackupCount(clusterSize);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
logger.info("Map {} configured with {}", currMap.getKey(), currMap.getValue().getBackupCount());
}
和 Hazelcast XML 配置:
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast ...>
<network>
<port auto-increment="false">${hazelcast.port}</port>
</network>
<management-center enabled="true" update-interval="3">
${hazelcast.mancenter.url}
</management-center>
<group>
<name>${group.name}</name>
<password>${group.password}</password>
</group>
<map name="*token">
<max-idle-seconds>120</max-idle-seconds>
<backup-count>1</backup-count>
<eviction-policy>LRU</eviction-policy>
</map>
<map name="*actionSessionRedirUrlMap">
<max-idle-seconds>30</max-idle-seconds>
<backup-count>1</backup-count>
<eviction-policy>LRU</eviction-policy>
</map>
</hazelcast>
这样做,我遇到了 UnsupportedOperationException,这似乎是不可能的:'(
java.lang.UnsupportedOperationException: Client config object only supports adding new data structure configurations
at com.hazelcast.client.impl.clientside.ClientDynamicClusterConfig.getMapConfigs(ClientDynamicClusterConfig.java:591)
...
非常感谢, 杰拉尔多·内托
您可以通过管理中心 (https://docs.hazelcast.org/docs/management-center/latest/manual/html/index.html#deploying-and-starting) 或通过使用名称相同但备份计数不同的新 MapConfig 启动精简会员来实现此目的。请参阅下面的示例:
MapConfig mapConfig = new MapConfig("map_a");
mapConfig.setBackupCount(3);
Config config = new Config();
config.setLiteMember(true);
config.addMapConfig(mapConfig);
HazelcastInstance lite = Hazelcast.newHazelcastInstance(config);