Over a million developers have joined DZone.

Configuring Hazelcast within Spring Context

DZone's Guide to

Configuring Hazelcast within Spring Context

· Database Zone ·
Free Resource

Built by the engineers behind Netezza and the technology behind Amazon Redshift, AnzoGraph is a native, Massively Parallel Processing (MPP) distributed Graph OLAP (GOLAP) database that executes queries more than 100x faster than other vendors.  

At first we need to declare an instance of Hazelcast within the Spring context using default spring bean namespace. To integrate Hazelcast with Spring we need either hazelcast-spring.jar or hazelcast-all.jar in the classpath. 

<bean id="instance" class="com.hazelcast.core.Hazelcast" factory-method="newHazelcastInstance">
        <bean class="com.hazelcast.config.Config">
            <property name="groupConfig">
                <bean class="com.hazelcast.config.GroupConfig">
                    <property name="name" value="dev"/>
                    <property name="password" value="pwd"/>
            <!-- and so on ... -->

<bean id="map" factory-bean="instance" factory-method="getMap">
    <constructor-arg value="map"/>

We need Spring version 2.5 or greater for hazelcast integration. Since hazelcast version 1.9.1, we have hazelcast namespace to configure in the Spring context file. Namespace (hz) given below.

<beans xmlns="http://www.springframework.org/schema/beans"

After configuring the hazelcast instance in spring context file, we need to create  hazelcast.xml file which will be placed in the classpath. Sample file given below.

<?xml version="1.0" encoding="UTF-8"?>

<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.3.xsd"
    <management-center enabled="true">http://localhost:8080/mancenter-3.3.3</management-center>
        <port auto-increment="true" port-count="100">5701</port>
            Allowed port range when connecting to other nodes.
            0 or * means use system provided port.
            <multicast enabled="true">
            <tcp-ip enabled="false">

    <partition-group enabled="false"/>

    <map name="CustomerMap">







        <max-size policy="PER_NODE">0</max-size>





    <services enable-defaults="true"/>


Here we declared a hazelcast map called ‘CustomerMap’. This map can be accessed in the application and can be used to store key value pairs. Some properties of hazelcast map are given below.

  • Backup-count  - Number of backups. If 1 is set as the backup-count for example, then all entries of the map will be copied to another JVM for fail-safety. 0 means no backup

  • Time-to-live-seconds - Maximum number of seconds for each entry to stay in the map. Entries that are older than time-to-live-seconds and not updated for time-to-live-seconds will get automatically evicted from the map
  • Max-idle-seconds - Maximum number of seconds for each entry to stay idle in the map.
  • Eviction-policy  -  Different eviction policies are available: NONE (no eviction), LRU (Least Recently Used), LFU (Least Frequently Used). NONE is the default.
  • Max-size -  Maximum size of the map. When max size is reached,map is evicted based on the policy defined. Any integer between 0 and Integer.MAX_VALUE. 0 means Integer.MAX_VALUE. Default is 0.
  • Eviction-percentage - When max. size is reached, specified percentage of the map will be evicted. Any integer between 0 and 100. If 25 is set for example, 25% of the entries will get evicted.
  • Merge-policy – There are different merge policies.
    • PassThroughMergePolicy - Entry will be added if there is no existing entry for the key.
    • HigherHitsMapMergePolicy - entry with the higher hits wins.
    • LatestUpdateMapMergePolicy - entry with the latest update wins.
  • Min-eviction-check-millis - Minimum time in milliseconds which should pass before checking if a partition of this map is evictable or not. Default value is 100 millis

Accessing Hazelcast Map in the application

At first we need to get reference to the hazelcast instance we declared in the spring context file. Here we use autowiring to get the hazelcast instance. After that hazelcast instance is used to create an ‘IMap’. Different types of maps are available like IMap, ConcrrentMap, MultiMap etc.

IMap is Concurrent, distributed, observable and queryable map. IMap does not allow nulls to be used as keys or values. Code snippet given below.

import org.springframework.beans.factory.annotation.Autowired;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;

public class HazelcastTest {

private HazelcastInstance hazelcastInstance;

public void addCustomers() throws Exception {

IMap<Integer, String> map = hazelcastInstance.getMap("CustomerMap");

map.put(1001, "Tom");
map.put(1002, "Jim");
map.put(1003, "Joe");

Above explaination will help to configure hazelcast with Spring and use hazelcast  IMap to
 store data.

Download AnzoGraph now and find out for yourself why it is acknowledged as the most complete all-in-one data warehouse for BI style and graph analytics.  

java ,theory ,hazelcast ,imap

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}