Hazelcast’s MapStore to be defined as object not as class

Hazelcast provides a so-called MapStore interface that can be used to persist the contents of a distributed map in a database. When you configure Hazelcast by an XML file, you have to add something like this to activate the MapStore:

1
2
3
4
<map-store enabled="true">
   <class-name>com.foo.MapStoreImplementation</class-name>
   <write-delay-seconds>10</write-delay-seconds>
</map-store>

But, what if your MapStore implementation needs to be configured on the object level before it can be used. In my case, I wanted to inject a pre-configured MySQL persistency layer into the MapStore implementation. In this case, you can use method setImplementation of class MapStoreConfig as shown in the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
final Config config = new Config( );
final MapConfig mapCfg = new MapConfig( );
mapCfg.setName( "MapName" );
mapCfg.setBackupCount( 1 );
 
final MapStoreConfig mapStoreCfg = new MapStoreConfig( );
 
/* Here, you define the MapStore implementation. */
mapStoreCfg.setImplementation( mapStoreImpl );
 
mapStoreCfg.setWriteDelaySeconds( 0 );
mapStoreCfg.setEnabled( true );
mapCfg.setMapStoreConfig( mapStoreCfg );
config.addMapConfig( mapCfg );
 
hzcast = Hazelcast.newHazelcastInstance( config );