1

我在 eclipse 中有一个代码,我已经成功建立了 cassandra 连接。连接成功完成,但我看到 eclipse 中的红色大按钮仍然亮着,即过程尚未完成。我想像执行普通代码一样关闭红色按钮。这是我的代码...

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;

public class MyConnection{

private Cluster cluster;

public void connect(String node) {
    cluster = Cluster.builder().addContactPoint(node).build();
    Metadata metadata = cluster.getMetadata();
    System.out.println("Cassandra connection established");
    System.out.printf("Connected to cluster: %s\n",
            metadata.getClusterName());
    for (Host host : metadata.getAllHosts()) {
        System.out.printf("Datatacenter: %s; Host: %s; Rack: %s \n",
                host.getDatacenter(), host.getAddress(), host.getRack());

    }
}

public void close() {
    cluster.shutdown();
}

public static void main(String[] args) {
    MyConnection c = new MyConnection();
    c.connect("127.0.0.1");
    c.close();

}

}

这是代码。有人请帮助..

4

2 回答 2

0

不太确定 eclips 在做什么,但在 intelliJ 中运行了相同的代码(稍作修改)并且该过程自行完成,这是输出:

log4j:WARN No appenders could be found for logger (com.datastax.driver.core.ControlConnection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Cassandra connection established
Connected to cluster: Test Cluster
Datatacenter: datacenter1; Host: /127.0.0.1; Rack: rack1 

Process finished with exit code 0

然后我尝试了以下方法:

  • 在 cassandra 服务器关闭的情况下运行相同的代码
  • 在远程服务器上运行相同的代码
  • 在我没有连接的远程服务器上运行相同的代码(超时)

所有这三种情况都导致错误,但代码仍然自行退出。

cluster.shutdown();清理com.datastax.driver.core.Cluster对象使用的连接是正确的。使用System.exit(0)会杀死进程,但它被认为是不好的做法。此代码自行执行和完成。我的建议是您检查您的运行(执行)设置是否有任何可能与您的应用程序交互/干扰的内容。

于 2013-06-15T14:22:31.387 回答
-1

尝试:

System.exit(0);

这将关闭您的红色按钮:-)

于 2013-06-15T12:31:06.807 回答