com.hazelcast.core.MapLoader Interface provides very important flexibility for data access. Distributed map implementation is an in-memory data store but it can be backed by any type of data store such as RDBMS, OODBMS, or simply a file based data store.

IMap.get(key) normally returns the value that is available in-memory. However, if the entry doesn’t exist in-memory, Hazelcast returns null. If a Loader implementation is provided then, instead of returning null, Hazelcast will load the unknown entry by calling the implementation’s load (key) or loadAll(keys) methods. Loaded entries will be placed into the distributed map and they will stay in-memory until they are explicitly removed or implicitly evicted (if eviction is configured).

V load(K key) : Loads the value of a given key. If distributed map doesn’t contain the value for the given key then Hazelcast will call implementation’s load (key) method
to obtain the value. Implementation can use any means of loading the given key such as an ORM framework, simple SQL or reading a file etc.

Map<K, V> loadAll(Collection<K> keys) : Loads given keys. This is batch load operation so that implementation can optimize the multiple loads.

Set<K> loadAllKeys() : Loads all of the keys from the store. This method is used for pre-populating the in-memory map when the map is first touched/used. If MapLoader.loadAllKeys returns NULL then nothing will be loaded. Your MapLoader.loadAllKeys implementation can return all or some of the keys. You may select and return only the hot keys, for instance. Also note that this is the fastest way of pre-populating the map as Hazelcast will optimize the loading process by having each node loading owned portion of the entries.

MapLoader initialization flow is as the following :

1- When getMap() first called from any node, initialization starts
2- Hazelcast will call MapLoader.loadAllKeys() to get all your keys on each node
3- Each node will figure out the list of keys it owns
4- Each node will load all its owned keys by calling MapLoader.loadAll(keys)
5- Each node puts its owned entries into the map by calling IMap.putTransient(key,value)

This article shows how to implement Hazelcast MapLoader Feature.

Used Technologies :

JDK 1.7.0_40
Spring 3.2.4
Hazelcast 3.0
Hibernate 4.1.8
Tomcat-Jdbc 7.0.27
MySQL Connector 5.1.17
Maven 3.0.4

STEP 1 : CREATE MAVEN PROJECT

A maven project is created as below. (It can be created by using Maven or IDE Plug-in).

DATA4J_Spring_Hazelcast_MapLoader

STEP 2 : DEPENDENCIES

Firstly, dependencies are added to Maven’ s pom.xml.

STEP 3 : CREATE DATABASE INSTALLATION

A new Customer Table should be created and customer records should be inserted.

STEP 4 : CREATE CUSTOMER ENTITY

A new Customer Entity is created. It will be distributed between two nodes in cluster.

A new Address Entity is created.

STEP 5 : CREATE ICustomerDAO INTERFACE

A new ICustomerDAO Interface is created.

STEP 6 : CREATE CustomerDAO IMPL

A new CustomerDAO is created for data-access.

STEP 7 : CREATE ApplicationContextUtil CLASS

A new ApplicationContextUtil class is created to provide access to application context.

STEP 8 : CREATE CustomerEntryListener CLASS

A new CustomerEntryListener class is created. This listener receives distributed customerMap events.

STEP 9 : CREATE IDistributedMapService INTERFACE

A new IDistributedMultiMapService Interface is created for service layer to expose cache functionality.

STEP 10 : CREATE DistributedMapService IMPL

DistributedMapService is created by implementing IDistributedMapService.

STEP 11 : CREATE CustomerMapLoader CLASS

CustomerMapLoader Class is created to load the cache that doesn’t contain the value for the given key.

STEP 12 : CREATE hazelcast-config.properties

hazelcast-config.properties file is created. It covers Hazelcast cluster’ s nodes configuration.

STEP 13 : CREATE jdbc.properties

jdbc.properties file is created to define database connection pool configuration and access database.

STEP 14 : CREATE applicationContext-hazelcast.xml

applicationContext-hazelcast.xml is created. It defines customerMap configuration via Hazelcast Spring Integration.

STEP 15 : CREATE dataContext.xml

dataContext.xml file is created. It defines data access configuration.

STEP 16 : CREATE applicationContext.xml

applicationContext.xml is created for Spring DI.

STEP 17 : CREATE DistributedMapDemonstrator CLASS for First Node

DistributedMapDemonstrator Class demonstrates Customers via customerMap. Firstly, customerMap is empty so when first and second customers are tried to get via first node, a database query has to be fired and these customers are put to customerMap via first node by querying database. When second node tries to get first and second customers, they will return via customerMap so there will no database operation at second node.

DistributedMapDemonstrator Class for Second Node :

STEP 18 : CREATE APPLICATION CLASS

Application class is created to run the application.

STEP 19 : BUILD PROJECT

After DATA4J_Spring_Hazelcast_MapLoader Project is build, data4j-spring-hazelcast-maploader-0.0.1-SNAPSHOT.jar will be created.

STEP 20 : RUN PROJECT ON THE MEMBERs OF CLUSTER

After created data4j-spring-hazelcast-maploader-0.0.1-SNAPSHOT.jar file is run at the members of the cluster, the following output logs will be shown on first member’ s console :

Second member’ s console logs :

First and second entries are queried via customerMap. First node has already put them to customerMap. Third and fourth entries are queried via database because they do not exist customerMap initially. After then they are put to customerMap.

STEP 21 : SOURCE CODEs

DATA4J_Spring_Hazelcast_MapLoader

REFERENCES

MapLoader Documentation
MapLoader API Documentation