HConnectionManager(Hector)连接状态

时间:2013-08-08 06:22:19

标签: cassandra connection-pooling hector

我通过Cassandra db连接到Hector

当给出错误的套接字(故意)时,连接失败并显示消息:

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

它每隔几秒钟继续尝试java.net.ConnectException: Connection timed out: connect。 我想知道连接是否失败或成功,因为一旦成功,我想采取一些行动。

我的代码如下所示:

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

正如你所看到的那样,我发起了一个连接事件,但无论如何我都会到达那里 - 无论连接是成功还是失败,我都无法区分这两者。 是否有一些我可以捕获的事件或任何其他方式我可以被告知连接状态?

1 个答案:

答案 0 :(得分:0)

我不确定这是最佳方式,但由于我找不到更好的答案,这就是我所做的:

创建此方法:

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

然后:

    if (isConnected())
    fireConnectionEvent(true);
    else {
    LOG.error("Could not connect to socket " + this.socket + ". Connection to Cassandra is down!!!");
    }