如何等待另一个应用程序 (eXist-db) 启动(使用 java),然后再与之交互?

How to wait for another application (eXist-db) to start (using java), before interacting with it?

我目前正在使用 eXist-db,我想要完成的是执行命令行脚本来启动 eXist-db (/bin/startup.sh) 等待它创建数据库以便我可以收集从中。

    //start database
    try {
        Runtime.getRuntime().exec(path + start);
    } catch (IOException ex) {
        return false;
    }
    //get collection
    col = DatabaseManager.getCollection(URI + "/db", username, password);

我想用 getCollection 等待直到创建数据库(可以调用),或者在一定的等待时间后如果数据库没有初始化我想终止它(假设最多一分钟) .这个问题的最佳解决方案是什么?多次使用 sleep/wait 并尝试调用数据库?是这样的吗?

    Process pr = null;
    try {
        pr = Runtime.getRuntime().exec(path + start);
    } catch (IOException ex) {
        return false;
    }

    for (int i = 0; i < 60; i++) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            pr.destroy();
            return false;
        }
        try {
            dbDetected = initCollection();
        } catch (XMLDBException ex) {
            if (ex.errorCode != ErrorCodes.VENDOR_ERROR  ||
                    "Failed to read server's response: Connection refused (Connection refused))"
                            .compareTo(ex.getMessage()) != 0 ) {
                pr.destroy();
                return false;
            }
        }

关于终止部分,我想确认存储进程并使用 Process.destroy() 函数终止它应该足够的假设(基于数据库脚本花费太长时间的假设,在正常运行时,在我的应用程序结束时,我会使用提供的 eXist-db 脚本 /bin/shutdown.sh).

而不是使用 startup.sh,如果你是 运行 嵌入式模式,那么你可以使用 ExistEmbeddedServer(或者它可能被称为 EmbeddedExistServer,抱歉我几天不在电脑前) class 来自测试包。

我认为您不能直接将 startup.sh 用于您的目的,因为它会创建一个前台进程。相反,您应该如上所述从 Java 应用程序启动 eXist。