Using the sample service application

A sample service application is provided

Writing a service

Writing an NT service in Java follows the same basic rules as writing one with conventional language.

You don't have to send an ending message to the event log when your app shuts down, the jservice.exe code does it for you.  You are not restricted to just logging to standard error or to the Event Log. You can also log to your own log file if you need to.

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.

Using the Sample Service Application

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:

You have now installed a new service. To de-install this sample service, run jservice -r MyAppService in a cmd window.

Sample Service Application Source Code

// sample java service application

import java.util.*;

import java.lang.*;


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( "" ) );
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
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..."); } } }