Contents OverviewApache Commons Daemon is made of 2 parts. One written in C that makes the interface to the operating system and the other in Java that provides the Daemon API. Both Win32 and UNIX like platforms are supported, but for this article I will only focus on the former and show you how to use Procrun to run your java application as a windows service. PrerequisitesIf you want to run the sample application, you will need Java 6 and Maven installed. GIT is also recommended. In a nutshell1. Download the commons daemon windows binaries and extract the contents For more details run the sample application and/or see the How It Works section. Running the Sample Application1. Using GIT, get the code from JavaWindowsServiceUsingCommonsDaemon.git, or if you don’t have GIT, download the zip and extract it locally. a. To verify that the service is installed, open up the services window from the Control panel. 5. Before starting the service from the Services Window, run LoggerService.exe to see the service run in console mode and confirm that everything was installed correctly. How does it workJava CodeThe bulk of the code is in the LoggerService class, which is a slightly modified version of the ProcrunService class. This is the code that you need to write to wrap your application, namely the main, start, stop and run methods. Next, the start method, like stop, must conform to the signature public void methodName (String[] args). Your application service will run as long as the start method does not return. The start method in the sample ignores the arguments, starts a thread to run the application and blocks till the thread dies or is interrupted.
Stop also ignores the arguments and interrupts the service thread to stop the service.
Run is where your application (logger in the sample) is invoked. If interrupted, it will exit the while loop and end the service thread to stop the service.
Installation ScriptThe batch file Install-Remove-Service.bat relies on LoggerService.exe which was renamed from prunsrv.exe and can be found in the commons daemon windows binarieszip file. The convention, as stated before, is to rename prunsrv.exe to [YOUR_SERVICE_NAME].exe. Now let’s take a look at the Install-Remove-Service.bat and how to modify it for your own use. 1. The first line sets SERVICE_NAME defined, which as noted above, should exactly match the name you gave
2. Next, you need to provide the fully qualified classes that will be used to start and stop your service. These can be, and usually are the same class.
3. Optionally, you can provide the start and stop methods for the classes defined above. The methods must be of type public void methodName (String[] args). If none are provide, these default to the classes static void main(String[] args) method.
4. Optional parameters to the start/stop methods above.
5. Next provide the full path to the jar that contains your start/stop methods. This is used in 8 below.
6. You can also choose to have your service start automatically upon reboot. The default is manual startup.
7. Set this if you want to use a different JVM that is configured in your registry, or if the JVM is not configured in your registry.
8. Set the class path to include all jars needed to run your service
9. Next the service is installed with the //IS command. Note that if you use the –DependsOn option e.g. if your service depends on another service like ActiveMQ, then it must be included here as it will NOT work with the //US command
10.The next series of //US commands update the service configuration
The commons daemon windows binaries also include prunmgr.exe, which by convention is renamed to [YOUR_SERVICE_NAME]w.exe. After the service is installed, you can use it to start/stop your service and change most of the options that were set in the installation script. Below are some screenshots showing the General and Startup tabs. Questions/CommentsLet me know if you have any questions or comments, especially if you used this to turn your java application into a windows service. References/Further ReadingCommons Daemon Home Page
Tags: commons daemon, procrun, windows service |
|