-2

I want to be able to stop my application from inside every 3 days. I just want to schedule a method that would automatically stop the application without any intervention from outside (like sitting on my desktop etc).

So far, I have these in mind:

Runtime.getRuntime().exec("rhc stop-app --app ApplicationName"); //This doesn't work. I have tried it

Or

System.exit(0) // I am told to not run this command on openshift server

Or

Runtime.getRuntime().exit(0) //I am afraid to run this command

Or

Runtime.getRuntime().halt(0) //I am afraid to run this command as well      

Some of those commands I am afraid to use because I am told to not run them on openshift server as they may actually stop the whole vm. And that will stop all the applications that are hosted on that vm. And that will get me banned. I will get banned.

edit: This question is not how to stop an application generally. This question is regarding how to stop a Spring MVC wep application which is hosted on OpenShift server. And no those answers donot work in that link.

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • All you need to do is to stop your application based on a logical condition e.g. `while (validConditionToContinue()) { yourProcessingHere(); }`. The program will then naturally exit when the condition no longer applies. And it's up to your application to trigger how it exits that loop. – ManoDestra Jul 29 '16 at 14:37
  • @ManoDestra I want a specific command to stop the application. That specific command is Runtime.getRuntime().exit(0). Thanks – Faraz Aug 01 '16 at 08:37
  • Bad practice. You should not require that command unless something absolutely disastrous has happened that requires immediate exit from the application. It should not be used for normal exit from an application. That can be done very simply using normal logic. Unless you're using shutdown hooks: http://stackoverflow.com/questions/3715967/when-should-we-call-system-exit-in-java. Your current requires above don't really match that scenario. You should handle it with logic and exit your application more gracefully than forcing an exit using Runtime. – ManoDestra Aug 01 '16 at 13:07
  • @ManoDestra I have realized the problem with my approach. I would call it a unexpected side effect. The side effect is that my application is automatically restarted after some time. lol. I don't want that. I had Runtime.getRuntime().exit(0) specifically for that to not happen. I don't want my application to restart without me restarting it! Now, coming to your approach. If I go by your approach, then the program will still not stop. It would be like if false, don't run my processes. But the application is still up! Sure it will not run my processes, but my app is still up and running. – Faraz Aug 04 '16 at 17:05
  • @ManoDestra I want a full stop of my application! Is there a way? Thanks – Faraz Aug 04 '16 at 17:07
  • If the logic has completed, then your application will exit, as it will fall out of its running loop. while (moreToProcess) { doStuff(); }. When that logical condition is false, then it falls out of the loop and with nothing more to do in the main method(), then your application closes of its own accord. You don't need an explicit closure, except in the case of unrecoverable failure. For normal application exit, explicit closure is generally not required. If it is, then you're not coding your application correctly. – ManoDestra Aug 04 '16 at 19:40
  • Create a sample test Java class that performs a few statements. Something like `int count = 0; while (count < 100) { count++; }`. Put that in your main method and then run the compiled program. Does it end? Yes. Of course it does. There's no need for explicit closure by using Runtime. Programs end of their own accord when you dictate it to be so by simply allowing your program to run through to completion and ensuring that all resources created have been closed, etc and that any non daemon threads have completed their tasks. Handle your closure logic in code and allow natural closure. – ManoDestra Aug 05 '16 at 16:47
  • @ManoDestra can we talk this situation in private chat? – Faraz Aug 08 '16 at 06:29
  • Due to network restrictions, I can't do that. Look at this answer. It accurately reflects the issues regarding using System.exit() (and by association Runtime.getRuntime().exit()). Unless you have encountered an extensive failure, or you wish to exit naturally and return a 0 at the highest level (so long as resources have been released), then using this method is okay. You could also look at shutdown hooks if you do need to handle anything that needs cleaned up at the end. However, as I've already stated, immediate closure like this is not generally recommended. Your program can naturally end. – ManoDestra Aug 08 '16 at 13:30
  • Answer referred to above: http://stackoverflow.com/a/6171272/5969411 – ManoDestra Aug 08 '16 at 13:30
  • This question is not about java but about the openshift server. You should explain the reason why do you want to stop it, e.g.: 'Stop further billing'? – tak3shi Aug 10 '16 at 08:27
  • Possible duplicate of [How to quit a java app from within the program](http://stackoverflow.com/questions/2670956/how-to-quit-a-java-app-from-within-the-program) – Raedwald Aug 10 '16 at 08:37
  • @tak3shi the reason is I don't want to provide free service after I am dead. I want my application to stop. – Faraz Aug 10 '16 at 11:56

1 Answers1

0

Well, I just used Runtime.getRuntime().exit(0) and it successfully stopped my application. I didn't get ban or anything. And I restarted my application to see if there are any issues. No Issues. So that command will do. Thanks

edit: This works but openshift restarts the application automatically. So this is not the answer. I will put the answer here if I find one. Thanks

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • Have you realized that openshift is an application server and not an application? You are killing the whole application server just to stop a process in your application? – tak3shi Aug 10 '16 at 10:43
  • Yes I know. They have security checks. No one can just have a simple command like System.exit() in their code and stop the whole server. They don't allow that. So yeah I am not killing the whole server. I just want to kill my own application after a certain time. And hence the question – Faraz Aug 10 '16 at 11:49