Using Spring to populate Hazelcast Map at Start-up

The application I work on has been using Hazelcast for awhile now, but today we had a new scenario where we wanted to initially populate the Hazelcast Map from a relational database but did not want the our Map to be backed by a data store.  We wanted our Map to continue to only be in memory (for performance reasons).

As a relatively “newbie” to Hazelcast, I initially focused on the MapLoader interface, but as the documentation on that interface explains, it is used when the Map is backed by a data store.

We ended up using Spring to populate our Map by using the init method on our beans.

Here is the relevant code…

First, with our Hazelcast Map the keys will be of type String and the values will be of type ProcessStats.  The ProcessStats class:

public class ProcessStats implements Serializable {
  private static final long serialVersionUID = 1L;
  private String processName;
  private Long total;
  public ProcessStats(String processName, Long total) {
    this.processName = processName; total;

  // Getters and Setters left out for brevity


Finally, here is the Spring bean that we used to populate the Map.

public class HazelcastCacheLoader {
 HazelcastInstance hz;

 public void init() 
   IMap<String, Object> processStatsMap = hz.getMap("processStats");
   List<Proccess> processes = // Retrieve this list from the database
   for (Process proc : processes)
     String key = proc.getProcessID();
     String processStats = calcStatsForProcess(proc);
     processStatsMap.put(key, processStats);


About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Elasticsearch, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in Hazelcast, Spring and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s