4

I need to restart tomcat service from a webapp running on this tomcat. So I'm trying to execute script that stops tomcat service, and then starts it:

echo "before stop" >> textfile.txt
NET STOP "Tomcat7"

:loop
    timeout 3
    SC query Tomcat7 | FIND "STATE" | FIND "RUNNING" > NUL

IF ERRORLEVEL 1 (
    goto start
) ELSE (
    goto loop
)

:start
    NET START "Tomcat7"

Java code:

   String command = "C:\\Tomcat 7.0\\bin\\restart.bat";
   Process p = Runtime.getRuntime().exec(command);

Tomcat is stopped, but not started. If I run this batch from command line, it works properly.

thank you for your time

Kara
  • 6,115
  • 16
  • 50
  • 57
lili
  • 1,866
  • 8
  • 29
  • 50

3 Answers3

5

This worked:

        String fileName = "C:\\Tomcat 7.0\\bin\\restart.bat";
        String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
        Runtime.getRuntime().exec(commands);

taken from http://www.rgagnon.com/javadetails/java-0014.html

lili
  • 1,866
  • 8
  • 29
  • 50
4

What you are asking is not exactly safe and possible but do take a look at Tomcat manager API that allows you to programmatically manipulate Tomcat deployment and instance:

http://tomcat.apache.org/tomcat-7.0-doc/api/index.html

http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/manager/host/HostManagerServlet.html

Edmon
  • 4,752
  • 4
  • 32
  • 42
  • Thank you Edmon. Why it is not possible from batch? – lili Aug 09 '12 at 13:23
  • 1
    If you are running from the same process as Tomcat (i.e. your web app is hosted on Tomcat) if you restart Tomcat, you will kill your own app. You could try of course and see what happens, but I doubt it is going to do much of what you would want. – Edmon Aug 09 '12 at 13:34
  • I am ok with restarting my own application. As I wrote, I can stop tomcat, but not start it – lili Aug 09 '12 at 13:46
  • right, because your webapp does not have a instance to run since tomcat is down - it stops it and then all goes down. You would have to control Tomcat from outside of the tomcat instance. – Edmon Aug 09 '12 at 13:48
  • I run a batch that does 2 things: stops tomcat service, then starts it. Do you mean that when my app is stopped, batch thread is killed and cannot continue running? – lili Aug 09 '12 at 13:49
  • You said "I need to restart tomcat service from a webapp running on this tomcat." - if that is what you really want to do - that will not work. You can try to kick off some job outside of tomcat that does this, but webapp will die in the process since it is on tomcat that gets stopped (and started). – Edmon Aug 09 '12 at 13:54
  • I run batch from webapp. Isn't it considered an external job? – lili Aug 09 '12 at 13:59
  • yes, but when you kick off the batch job it stops the tomcat and hence it stops the webapp itself. – Edmon Aug 09 '12 at 14:08
  • so how can I start an external job from java? – lili Aug 09 '12 at 14:09
  • 2
    This thread has all the information: http://stackoverflow.com/questions/3468987/executing-another-application-from-java – Edmon Aug 09 '12 at 14:15
1

Agree with Edmon.

Tomcat is a provider of containers. Each container should act independently of each other, even if they call the services another provides. This should all be done via RMI or alike.

Like Edmon also suggests, you could call using the API, but again... sounds bad. Instead, question why it needs to restart. Then, if there's no work around, use the Tomcat Manager.

Michael
  • 1,014
  • 1
  • 8
  • 23
  • I need to restart tomcat service after a heavy batch process to prevent a memory leak (as a workaround until a leak is found). I think that reloading webapp will not be enough. – lili Aug 09 '12 at 13:30