0
votes

In Hazelcast I uses client - server mode. There will be approximately 10 nodes in the cluster and there will be multiple clients accessing the same cache from different application instances. Could you please help me with the following ?

  1. In Client Server mode of Hazelcast which is the correct way to create a cache. Is it via programatically or via declarative way ?. There are multiple caches available in my application and they differ in some properties (like eviction configuration etc). Each cache will be accessed by a separate object instance inside my application (i.e one object instance will use only 1 cache).

  2. Is there any advantage of one method over another or both are same ?

Thanks JK

1

1 Answers

2
votes

@JK_007, Hazelcast can be configured either declaratively (XML) or programmatically (API) or a combination of both can be used. Both are correct ways to create a cache.

The choice really depends on your use case. Declarative way is the static way of defining your configuration. You can mention separate set of configuration for each distributed data structure you intend to use in your application. For e.g. the following snippet in your hazelcast.xml creates a IMAP with name simpleMapand the below mentioned configuration.

<map name="simpleMap">
    <backup-count>0</backup-count>
    <max-idle-seconds>0</max-idle-seconds>
    <eviction-policy>LRU</eviction-policy>
    <time-to-live-seconds>30</time-to-live-seconds>
    <max-size>3000</max-size>
    <eviction-percentage>30</eviction-percentage>
    <merge-policy>com.hazelcast.map.merge.PutIfAbsentMapMergePolicy</merge-policy>
</map>

You can also define your hazelcast XML configuration file from multiple XML configuration snippets. In order to do the same, you can use the <import/> element to load different XML configuration files.

The programmatic way is very useful if you need to define caches during runtime, ( for e.g. on the basis of some condition) and also for testing. You can view it as a solution to the static nature of hazelcast.xml.

For each tag in the hazecast xml, you can find its programmatic equivalent in the Config object. Summarizing, if you need dynamic capabilities (add/modify configurations), go for programmatic way. If you are very sure that nothing needs to be done during runtime, you may use declarative way.