How do I inject a HazelcastInstance to a MapStoreFactory

240 views
Skip to first unread message

Sutanu Dalui

unread,
Jul 11, 2017, 4:40:18 AM7/11/17
to Hazelcast
Hi,

I am using a MapStore backed implementation. I would want to invoke the init(HazelcastInstance hazelcastInstance, Properties properties, String mapName) method of the MapStore to inject the Hz instance.

I tried making the MapStoreFactory implement HazelcastInstanceAware, however that did not work. I need the hazelcastInstance since I want to make my MapStore, a MapListener as well. Is this something achievable?

Thanks,
Sutanu

Sutanu Dalui

unread,
Jul 11, 2017, 5:02:48 AM7/11/17
to Hazelcast
Kindly disregard the question. Removing the init() invocation from the Factory makes it work.

Viktor Gamov

unread,
Jul 11, 2017, 9:02:51 AM7/11/17
to Hazelcast
Sutanu,

You don't need to inject HazelcastInstance to MapStoreFactory.
Rather you  need to implement MapLoaderLyfesycleSupport in your MapLoader/Store
You don't need to invoke MapLoaderLyfesycleSupport.init manually.
Hazelcast will invoke it during construction of Map and pass appropriate HazelcastInstance, map name and properties.

Storing Entries to Multiple Maps

A configuration can be applied to more than one map using wildcards (see Using Wildcards), meaning that the configuration is shared among the maps. But MapStore does not know which entries to store when there is one configuration applied to multiple maps.

To store entries when there is one configuration applied to multiple maps, use Hazelcast's MapStoreFactoryinterface. Using the MapStoreFactory interface, MapStores for each map can be created when a wildcard configuration is used. Example code is shown below.

Config config = new Config();
MapConfig mapConfig = config.getMapConfig( "*" );
MapStoreConfig mapStoreConfig = mapConfig.getMapStoreConfig();
mapStoreConfig.setFactoryImplementation( new MapStoreFactory<Object, Object>() {
  @Override
  public MapLoader<Object, Object> newMapStore( String mapName, Properties properties ) {
    return null;
  }
});

To initialize the MapLoader implementation with the given map name, configuration properties, and the Hazelcast instance, implement the MapLoaderLifecycleSupport interface. This interface has the methods init() and destroy() as shown below.

public interface MapLoaderLifecycleSupport {

  void init( HazelcastInstance hazelcastInstance, Properties properties, String mapName );

  void destroy();
}

The method init() initializes the MapLoader implementation. Hazelcast calls this method when the map is first used on the Hazelcast instance. The MapLoader implementation can initialize the required resources for implementing MapLoader such as reading a configuration file or creating a database connection.

Hazelcast calls the method destroy() before shutting down. You can override this method to cleanup the resources held by this MapLoader implementation, such as closing the database connections.


Here's an example that may give you an idea

Thank you
Reply all
Reply to author
Forward
0 new messages