jsrvany logo
w r i t i n g   w r a p p e r s
 
To properly use the jsrvany API you need to either implement the ServiceControlListener interface in your java application or write a wrapper class that calls your application to do this for you.

While it is not strictly necessary to implement the ServiceControlListener interface - jsrvany will simply call the main method of the class you specify on installation - if you want your application to respond correctly to the various service control events, you will need to implement the ServiceControlListener interface in your main or wrapper class.

While all applications will have different requirements, depending on their own implementation, the events to which your want them to respond and the actions required, some simple basic steps to do this are as follows

  • Create an empty wrapper class that extends SimpleListener. SimpleListener is a do-nothing implementation of the ServiceControlListener interface and by extending SimpleListener, you can simply override the methods that are necessary to respond to the events that you need. If your wrapper class already extends another class, you will need to implement the whole of the ServiceControlListener interface.
  • In the main method of your wrapper register your application for callback as follows
    public class MyWrapper extends SimpleListener {
    
        private static ServiceControlManager manager = null;
    
        /**
         * main method
         * 
         * @param args are the arguments to the program as an array of Strings
         * @return void
         */
        public static void main(String[] args) {
    
            // get the ServiceConrolManager
            manager = ServiceControlManager.getInstance();
    
            // register this class as a listener
            manager.addServiceControlListener(main);
    
            // start your application
            MyApp.main(args);
        }
    
    
  • You may register as many classes as you wish with the ServiceControlManager. It is implemented as a singleton and so all calls to the getInstance() method of ServiceControlManager will always returns the same instance, consequently any event will result in notifications being sent out to all listening wrapper classes.
  • Override the handleServiceControlEvent(...) of the event you wish to trap. Most applications will want to respond to the StopServiceControlEvent and TerminateServiceControlEvent at a minimum.
    
        /**
         * handle a StopServiceControlEvent This method is called by the ServiceControlManager
         * when the appropriate event occurs
         * 
         * @param e is the event as an instance of StopServiceControlEvent
         * @return void
         */
        public void handleServiceControlEvent(StopServiceControlEvent e) {
            System.err.println("handleServiceControlEvent: (Stop) "+e);
    
            // shutdown MyApp
            // ...your shutdown code goes here;
        }
    
    
    
Example implementations are available.