0

I am using the tool monit to start/stop a process. I have a java file as follows:

class test {
 public void start()
 {
  //do something
 }
 public void stop()
 {
  //do something
 }
}

I want to call the start func when a start command is issued from monit and vice versa. I cannot seem to find a good tutorial explaining what steps I need to take for executing the start and stop method. do I need to write a bash script? and monit will call the bash script which in turn calls the java method?

samach
  • 3,244
  • 10
  • 42
  • 54

1 Answers1

0

The entry point into a java program is the main method.

public static void main(String [] args) 
{
    // args carry the command line arguments.
}

In your case, you should create an instance of test and call start() method on that instance.

public static void main(String [] args) 
{
    test obj = new test();
    obj.start();
}

Java's Runtime class provides an option to add a shutdown hook that gets called when the java program is being terminated. You write a simple thread class that has access to the test instance created in the main method above so that when the shutdown hook thread's run method is called, you delegate it to test instance's stop method.

Hope this helps.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Thanks. it helps. But how will monit call my java main method? – samach Sep 11 '12 at 17:48
  • if monit can start a process, then it would simply call `java `. E.g `java com.vikdor.apps.MainLauncher` where MainLauncher has the static main method defined which will do the stuff discussed in the response. – Vikdor Sep 11 '12 at 17:51