Little framework to create Java programs that behave like Linux daemons/Windows services.
There are some small steps to follow to create a Linux daemon with Java:
- Implement your subclass of de.taimos.daemon.DaemonLifecycleAdapter
- Call the DaemonStarter from your main method
- 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 production 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
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.
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.
This method is called when the daemon completed startup successfully.
This method is called when the daemon completed shutdown successfully. Immediatly after this method System.exit(0) is called.
This method is called when the daemon received shutdown signal.
This method is called when the daemon aborts execution. Immediatly after this method System.exit(1) is called.
This method is called when the daemon received the OS signal USR2. You can do what you want within this method.
This method is called when an exception occurs in DaemonStarter. It provides the current phase and the thrown exception.
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.
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());
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
The name of the daemon
The name and location of the PID file. It defaults to /var/run/
The location of the daemon program. It defaults to /opt/
The user under which the daemon is run. The init script then calls java with su -c <command> RUNUSER
The name of the jarfile as it is found in $RUNDIR. The script calls java -jar .
Some Java options like memory size etc.
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
The short name of the service (no spaces)
The full name of the service
The description of the service
The name of the jarfile as it is found in the installation folder.
The java main class to start the service