Hazelcast spring 配置

Hazelcast spring configuration

applicationContext 段中创建的 <hz:map> 标记与在 <hz:config> 段中定义的标记有什么区别?

它们有什么关系?

我知道 applicationContext 中的 <hz:map> 会导致创建 IMap 类型的 bean,而当没有 <hz:map> 时它不会。

但是当定义了一个 bean 并且随后在 hazelcast 配置下有一个同名的 <hz:map> 时,下面的配置会做什么?

<hz:map id="loggedInUserMap" name="loggedInUserMap" instance-ref="ipds" scope="singleton" />
<hz:hazelcast id="ipds">

        <hz:config>

            <hz:instance-name>${hz.instance.name}</hz:instance-name>
            <hz:group name="${hz.group.name}" password="${hz.group.password}"/>

            <hz:map name="loggedInUserMap" backup-count="1" eviction-policy="NONE" in-memory-format="BINARY">
                <hz:near-cache time-to-live-seconds="0" max-idle-seconds="60"
                               eviction-policy="LRU" max-size="5000"  invalidate-on-change="true"/>
            </hz:map>

        </hz:config>

    </hz:hazelcast>
<hz:map id="loggedInUserMap" name="loggedInUserMap" 
            instance-ref="ipds" scope="singleton" />

这将导致创建一个名为 'loggedInUserMap' 的 bean(由 id 属性指向)。 Hazelcast 上下文中的地图名称也将是 "loggedInUserMap"(由 name 属性指向)。

A <hz:map> 标签在 <hz:config> 中指的是在创建 IMap 时可以使用的特定配置(这里称为 MapConfig)。 hazelcast.xml 中可能有很多这样的 MapConfigs。一个 MapConfig 可以被多个 IMap 共享,也可以使用通配符 *.

如果您的 MapConfig name 与 hazelcast 上下文中使用的地图 "name" 相匹配,则在创建该 IMap 对象时将使用该配置。在你的情况下是 "loggedInUserMap".

如果未找到,名称为 "default" 的 MapConfig 将用于创建该 IMap 对象。

如果未找到,则在创建该 IMap 对象时将使用 IMap 的默认值。

我想下面的例子会理清思路。

示例配置

<hz:config>
    <hz:instance-name>${hz.instance.name}</hz:instance-name>
    <hz:group name="${hz.group.name}" password="${hz.group.password}"/>

    <hz:map name="default" 
        backup-count="2" max-size="0"
        time-to-live-seconds="25" eviction-percentage="30"
        eviction-policy="NONE"/>

    <hz:map name="userMap" 
        backup-count="2" max-size="0" 
        time-to-live-seconds="6000" eviction-percentage="30"
        eviction-policy="NONE"/>

    <hz:map name="FruitMap*" 
        backup-count="2" max-size="0" 
        time-to-live-seconds="10" eviction-percentage="30"
        eviction-policy="NONE"/>

</hz:config>

<hz:map instance-ref="ipds" id="userMapSpringId" name="userMap" />
<hz:map instance-ref="ipds" id="mangoMapSpringId" name="FruitMap1" />
<hz:map instance-ref="ipds" id="appleMapSpringId" name="FruitMap2" />
<hz:map instance-ref="ipds" id="alientFruitMapSpringId" name="AlienFruit" />

示例代码

IMap map1 = (IMap) ctx.getBean("userMapSpringId");
// map1 will make use of the configuration with name "userMap"

IMap map2 = (IMap) ctx.getBean("mangoMapSpringId");
IMap map3 = (IMap) ctx.getBean("appleMapSpringId");
// Here two different IMaps objects are created. 
// However both map2 and map3 will make use of the same configuration "FruitMap*". 

IMap map4 = (IMap) ctx.getBean("alientFruitMapSpringId");
// In the case of map4, there is no configuration which matches its hazelcast name 
// (AlienFruit). Hence it will make use of the configuration with name "default".

我希望带注释的代码片段是不言自明的。