-2

I need to restart the tomcat from Java code.

For eg, if a query is not executed for a certain time Period then it will restart the tomcat automatically.

I have tried the following shutdown and startup code, but when we shutdown the tomcat then the java code will not run and tomcat not started.

Note :- I am running this code from a application and restarting the same tomact which the same application is using.

Following the code

try {

    PreparedStatement.setQueryTimeout(10);
    rs = PreparedStatement.executeQuery();

} catch (SQLException ex) {

   System.out.println("IN CATCH BLOCK FOR THE REFRESH INVOICE");

   String shutcommand = "killall java";
    Process shutchild = Runtime.getRuntime().exec(shutcommand);

    System.out.println("JAVA PROCESS KILLED");

    String locationCommand = "cd /root/cluster/tomcat6/bin";
    Process locationChild = Runtime.getRuntime().exec(locationCommand);

    String strtcommand = "./startup.sh";
    Process strtchild = Runtime.getRuntime().exec(strtcommand);
}
NewDeveloper
  • 31
  • 3
  • 8
  • 1
    Now this is an interesting approach. If a database query fails, your application commits suicide and then tries to raise himself from the dead. How would restarting Tomcat even help, considering that it's probably the database server's fault that things are taking too long? – Kayaman Jan 05 '18 at 08:23
  • Doing this for every `SQLException` may cause restart loops if there is some other data problem (like for example a duplicate key). – Henry Jan 05 '18 at 08:27
  • I am java - kill me - ok I am dead - now I can restart tomcat – Scary Wombat Jan 05 '18 at 08:34
  • The way something like this is usually done is using a wrapper process that restarts it's child when it detects that something is wrong. – Henry Jan 05 '18 at 08:36
  • 2
    totall bad idea. I guess XY problem. What is MAIN problem? – Jacek Cz Jan 05 '18 at 08:37
  • @Henry , @ Scary :- Yes I have a resource problem. If I didn't get the data after 10 seconds then the query is in deadlock condition and it is taking db resources and web application is slow. – NewDeveloper Jan 05 '18 at 08:41
  • Maybe you should concentrate on fixing the actual problem causing the deadlock, as restarting the Tomcat is just a very poor workaround that you can't even get to work properly. – Kayaman Jan 05 '18 at 08:43
  • Thanks. Now I have made a script to shutdown and restart tomcat and called the same from java code. Work Fine. I have a release today, so done this, will fix the deadlock afterwards. – NewDeveloper Jan 05 '18 at 09:28
  • "work fine" ... probably You only hide real problem (and probably few others, not known yet) – Jacek Cz Jan 05 '18 at 12:58
  • **@jacek Cz** the MAIN problem is due to resource deadlock when the sql query is called from java code, and the cpu usage is high of the database and the application is slow. The sql query is scheduled to be called in every 30 seconds to refresh the screen for our work. As the table is used in many process, so this happen. – NewDeveloper Jan 07 '18 at 06:25

4 Answers4

0

Killing tomcat after SQLException this is not good idea to handle this exception. Probably the problem is on database site.

But if you are sure, that it is what you need you can kill this java proces in this section, but to run tomcat you should use for example bash and cron. Why? Beacues after killing your executing code will stop, so you don't achieve the line to start tomcat.

How to check tomcat: Is Tomcat running?

Adrian Klimczak
  • 133
  • 1
  • 10
  • Thanks, i am facing the same problem, is there any way out to achieve this. – NewDeveloper Jan 05 '18 at 08:37
  • Your java code is fine to kill all java process. To check tomcat is alive: [is-tomcat-running](https://stackoverflow.com/questions/3944157/is-tomcat-running) to run tomcat from cron watch this: [execute-python-script-on-crontab] (https://stackoverflow.com/questions/8727935/execute-python-script-on-crontab) – Adrian Klimczak Jan 05 '18 at 08:42
  • Thanks. Now I have made a script to shutdown and restart tomcat and called the same from java code. Work Fine. I have a release today, so done this, will fix the deadlock afterwards – NewDeveloper Jan 05 '18 at 12:10
0

TL;DR

File binaryDir = new File(System.getProperty("catalina.home") + File.separator + "bin");
String restartCommand = "\"shutdown.bat & ping 0.0.0.0 -n 4 & C:\\WINDOWS\\system32\\net start Tomcat8\"";
new ProcessBuilder("cmd", "/c", restartCommand).directory(binaryDir).start();

Survive

Creating new process will survive JVM shutdown. If you combine commands in one line it should work fine according to my tests and this.

shutdown.bat

You need to use shutdown.bat instead stopping windows service because it often fails on Windows with message Cannot stop service Apache Tomcat...

ping 0.0.0.0 -n 4

You need to wait some time after shutdown otherwise you will get Service is already starting. Try again later error message. Also note I use ping instead timeout because it causes problems on some systems.

C:\WINDOWS\system32\net start Tomcat8

I'm starting windows service because invoking startup.bat won't work for me. Also remember to replace Tomcat8 if you using different tomcat or custom service name for example Tomcat7

Redirect I/O

Don't redirect input or output of process instance or command will shutdown with JVM, and Tomcat won't start.

dagi12
  • 449
  • 1
  • 5
  • 20
-2

You can execute this native command using java

String command = "c:\program files\tomcat\bin\startup.bat";//for linux use .sh
Process child = Runtime.getRuntime().exec(command);
MrFlamme26
  • 91
  • 7
-3

You should consider using ProcessBuilder instead of Runtime exec. Also, you should split all the arguments when you want to execute a command.

I suggest this :

ProcessBuilder shutcommand = new ProcessBuilder("killall", "java");
Process shutchild = shutcommand.start();

System.out.println("JAVA PROCESS KILLED");

ProcessBuilder strtcommand = new ProcessBuilder("/root/cluster/tomcat6/bin/startup.sh", "java");
Process strtchild = strtcommand.start();
Mickael
  • 4,458
  • 2
  • 28
  • 40
  • So the only flaw you see with this approach is not using `ProcessBuilder`? The idea of a program killing itself and then starting itself (when dead) is just fine and dandy? – Kayaman Jan 05 '18 at 08:31