I'm connecting to Cassandra db via Hector.
When given a wrong socket (in purpose) the connection fails with the message:
INFO CassandraHostRetryService - Downed Host Retry service started with queue size -1 and retry delay 10s
ERROR HConnectionManager - Could not start connection pool for host 132.202.35.14(160.202.35.14):9160
INFO CassandraHostRetryService - Host detected as down was added to retry queue: 132.202.35.14(160.202.35.14):9160
INFO JmxMonitor - Registering JMX me.prettyprint.cassandra.service_RTBCluster:ServiceType=hector,MonitorType=hector
And it keeps on trying with java.net.ConnectException: Connection timed out: connect
every few seconds.
I would like to know if the connection had failed or succeeded since once it succeeds I would like to take some actions.
My code looks like this:
CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator(this.socket);
cluster = new ThriftCluster(this.clusterName, cassandraHostConfigurator);
ConfigurableConsistencyLevel consistencyLevelPolicy = new ConfigurableConsistencyLevel();
consistencyLevelPolicy.setDefaultReadConsistencyLevel(getConsistencyLevelPolicy());
keyspace = HFactory.createKeyspace(keyspaceName, cluster, consistencyLevelPolicy);
fireConnectionEvent(true);
As you can see I'm firing a connection event, but I'm getting there anyways - whether connection succeeded or fails and I have no way to distinguish between the two. Is there some event I can catch or any other way I can be informed of the connection state?
I'm not sure it's the optimal way, but since I could not find a better answer this is what I did:
Created this method:
private boolean isConnected() {
List<KeyspaceDefinition> keyspaces = null;
try {
keyspaces = cluster.describeKeyspaces();
} catch (HectorException e) {
return false;
}
return (!CollectionUtils.isEmpty(keyspaces));
}
and then:
if (isConnected())
fireConnectionEvent(true);
else {
LOG.error("Could not connect to socket " + this.socket + ". Connection to Cassandra is down!!!");
}