Hazelcast 中地图的编程属性设置?

发布于 2024-08-22 17:34:59 字数 127 浏览 3 评论 0原文

有没有办法以编程方式为 Hazelcast 中的分布式地图设置“生存时间”属性(或者实际上是任何属性)?

我想避免为此更改 Hazelcast 配置 XML。

我使用的是 Hazelcast 版本 1.7.1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2024-08-29 17:34:59

从 1.7.1 版本开始,您可以在 Hazelcast 中以编程方式设置所有内容。
您需要创建一个 Config 对象并传递它。

这是在 Hazelcast 1.7.1 中执行此操作的方法

如果您使用 Hazelcast 静态方法来获取地图,例如 Hazelcast.getMap("myMapName"),则方法如下:

//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);

但如果您使用 Hazelcast.newHazelcastInstance 创建 Hazelcast 实例,则通过此方法的配置。然后从实例获取地图。这样你就可以在同一个 JVM 中创建多个 hazelcast 实例。以下是 hazelcast 最新版本中的代码

HazelcastInstance h = Hazelcast.newHazelcastInstance(myConfig);

h.getMap("myMapName");

,创建配置对象更加简单:

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

顺便说一句,Hazelcast 1.8.1 Final 即将发布。我建议你切换到那个版本。

干杯...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文