views:

530

answers:

2

I have an application with several threads running. Each of them is wrapped with a cath(Throwable ) that I can use if something unexpected happen. What is the best way to restart the app itself under Ubuntu/Linux. I found this project "Java Service Wrapper" , any experience with it?

I tried the -XX:OnError options, but it doesn't seems to work (Java(TM) SE Runtime Environment (build 1.6.0_16-b01) )

+4  A: 

Could you start it from a batch file (that loops) and have it use System.exit(1)?

If so you could test the error level in the batch file and if it's 1, loop back and restart the program but if it's zero, exit the batch file.

Edit: Do you wish to trigger the "Reset" externally (From unix?)

If so, my suggestion would be as follows:

  1. Create a command-line option called -restart
  2. When run without the command line, open a socket on a preset high port (32123?) and listen for a connection as part of starting your app.
  3. When you get a connection with some key string passed into it, do a System.exit(1);
  4. When run with -restart, instead of your normal startup connect to that port and send the correct key string and exit.
  5. If your restart routine can't connect, be sure to print an error message saying that the deamon isn't running.

There are other ways to send a message to the existing process, but a socket is probably the easiest--it's just a few lines of code. Another is polling for the existence of a file in some absolute location, and there is also finding the PID and killing it.

The socket as the added advantage of being platform independent.

Bill K
A: 

You must design a mechanism to handle your many threads, catch the issues and determine if you can restart the failing thread from inside Java.

The only place the operating system matters is if your application exits completely and needs to be restarted. As long as you have some part of your application running, you should not consider this.

Thorbjørn Ravn Andersen
Except if you want to send a socket message to the app to have it restart itself. Then maybe would you think that it might be ok that I consider this?
Joseph Gordon