1

Blockquote

I have a Java program with functionality that needs to be run periodically. However, I would like to be able to close the wrapper program (if you're a UNIX person, think Ctrl+C in the terminal window or SIGTERM) without any risk of interrupting the main program processes or threads that the wrapper would start.

At first I was thinking I should have my main program sleep for a certain interval after each round of processing. Then I figured that if the program needs to be closed and is manually interrupted by the user, it might be interrupted while it was processing data and the output files would be chopped or corrupted.

I'm thinking the best way is to write a wrapper program (probably also in Java) that would invoke the main program. If something closes the wrapper, I would like the spawned threads or program instances of my main program to remain open until they are ready to close properly. In addition, at most one instance of the main program should be running at any time.

Here's what I would like my wrapper to do (in pseudocode):

Every n minutes: If an instance of the main program is running, do nothing else spawn an instance of the main program.

On Close: leave main program run to completion if it is running

Should I use the Java Runtime object/library to run my main program (effectively from the command line) or should I change my main program to implement Runnable and have the wrapper program spawn threads running my main program?

EDIT (possible solution?):

Currently I'm thinking of using the Java Runtime object to invoke the main program using the exec function (see http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html and http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html). To determine if the main program is already running I can just use the Process.exitValue() function. If it throws an IllegalThreadStateException then the main program is still running.

neubert
  • 15,947
  • 24
  • 120
  • 212
user1258361
  • 1,133
  • 2
  • 16
  • 25
  • One solution is to _not_ use control-C. All of _our_ software is closed with a JMX button. This allows us to close our classes properly and not have to do so when the JVM is coming down hard. – Gray Apr 25 '12 at 14:24
  • my program is going to run from the terminal/console, it doesn't have a GUI. – user1258361 Apr 25 '12 at 14:32
  • Doesn't matter. You can still easily publish JMX buttons and even use a JMX command-line client to shut the application down. – Gray Apr 25 '12 at 14:33
  • 1
    Consider using a ShutDown hook. This answer discusses it: http://stackoverflow.com/q/1216172/505722 – Jim Apr 25 '12 at 14:35

2 Answers2

1

You can use a shutdown hook to perform an orderly shutdown when your application gets a SIGINT (Control-C): see Runtime.addShutdownHook(...).

However this doesn't deal with SIGKILL, JVM crashes, operating system crashes and hard power failures. Indeed, there is no way that an application can shut down cleanly in that kind of scenario. For that you need to design your application to periodically persist its state, using a robust persistence mechanism.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Create a new thread that has certain flags set in a static way. this thread will be run at the start of the main application, could be run/loaded from it. Create a java event listener on the closing action of the program and regster your new thread in it. Whenever your program is closed this new thread will initiate other threads to come alive and do the final savings of the resources to perform one of the stated conditions above. your new process now will kill these threads one after another(whenever each on has done is job) until all the above conditions (flags) are met. when there is no other thread left, this new thread will kill itself, and the program is finished.

GingerHead
  • 8,130
  • 15
  • 59
  • 93