'JMeter: How to detect the test has been cancelled in a JSR223 (Java) script

I've written some Java code that runs in an endless loop.

How do I detect the test has been stopped, so I can exit the loop?



Solution 1:[1]

There is running property in ThreadGroup class which is being set to false when the test is stopping so you could check it somewhere in your loop like:

if (!ctx.getThreadGroup().running) {
    //do what you need here
}

Also be informed that if you choose java as the language in the JSR223 test element it's not real Java, it's Beanshell and since JMeter 3.1 you're supposed to use Groovy for scripting, moreover the above code won't even work and you will have to explicitly use reflection to read the running property value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T