2013-07-30 76 views
0

在我的基於Java-Spring的Web應用程序中,我使用Hector and Spring連接到Cassandra數據庫。測試Hector Spring Cassandra連接

連接工作正常,但我想能夠測試連接。

所以,如果我故意提供錯誤的主機CassandraHostConfigurator我得到一個錯誤:

ERROR connection.HConnectionManager: Could not start connection pool for host <myhost:myport> 

這是確定課程。但是我怎麼測試這個連接呢?

如果我實際上(而不是通過spring上下文)定義連接,那很明顯,但是通過spring上下文,它不太清楚如何測試它。

你能想出一個想法嗎?

回答

0

因爲我無法想出也沒有找到一個滿意的答案,我決定以務實的態度定義我的連接,並使用一個簡單的查詢:

private ColumnFamilyResult<String, String> readFromDb(Keyspace keyspace) { 
ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>(keyspace, tableName, StringSerializer.get(), 
    StringSerializer.get()); 
// It doesn't matter if the column actually exists or not since we only check the 
// connection. In case of connection failure an exception is thrown, 
// else something comes back. 
return template.queryColumns("some_column"); 
} 

而我的測試檢查,在不爲空返回的對象。

工作正常另一種方式:

public boolean isConnected() { 
List<KeyspaceDefinition> keyspaces = null; 
try { 
    keyspaces = cluster.describeKeyspaces(); 
} catch (HectorException e) { 
    return false; 
} 
return (!CollectionUtils.isEmpty(keyspaces)); 
}