Create and populate Hazelcast map on node startup

1.7k views Asked by At

Is there a way to execute a code fragment on hazelcast (version 3.4) node after node has started?

The idea is : once one of hazelcast nodes are started to create map (not waiting for .getMap to be executed on client side) and popualte with data from database, so that when client requests data, map is already populated.

2

There are 2 answers

0
arvist On BEST ANSWER

This can be solved by defining custom service in hazelcast.xml. The service constructor is invoked on nodes start up and from there it's possible to request and populate hazelcast map. But the invocation of getMap should be done in new Thread

2
A.K.Desai On

Yes. You need to implement the MapLoader for your IMap and then make use of MapLoader.loadAllKeys() method to initialize the map with hot keys.

This is what the documentation says:

You can use the MapLoader.loadAllKeys API to pre-populate 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. For example, you may select and return only the hot keys. MapLoader.loadAllKeys is the fastest way of pre-populating the map since Hazelcast will optimize the loading process by having each node loading its owned portion of the entries.

3.5 Map Persistence Documentation Link