2

I am currently working with eXist-db, and what I want to accomplish is executing command line script to start eXist-db (/bin/startup.sh) wait for it to create database so I can get collection from it.

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

I want to wait with the getCollection until database is created (can be called) , or after certain amount of waiting time if the database doesn't initialise I would like to kill it (lets say one minute at most). What is the best solution for this problem? Using sleep/wait several times and trying to call database? Something like this?

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

And as to killing part, I would like to confirm the assumption that storing the process and killing it using Process.destroy() function should be enough (basing it on assumption that the script for database is taking too long, in normal runtime, at the end of my application I would use provided eXist-db script /bin/shutdown.sh).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zerg Overmind
  • 955
  • 2
  • 14
  • 28
  • Check the process ID or the port seems reasonable to me. Depending on how far you want to go with a "health check", verify that you can login, even – OneCricketeer Apr 29 '18 at 18:19
  • 1
    I would prefer calling for collection, as this way i can later on add checks if for example the username and password was not denied and in such case request new account information (currently i am checking only for existance of the database and stopping on any other error). – Zerg Overmind Apr 29 '18 at 18:22

1 Answers1

1

Rather than using startup.sh, if you are running in embedded mode, then you can use ExistEmbeddedServer (or it might be called EmbeddedExistServer, sorry I am away from my computer for a few days) class from the test package instead.

I don't think you can use startup.sh directly for your purpose, as it creates a foreground process. Instead you should start eXist from your Java application as described above.

adamretter
  • 3,885
  • 2
  • 23
  • 43
  • I am not running it in embedded mode as i encountered some problems while trying to follow the documentation which i do not know how to fix, see https://stackoverflow.com/questions/50067168/problems-attempting-to-embed-exist-db/50067876 which is why i resorted to attempting to use it like this – Zerg Overmind May 01 '18 at 10:31