If Quartz Scheduler dies, how to stop the Java child processes that it started?

I am currently using Quartz Scheduler to replace Cron on our Windows Server Server Box. I have two specific jobs that needed to be run in a new virtual machine, so I use the ProcessBuilder object in Java 5 to get my Process object. The problem I am facing is when the JVM Quartz Scheduler stops, 2 jobs in a separate JVM keep going.

        Process process = Runtime.getRuntime().exec(command);
        try
        {
            while (true)
            {

                Thread thread1 = new Thread(new ReaderThread(process.getInputStream()));
                Thread thread2 = new Thread(new ReaderThread(process.getErrorStream()));

                thread1.start();
                thread2.start();

                thread1.join();
                thread2.join();

Is there a way to kill these threads when the parent JVM associated with my quartz scheduler dies? Even if I knew about a way to kill them from another process manually, I could figure out how to do this through Quartz.

Thank you in advance

+3
2

Quartz JVM , finally. . JVM. javadocs Runtime,

, , - .

( - )

    private static final long TIMEOUT_MS = 60000;
    Process process = Runtime.getRuntime().exec(command);
    try
    {
        while (true)
        {

            Thread thread1 = new Thread(new ReaderThread(process.getInputStream()));
            Thread thread2 = new Thread(new ReaderThread(process.getErrorStream()));

            thread1.start();
            thread2.start();

            process.waitFor();
            thread1.join(TIMEOUT_MS);
            thread2.join(TIMEOUT_MS);
            ...
        }
    } finally {
        process.destroy();
    }

, , , Java, , , , , ReaderThreads. , , , Java. " taskkill", :

taskkill/IM MySpawnedProcess.exe

+3

.

class ProcessKiller extends Thread {
  private Process process = null;
  public ProcessKiller(Process p) {
    this.process = p;
  }


  public void run() {
    try {
      p.destroy();
    } catch ( Throwable e ) {}
  }
}

Runtime.getRuntime().addShutdownHook( new ProcessKiller( process ));
+2

Source: https://habr.com/ru/post/1703780/


All Articles