I need to pass (inject) already created object into Hazelcast MapStore. In the example (from Hazelcast documentation) below shown how to pass properties into MapStore from xml configuration and create new object using these properties inside store init method.
But how to pass already constructed connection object in Vert.x? As it is not Spring project, I can't use @SpringAware and bean injection.
<hazelcast>
...
<map name="supplements">
<map-store enabled="true" initial-mode="LAZY">
<class-name>com.hazelcast.loader.YourMapStoreImplementation</class-name>
<properties>
<property name="mongo.url">mongodb://localhost:27017</property>
<property name="mongo.db">mydb</property>
<property name="mongo.collection">supplements</property>
</properties>
</map-store>
</map>
...
</hazelcast>
After specifying the database properties in your configuration, you need to implement the MapLoaderLifecycleSupport interface and give those properties in the init() method, as shown below:
public class YourMapStoreImplementation implements MapStore<String, Supplement>, MapLoaderLifecycleSupport {
private MongoClient mongoClient;
private MongoCollection collection;
public YourMapStoreImplementation() {
}
@Override
public void init(HazelcastInstance hazelcastInstance, Properties properties, String mapName) {
String mongoUrl = (String) properties.get("mongo.url");
String dbName = (String) properties.get("mongo.db");
String collectionName = (String) properties.get("mongo.collection");
this.mongoClient = new MongoClient(new MongoClientURI(mongoUrl));
this.collection = mongoClient.getDatabase(dbName).getCollection(collectionName);
}
When creating the member you can do something like this
Config config = ...;
config.getMapConfig(MAP_NAME)
.getMapStoreConfig().setEnabled(true)
.setImplementation(new MyMapStore<>());
You can use setImplementation() to pass an object. The MyMapStore will contain the connection object