I have a javaagent which collects certain info while the app is running. It also registers a shutdown hook which at the time of the app exit compares the collected data with some golden data and is expected to exit with a non zero exit status if the comparison fails.
However, I am not able to do this since calling System.exit(exitStatus) seems to force the app into deadlock.
Is there a way to exit with a non-zero exit status FROM WITHIN THE SHUTDOWN HOOK
I also tried using java.lang.Runtime.halt(exitStatus) but it doesn't seem to help. echo $? seems to always indicate the exit status to be 0
My application code
public class Sample {
public static void main(String args[]) {
System.out.println("My code");
}
}
My Agent code:
import java.lang.instrument.Instrumentation;
class CompareDataAtTheTimeOfExit extends Thread {
@Override
public void run() {
// If comparison fails exit with 5
}
}
class Agent {
public static void premain(String args, Instrumentation inst) throws Exception {
Runtime r = Runtime.getRuntime();
r.addShutdownHook(new CompareDataAtTheTimeOfExit());
}
}