Vipin CP
Vipin CP

Reputation: 3807

Load hazelcast imap on application startup

How can i load map in startup . I have below config in my hazelcast.xml file with inital mode eager. But still its loading only when map is first accessed.

  <map name="cpMap">
        <map-store enabled="true" initial-mode="EAGER">
            <class-name>com.hazelcast.samples.spring.data.migration.CPLoader</class-name>
        </map-store>
    </map>

ALso I have the map loader implementation beans as well. I am using spring for this.

Upvotes: 0

Views: 903

Answers (2)

İlker Korkut
İlker Korkut

Reputation: 3250

You can call a map after application ready or any Spring context change event via Spring's EventListener as follow.

 @EventListener(ApplicationReadyEvent.class)
 public void myAppReadyMethod() {
        // Call any map
        hz.getMap("myMap"); // Assumed hazelcast instance is already injected
 }

You may take a look, Spring application javadocs, context events javadocs.

Upvotes: 1

Nicolas
Nicolas

Reputation: 1186

The documentation is pretty clear on that:

When getMap() is first called from any member, initialization starts depending on the value of InitialLoadMode. If it is set to EAGER, initialization starts on all partitions as soon as the map is touched, i.e., all partitions are loaded when getMap is called.

Hence, the map should be accessed for the map to start populating itself. If you're using Spring, I'd recommend a CommandLineRunner bean to access the map and start the process.

Upvotes: 1

Related Questions