Little framework to create Java programs that behave like Linux daemons
Java Shell Batchfile
Latest commit ce241f2 Feb 4, 2017 @hoegertn hoegertn update Spring

README.md

Taimos daemon-framework

Little framework to create Java programs that behave like Linux daemons/Windows services.

Build Status

Getting started

There are some small steps to follow to create a Linux daemon with Java:

  1. Implement your subclass of de.taimos.daemon.DaemonLifecycleAdapter
  2. Call the DaemonStarter from your main method
  3. Customize your init-script

There are three different startup modes which can be specified with -DstartupMode=<mode>:

  • start: intended for background daemons
  • run: intended for foreground daemons
  • dev: start in development mode

The default mode is dev. If you want to run the program in prodcution mode make sure you start it with -DstartupMode=start or -DstartupMode=run.

In start mode the console logging is disabled.

In development mode the program's behavior will differ in some small points:

  • Logging uses console instead of syslog and logfile

DaemonLifecycleAdapter

void doStart() throws Exception

This method is called to start your program. It must be non blocking. If this method throws any Exception the DaemonFramework stops execution of the daemon.

void doStop() throws Exception

This method is called on OS signal to stop your program. It must be non blocking. If this method throws any Exception the DaemonFramework stops execution of the daemon.

started()

This method is called when the daemon completed startup successfully.

stopped()

This method is called when the daemon completed shutdown successfully. Immediatly after this method System.exit(0) is called.

stopping()

This method is called when the daemon received shutdown signal.

aborting()

This method is called when the daemon aborts execution. Immediatly after this method System.exit(1) is called.

signalUSR2()

This method is called when the daemon received the OS signal USR2. You can do what you want within this method.

exception(LifecyclePhase phase, Throwable exception)

This method is called when an exception occurs in DaemonStarter. It provides the current phase and the thrown exception.

Map<String, String> loadProperties()

This method is called to obtain the daemon properties. All properties provided in this map will be available via System.getProperty later on. The properties found in de.taimos.daemon.DaemonProperties are used by the daemon framework itself and should be filled.

Call DaemonStarter

In your main method just call startDaemon to run the daemon framework. You have to provide a service name and an instance of your subclass of DaemonLifecycleAdapter;

DaemonStarter.startDaemon("my-service-name", new MyLifecycleAdapter());

Customize init-script (Linux)

You just have to create a copy of initscript from the projects linux folder and customize it. For basic usage it is enough to change the variables on top of the script

progname

The name of the daemon

pidfile

The name and location of the PID file. It defaults to /var/run/

RUNDIR

The location of the daemon program. It defaults to /opt/

RUNUSER

The user under which the daemon is run. The init script then calls java with su -c <command> RUNUSER

JAR_FILE

The name of the jarfile as it is found in $RUNDIR. The script calls java -jar .

JAVA_OPTS

Some Java options like memory size etc.

Customize service setup (Windows)

You just have to create a copy of the contents of the windows folder and customize it. For basic usage it is enough to change the variables on top of the script install.bat and to rename the service.exe file.

set svcName=CloudConductor set svcFullName="CloudConductor Server" set svcDesc="Cinovo CloudConductor" set svcJAR=de.cinovo.cloudconductor.cloudconductor-server.jar set svcStarter=de.cinovo.cloudconductor.server.ServerStarter

svcName

The short name of the service (no spaces)

svcFullName

The full name of the service

svcDesc

The description of the service

svcJAR

The name of the jarfile as it is found in the installation folder.

svcStarter

The java main class to start the service