create a system service

Intro

A system service is a java application without user interface and which usually runs permanently. To allow the GUI-application watch and manage system services you'll have to attend a few rules.

Genealogy

A system service should be derived from AbstractService, ...
a sample service SampleService is part of distribution.

public class SampleService extends AbstractService {
    private static final String   APP_NAME = SampleService.class.getSimpleName();
    private static final String[] ctxPaths = { 
            "de/schwarzrot/app/base/ctx/security-context.xml",
            "de/schwarzrot/app/base/ctx/data-access-context.xml",
            "de/schwarzrot/service/sample/ctx/application-context.xml" };

    public static void main(String[] args) {
        List sArgs = new ArrayList(Arrays.asList(args));

        new ServiceLauncher(new SampleService(), 
                                           ctxPaths, 
                                           sArgs).start();
    }
} 

The ServiceLauncher does most of the grunt work of configuration and initialization and enables the service to keep on running, even if started from a console.

A few words about the context files (*.xml files above):
The first both files are SRJRCFrames basics for dataaccess and the like. You should use them as provided. The third context file is the default configuration of your service, so change it as needed, to fit your conception.

workaday

The two methods, you'll need for your service, are:

@Override
public void setupLate() {
   sysInfo = ApplicationServiceProvider.getService(SysInfo.class);
}

@Override
public void run() {
   Log logger = LogFactory.getLog(SampleService.class);

   logger.warn("username ...: " + sysInfo.getUserName());
   logger.warn("system .....: " + sysInfo.getOperatingSystem());
   logger.warn("curdir .....: " + sysInfo.getStartupDirectory());
} 
setupEarly
will be called at service start right after the process has become daemon. Implementation is optional. There's an Adapter-method.
setupLate
is the last method called before the service enters the workloop. This function must be implemented
run
The worker method. This method is called cyclically as long as the service is alive. If the service is expected to operate at low load, the method isIdle can be used to put the service to sleep for a while. The sleep-duration can be changed in the service configuration entry in the database. If you want the service to shutdown from your service code, you may use the method setShutdownRequested.

Startup aid

A system service is not intended to be started manually. It is possible, but bothersome. That's the job of SRServiceManager.

The SRServiceManager uses the database for service management. The Entity ServiceDefinition, or database table services defines, which service should run on what host and how the environment of the service should look like. A service can be declared as "autostart", than the service manager will start that service, as soon it gets started itself.