2016-12-04 84 views
0

我想写我自己MapStore将访问卡桑德拉。传递参数给Hazelcast MapStore

我想能够传递参数如卡桑德拉的地址,我怎么能做到这一点,假设我可以使用构造函数。

我使用的是Dropwizard,特别是dropwizard-cassandra库。

+0

亲爱danieln,请查看我的回答并标记它解决它是否适合你。谢谢 –

+0

我最终做的是实现''MapStoreFactory'',所以我可以传递参数到''MapStore''构造函数,如下所示: ''mapStoreConfig.setFactoryImplementation((MapStoreFactory )( MAPNAME,属性) - > {如果(mapName.equals( “X”)){返回mapStore;}返回NULL;}); '' – danieln

+0

亲切,如果你满意接受的答案。谢谢 –

回答

2

@danieln

@noctarius显示,hazelcast.xml指定属性的声明方式。

MapLoader没有办法将该属性注入到实例中。

要做到这一点,您需要实现MapLoaderLifecycleSupport接口。 属性将我注入init()方法

public interface MapLoaderLifecycleSupport { 
/** 
* Initializes this MapLoader implementation. Hazelcast will call 
* this method when the map is first used on the 
* HazelcastInstance. Implementation can 
* initialize required resources for the implementing 
* mapLoader, such as reading a config file and/or creating a 
* database connection. References to maps, other than the one on which 
* this {@code MapLoader} is configured, can be obtained from the 
* {@code hazelcastInstance} in this method's implementation. 
* <p> 
* On members joining a cluster, this method is executed during finalization 
* of the join operation, therefore care should be taken to adhere to the 
* rules for {@link com.hazelcast.spi.PostJoinAwareService#getPostJoinOperation()}. 
* If the implementation executes operations which may wait on locks or otherwise 
* block (e.g. waiting for network operations), this may result in a time-out and 
* obstruct the new member from joining the cluster. If blocking operations are 
* required for initialization of the {@code MapLoader}, consider deferring them 
* with a lazy initialization scheme. 
* </p> 
* 
* @param hazelcastInstance HazelcastInstance of this mapLoader. 
* @param properties  Properties set for this mapStore. see MapStoreConfig 
* @param mapName   name of the map. 
*/ 
void init(HazelcastInstance hazelcastInstance, Properties properties, String mapName); 

/** 
* Hazelcast will call this method before shutting down. 
* This method can be overridden to clean up the resources 
* held by this map loader implementation, such as closing the 
* database connections, etc. 
*/ 
void destroy(); 
} 

我们没有卡桑德拉的例子,但我们确实有在您的处置蒙戈例如here。本实施例说明使属性,以装载机的方法。

此外,我们很乐意接受卡桑德拉的例子为hazelcast-code-samples,如果您愿意捐助一个。

如果您有任何问题,请在下面的评论中告诉我。

谢谢

维克

+0

哦很好的补充,谢谢:) – noctarius