Writing an NT service in Java follows the same basic rules as writing one with conventional language.
Use the properties file extensively, as it is your link to the 'outside world'. Debug modes, log file names, etc. should be included in the file. Changing modes or files would be as easy as stopping the service, editing and saving the properties file and re-starting the service.
To get a basic understanding of what needs to happen, it would be a good idea to visit the HOWTO page now.
Also, see the section on editting the properties file.
The basics of using the Sample Service is as follows:
// sample java service application import java.util.*; import java.lang.*; import java.io.*;Import the dsb.javasvc package, remember to set up your classpath correctly to include the .jar file
import dsb.javasvc; /* * All services should be threads, this allows the StopJavaService class to stop it * */ public class MyAppService extends Thread { protected int shutdownPort=0; protected int loggingPort=0; //declare the stopjavaservice object protected StopJavaService sjs=null; public static void main(String args[]) throws Exception { new MyAppService(); } /* * Start the service application * read in the properties file, especially looking for the shutdown.port property */ public MyAppService() { Properties p = new Properties( ); String str; try { p.load( new FileInputStream( "your_app.properties" ) );Read in the shutdown port number from your properties file
String portname = p.getProperty( "var.shutdown_port", "0" ); shutdownPort = Integer.parseInt(portname); // we have the shutdown port now if(shutdownPort > 0) { loggingPort = shutdownPort + 1; } this.start(); } catch( Exception e) { // log to stderr Logging.LogErr(e.getMessage()); } } public void run() { try { sjs = new StopJavaService("StopJavaService"); // set the listen port (always based upon shutdownPort, it is shutdownPort + 1) sjs.setPort(shutdownPort);Set the thread to stop in the StopJavaService class to this
sjs.setThreadToStop(this);Start the StopJavaService thread.
sjs.start(); // do nothing, just waste time for(;;) { this.sleep(20000);Log a message in the event log
Logging.LogServiceMsg("In the loop",loggingPort); } } catch( Exception e) { Logging.LogErr(e.getMessage()); } finally { Logging.LogErr("Stopping MyAppService..."); } } }