Changeset 31644

Show
Ignore:
Timestamp:
03.05.2017 17:36:02 (2 years ago)
Author:
ak19
Message:

Learnt about shutdown hooks in Java. Adding one to GLI (Gatherer) to issue the ant call to stop the GS3 server (tomcat and derby) on unexpected termination of GLI, such as Ctrl-C.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r31636 r31644  
    388388                    GS3ServerThread thread = new GS3ServerThread(gsdl3_src_path, "restart"); 
    389389                    thread.start(); 
     390     
     391                    // The Java virtual machine shuts down in response to two kinds of events: 
     392                    // - The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or 
     393                    // - The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown. 
     394                    // https://coderanch.com/t/328888/java/Killing-process-spawned-Runtime-exec 
     395                    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 
     396                        public void run() {                      
     397 
     398                            if(Gatherer.exit != true) { // unexpected termination, such as Ctrl-C won't set Gatherer.exit 
     399                            // so still need to at least issue the ant call to stop any running GS3 
     400                            // (tomcat and networked Derby Server) by calling GS3ServerThread.stopServer() 
     401                            System.err.println("ShutDownHook called..."); 
     402                            System.err.println("Attempting to forcibly terminate the GS3 server..."); 
     403                            GS3ServerThread.stopServer(); 
     404                            } 
     405                        } 
     406                        })); 
     407                     
     408 
    390409 
    391410                    // If fedora installed inside this local GS3, then ask for fedora login details now.