Sunday, April 23, 2017

Hazelcast as Windows Service Using Procrun


Hazelcast as Windows Service

Check this Github Repo For code

https://github.com/aravindrajasekharan/HazelCast-WindowsService.git

Hazelcast can follow 2 main deployment typology however if you are following a Server - Client Architecture  and you want to start a server on a windows node, follow the below steps.

Am using Apache Procrun to expose the Hazelcast as a windows Service

https://commons.apache.org/proper/commons-daemon/procrun.html

Follow the link to understand more on the same,It is very easy to setup however documentation is little tedious to get a grasp.

PROS
----

Will create a windows service (kind of obvious :) ).

CONS
-------

Another Jar to maintain along with the Hazelcast. Since we have a dependency in our self executable jar project, on Hazelcast version upgrade means upgrading the jar as well. (Ideally,it will work even without upgrading assuming the Core Hazelcast API is going to remain the same with the future releases).

Will look for some other options till then will stick to this.

Anyways here are the steps to follow


Creating a Executable Java Wrapper JAR for Hazelcast.

  1. Download the Source code and Compile the Project.It is a maven project and uses the Apache Maven Shade Plugin in POM. It is disabled now to not include the Dependencies- Hazelcast jar and Log4j jar etc. The Idea is to make procrun pick it up from the class path. IF shade plugin is enabled can create a FAT jar that will have all dependencies packed in one. This will make future releases more tedious as the jar dependencies needs to be upgraded as well.
    https://maven.apache.org/plugins/maven-shade-plugin/
  2. Our Idea is to Create a Self Executable Jar File with a start method which will start the server and Stop Method which will stop the Server.
  3. Once the Compile is successful , Use the "HazelCastService-1.0-SNAPSHOT.jar" Check the Manifest file to make sure it contains this line Main-Class: com.aravind.service.HazelCastService. This entry makes sure the jar is executable.
  4. In Pom file the versions defined are 1.7 however it will work just fine with 1.8. Hazelcast version also will work fine with Java 1.8.
  5. Start Method takes the Config file as argument. The Argument is passed into the class by the service created by Procrun.

Setting Up Procrun and Windows Service.

The Scripts Folder contains 2 files
    1. HazelCastService.bat - This File will create the Service in windows
      1. Open the File and Modify the locations in the file to point to the right locations on the node. Note: If the Locations contains Space, try enclosing in Double quotes and run again.
      2. Add the Log4j jar defined in the Class path line to the Folder in which all other jars exist. I usually put both these files in the lib folder of Hazelcast download along with the log4j Jar.
      3. PR_STARTPARAMS - will take the location of the XML file.
      4. -Dhazelcast.logging.type=log4j if this is defined dont forget to add the log4j.xml or properties and add that file name to the end of Class Path variable. Best is to add it in same folder as the other jars. If file not added all the things that get logged on to the Console windows will be auto redirected to the lof files specified in the bat file.
      5. hazelcastinstallsvc.exe //IS//%SERVICE_NAME% - this line will install the windows service
      6. The Logs can be seen in the folder we specified in the bat file. Note: Might not see the details in Event logs on windows. Always check the defined log file path and file for details.
    2. hazelcastinstallsvc.exe - this is downloaded from the Procrun downloads page however this is the exe under amd64 folder.

Uninstall Service

use hazelcastinstallsvc.exe //DS//<SERVICE_NAME> change service name with your service name.

The Solution works with all the JVM parameters and is not that difficult. I feel it is more elegant than relying on the Bat file or using Run time to kill the  process from java. 

The other thing to note is porting to Linux is also easy using the same apache commons library jsvc.


No comments:

Post a Comment