2016-12-04 85 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