Вопрос

In my Java-Spring based web app I'm connecting to Cassandra DB using Hector and Spring.

The connection works just fine but I would like to be able to test the connection.

So if I intentionally provide a wrong host to CassandraHostConfigurator I get an error:

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

Which is ok of course. But how can I test this connection?

If I define the connection pragmatically (and not via spring context) it is clear, but via spring context it is not really clear how to test it.

can you think of an idea?

Это было полезно?

Решение

Since I could not come up nor find a satisfying answer I decided to define my connection pragmatically and to use a simple query:

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");
}

And my test checks that the returned object in not null.

Another way that works fine:

public boolean isConnected() {
List<KeyspaceDefinition> keyspaces = null;
try {
    keyspaces = cluster.describeKeyspaces();
} catch (HectorException e) {
    return false;
}
return (!CollectionUtils.isEmpty(keyspaces));
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top