1

我正在通过Hector连接到Cassandra db

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

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

如您所见,我正在触发一个连接事件,但无论如何我都会到达那里 - 无论连接成功还是失败,我都无法区分两者。是否有一些我可以捕捉到的事件或任何其他方式可以通知我连接状态?

4

1 回答 1

0

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!!!");
    }
于 2013-08-11T06:13:43.210 回答