Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hazelcast spring configuration

Whats the difference between <hz:map> tag created in the applicationContext vs the one that is defined in the <hz:config> segment?

How are they related?

I am aware that <hz:map> in applicationContext would result in creation of a bean of type IMap and it won't when no <hz:map> is there.

But what does the following configuration do when there is a bean defined and subsequently have a <hz:map> with same name under hazelcast configuration?

<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>
like image 826
Manish Avatar asked Oct 20 '15 09:10

Manish


1 Answers

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

This will result in creation of a bean named 'loggedInUserMap' (pointed by id attribute). The name of the map in Hazelcast context will be also "loggedInUserMap" (pointed by name attribute).

A <hz:map> tag inside <hz:config> refers to a specific configuration that can be used while creating an IMap (Here by referred to as MapConfig). There could be many such MapConfigs in a hazelcast.xml. One MapConfig can be shared by multiple IMaps as well using wildcard *.

If you have a MapConfig with name that matches with the map "name" used in hazelcast context, then that configuration will be used while creating that IMap object. In your case it is "loggedInUserMap".

If not found, MapConfig with name "default" will be used to create that IMap object.

If not found, defaults for the IMap will be used while creating that IMap object.

I think the following example will sort things out clearly.

Sample Config

<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" />

Sample code

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

I hope the code snippet with comments are self-explanatory.

like image 98
Dinesh Avatar answered Sep 23 '22 01:09

Dinesh