I would like to know how to stop an application programmatically mid-way, if a certain condition is raised. I do not want to code as System.exit(1) as it will terminate the entire JVM and more than 1 application running in the JVM will be shut down. My goal is to shut down only one application (among more than one) that is running in the same shared JVM. The application me be in the process of starting and if a certain condition is encountered, I want to stop the application from starting and shut down only that the application.
I was thinking that throwing a RuntimeException will stop the Spring based application from starting. But that is not true. The runtime exception was thrown, but that did not stop the application from starting.
Is there a way to stop the application from starting (and eventually stopping it), if a certain condition is raised?
Again, not coding as System.exit(1) that would trigger shutdownhook of the entire JVM.
Would like to find a solution for both stand-alone and web-based Java application.
WebSphere does stop the app if it fails to start in certain ways. For example, if a servlet throws an exception in init. Where in Spring are you executing code that you want it to stop? This may give a clue on what to do.
On application start-up, one of the Spring beans looks for a certain condition. If that condition is met, then do not want to start the application any more. Rather, terminate the application without shutting down the JVM (thus, not coding as System.exit(int)). On a side note, as because I cannot code System.exit(int), I cannot code for any shutdownhook either. This is because, shutdownhook gets executed only if the JVM is shutting down.
Now, the Spring bean (which looks for special condition to terminate application) is in a JAR project, which is a part of EAR project with web module. The goal was to localize special condition check only in the Spring bean that is a part of Jar project. For obvious reason, do not want to include Servlet related API in the Spring bean in the JAR project.In other words, the Spring bean in the JAR project shouuld have all the bells and whistles to terminate the application.
I understand that I can write custom code to dispose the intialized Spring beans when the special condition arises.
What I am unable to do is, stop the application from starting, when the special condition is reached. System.exit(int) is not an option.
What could I do to stop the application from starting when the special condition is met? I can write necessary code for clean-up. Is there a way to stop the application from starting?
Will the same code for stopping the application could be applicable for both stand-alone (JAR) and web based (EAR) application?