@Override public void run() { Thread.currentThread().setName("DaemonShutdownThread"); debug.fatal( "**************************************************************************************"); debug.fatal(" JVM IS SHUTTING DOWN. KILLING CHILD PROCESS IF IT IS STILL ALIVE."); if (processThread != null && processThread.isAlive()) { debug.fatal("Process is still running. Shutting it down."); processThread.destroy(); } debug.fatal( "**************************************************************************************"); }
private void start(final String args[]) { Thread.currentThread().setName("daemon_main"); shutdownThread = new Daemon.DaemonShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownThread); // To make sure the // child process is // killed on JVM-kill connectionThread = new Daemon.ManagerConnectionThread(); connectionThread.start(); processThread = new Daemon.ProcessThread(args); shutdownThread.setProcessThread(processThread); processThread.start(); String oldServerBuildNumber = connectionThread.serverBuildNumber; while (true) { if (restartOnBuildNumberChange && !oldServerBuildNumber.equals(connectionThread.serverBuildNumber)) { debug.info( "**************************************************************************************"); debug.info( "**************************************************************************************"); debug.info( "Server has restarted and has a different build number... Restarting our child process."); if (processThread.isAlive()) { debug.info("Process is still running. Shutting it down."); processThread.destroy(); try { Thread.sleep(5000); } catch (final InterruptedException ignored) { } // Let the process shut down... } debug.info( "**************************************************************************************"); debug.info( "**************************************************************************************\n\n"); if (jarSource != null && jarTarget != null) { try { debug.info("Copying newer jarfile '" + jarSource + "' to '" + jarTarget + "'"); FileUtil.copy(new File(jarSource), new File(jarTarget), true); debug.info("Jar copied without error."); debug.debug("Asking the WrapperManager to restart the JVM... "); requestRestart(); } catch (final IOException ex) { debug.error("Exception copying jar.", ex); } } else { debug.debug("Jar source and target were not specified. Jar copy skipped."); } processThread = new Daemon.ProcessThread(args); shutdownThread.setProcessThread(processThread); processThread.start(); oldServerBuildNumber = connectionThread.serverBuildNumber; } try { Thread.sleep(5000); } catch (final InterruptedException ignored) { } } }
/** Spawns a new process using args as it's commandline options and then watches it. */ private void runAsProcess() { BufferedInputStream inStream = null; BufferedInputStream errStream = null; try { debug.debug( "**************************************************************************************"); debug.debug("Starting child process..."); for (int i = 0; i < args.length; i++) { debug.debug("args[" + i + "]='" + args[i] + "'"); } debug.debug( "**************************************************************************************"); proc = (Runtime.getRuntime()).exec(args); inStream = new BufferedInputStream(proc.getInputStream()); errStream = new BufferedInputStream(proc.getErrorStream()); int exitValue = 0; while (true) { while (inStream.available() > 0) { handleCharacter(inStream.read()); } while (errStream.available() > 0) { handleCharacter(errStream.read()); } try { exitValue = proc.exitValue(); break; // If we got here, the process has exited } catch (final IllegalThreadStateException ignored) { } Thread.sleep(100); } debug.debug("Process has exited, exitValue=" + exitValue); if (exitValue != 0) { debug.warn("Process exited with a non-zero exit value. "); debug.warn("Waiting 30 seconds to make sure the child process has fully exited."); try { Thread.sleep(30000); } catch (final InterruptedException ignored) { } debug.warn("Asking the WrapperManager to restart the JVM... "); requestRestart(); } } catch (final IOException ex) { debug.error("IOException during process exec.", ex); } catch (final InterruptedException ex) { debug.info( "InterruptedException caught during process exec. Assuming our container has told us to quit."); } destroy(); try { if (inStream != null) { inStream.close(); } inStream = null; if (errStream != null) { errStream.close(); } errStream = null; } catch (final IOException ignored) { } debug.info("ProcessThread ending normally."); }