Apache HazelCast – Distributed Cache

Apache HazelCast – Distributed Cache


This blog deals with HazelCast using distributed cache. The Hazelcast component is an in-memory data grid, entirely written in Java.Here we are going to load and retrieve the keys and values to/from distributed collections (IList, ISet, and IMap) of HazelCast using java program instead of the standard Java collection. HazelCast provides Java Standard Collections, Map, List, and Set with features like ‘Add Listener’ for storing data, distributed into various formats. Hazelcast Set/List/Map uses Listener to “listen” to, or notify users of events which occur, when items are added and removed.

Please refer to our previous blog on “Apache HazelCast – Management Center“.

Use Case

Let’s have a use case to load and retrieve the sample data into Collections List, Set and Map from different JVMs. This will help us understand how cached data is distributed among multiple instances.

What we need to do:

  • Prerequisites
  • We have a set of Java programs to perform the following operations:
    • Load sample data into the HazelCast Cache system
    • Retrieve cached data from HazelCast Cache system
    • Add Listeners to IMap, ISet, IList
  • Execution of the Java programs.


Follow the sections given below from the previous blog “Apache HazelCast – Management Center” to download, install Hazelcast and to start HazelCast nodes, in order to perform this use case.


  • Install JDK 1.6+
  • Make sure HazelCast nodes are running
  • Configure below two jars into classpath:
  • hazelcast-3.x.x.jar
  • hazelcast-client-3.x.x.jar

Load sample data to HazelCast Cache system

CacheLoader.java: This Java class is used to load the data into IList, ISet, and IMap.

Retrieve Cached data from HazelCast Cache system

  • CacheRetrival.java: This Java class is used to retrieve and process the existing/cached data in IList, ISet, and IMap in HazelCast Cache.

Add Listeners to IMap, ISet, IList

  • We need to add Listeners to Hazelcast ISet / IList / IMap, to make note of or ‘listen’ to an entry that is being added /removed /updated /evicted from a map. For this, first run the below given CacheListener.java script, and then execute CacheLoader.java. Make sure this program is running, whenever data is being added.
  • CacheListener.java: This class helps to add listeners to Distributed List, Set, and Map.

Note: Don’t shutdown the Hazelcast instance for this program; otherwise, the Listeners get killed.

  • MapEntryListener.java: This listener class helps to make note of or ‘listen’ to an entry being added/removed/updated/evicted from   map, if needed.
  • ListListener.java: This listener class helps to ‘listen’, whenever an item is added or removed from a list or set, if needed.

Execution of programs

Create three jar files for the three java classes given below:

  • com.treselle.hazel.distributedcache.CacheLoader (CacheLoader.jar)
  • com.treselle.hazel.distributedcache.CacheRetrival (CacheRetrival.jar)
  • com.treselle.hazel.distributedcache.CacheListener (CacheListener.jar)

Choose three JVM machines or Open three Command prompts:

  •  First start the CacheListener.jar in one command prompt/JVM machine.
  • Run the other two programs in other prompts/JVM, like the above command.

Note: If CacheListener.jar is running while the elements are being added to the list, map and set, stat details (like source, event type) of each of the elements will also be logged.

  • After running the CacheLoader.jar, Open the Hazelcast management center (http:{tomcat_server_ip}:8080/mancenter-3.2.6/main.jsp), the map1 map and its entries can be added as given below:
  • Note: Red highlight indicates real entries. Green highlight indicates backup of real entries.


  • Hazelcast acts as an in-memory data grid – distributing data across cheap, commodity hardware with an open-source infrastructure.
  • Apache HazelCast is capable for maintaining the cache data among all instances of application. This will enable the retrieval of data from new instance that is stored in another instance.
  • The last part of this series will focus on HazelCast using Distributed Topic where we are going to publish/subscribe messages (objects).

This blog series is broken down into the following three-part posts:


5395 Views 10 Views Today