Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping a running process via GUI, in java

I have a GUI program that executes TestNG automation scripts. It's meant for users to easily configure some setting and launch the automation script that they want.

One thing I need to add is the ability to instantly stop the running TestNG process. Something like how in Eclipse, the 'Terminate' button will instantly stop whatever is running.

This is what the code that launches the TestNG tests looks like:

public class ScriptRunner implements Runnable {

    public void runScript() {
        Thread testRun = new Thread(this);
        testRun.start();
    }

    @Override
    public void run() {
        //various other things are configured for this, 
        //but they're not relevant so I left them out
        TestNG tng = new TestNG();

        //While this runs, various browser windows are open, 
        //and it could take several minutes for it all to finish
        tng.run();
    }
}

As per the comment, the tng.run() can take several minutes to complete, and it's performing several things, opening/closing browser windows, etc.

How can I just instantly terminate the process, like you would when running an application from an IDE?

EDIT:

Per the comments, I'm attempting to use a ServiceExecutor and shutDownNow() The code is looking like this:

ExecutorService executorService = Executors.newFixedThreadPool(10);

public void runScript() {
    executorService.execute(this);
}

//this method gets called when I click the "stop" button
public void stopRun() {
    executorService.shutdownNow();
}

@Override
public void run() {
    //same stuff as from earlier code
}
like image 894
Andrio Avatar asked Nov 13 '15 16:11

Andrio


People also ask

How do you stop a process from running in Java?

If you start the process from with in your Java application (ex. by calling Runtime. exec() or ProcessBuilder. start() ) then you have a valid Process reference to it, and you can invoke the destroy() method in Process class to kill that particular process.

How do you end a GUI?

GUI1 is the starting GUI when the application is run, after doing the selection within the button group the user should go to the next GUI by pushing “Next” button or terminate the application by pushing “Exit” button.

How do I close my GUI swing?

The first option which is the default is EXIT_ON_CLOSE which terminates the Java swing GUI program when you click the close button on the JFrame window. Another option is DISPOSE_ON_CLOSE which terminates JVM if the last displayable window is disposed of.

How do you kill a Java process in Windows?

If you want to kill all java.exe processes : taskkill /F /IM java.exe /T .


2 Answers

Spawn a child JVM process using ProcessBuilder or Runtime and you will be able to terminate that process when the user requests that the script stops running.

like image 117
Lawrence McAlpin Avatar answered Sep 19 '22 10:09

Lawrence McAlpin


You can use ExecutorService to start test execution into one another thread. You can choose to have many thread in parrallel or juste one thread for all tests in sequence by choosing which executor service you need.

After that, start the execution of all tests in the same executor service instance by calling submit() method on it. You can stop the execution of all submitted runnables by calling shutdownNow() method.

It is important to use the same instance of ExecutorService, otherwise you start each test in a different thread and you will not enable to break the execution chain (or by calling shutdownNow() on all of them).

like image 22
Prim Avatar answered Sep 22 '22 10:09

Prim