Pages

Monday, November 3, 2014

Configuring Glassfish 4.1 as a Windows Service

Previously I did publish a post on how to install Glassfish V2 as a Windows service a few years ago. Things have moved on quite a bit since then and now the latest version of the server comes with a built in command to configure the domain as a windows service.

There are a few short comings though. The command does not fully leverage the windows services and you still have to configure a few things after creating the service. Well, without further ado, let’s start installing the service and you will soon find out what I meant by “shortcomings”.

First thing first, we need to make sure the Glassfish is working properly even before we think about starting to configure that as a service. Use the asadmin prompt to verify that you can start-stop the sample domain and that everything is working fine.

NOTE: You need admin privileges to install the windows service.

Now open a new command prompt as administrator and type in the following command.

asadmin create-service domain1
The Windows Service was created successfully.  It is ready to be started.  Here are the details:
ID of the service: domain1
Display Name of the service:domain1 GlassFish Server
Server Directory: C:\glassfish4\glassfish\domains\domain1
Configuration file for Windows Services Wrapper: C:\glassfish4\glassfish\domains\domain1\bin\domain1Service.xml
The service can be controlled using the Windows Services Manager 
or you can use the Windows Services Wrapper instead:
Start Command:  C:\glassfish4\glassfish\domains\domain1\bin\domain1Service.exe  start
Stop Command:   C:\glassfish4\glassfish\domains\domain1\bin\domain1Service.exe  stop
Restart Command:  C:\glassfish4\glassfish\domains\domain1\bin\domain1Service.exe  restart
Uninstall Command:  C:\glassfish4\glassfish\domains\domain1\bin\domain1Service.exe  uninstall
Install Command:  C:\glassfish4\glassfish\domains\domain1\bin\domain1Service.exe  install
Status Command: C:\glassfish4\glassfish\domains\domain1\bin\domain1Service.exe status
You can also verify that the service is installed (or not) with sc query state= all
windows.services.uninstall.good=Found the Windows Service and successfully uninstalled it.
For your convenience this message has also been saved to this file: C:\glassfish4\glassfish\domains\domain1\PlatformServices.log
Command create-service executed successfully.

This command will create a service like a breeze. However, the description of the service created using this command is not very descriptive. Problem here is that if you have more than a few services installed on the system, especially if you are looking to install multiple domains in one server then these descriptions are really not good enough.

Fortunately the command created a few exe files and a configuration file that we can use to install, uninstall the service at will. These files are located in the bin folder of the domain. The configurations of the service are stored in domain1Service.xml which looks like below for me.


<service>
  <id>domain1</id>
  <name>domain1 GlassFish Server</name>
  <description>GlassFish Server</description>
  <executable>C:/glassfish4/glassfish/lib/nadmin.bat</executable>
  <logpath>C:\\glassfish4\\glassfish\\domains/domain1/bin</logpath>
  <logmode>reset</logmode>
  <depend>tcpip</depend>
  <startargument>start-domain</startargument>
  <startargument>--watchdog</startargument>
  <startargument>--domaindir</startargument>
  <startargument>C:\\glassfish4\\glassfish\\domains</startargument>
  <startargument>domain1</startargument>
  <stopargument>stop-domain</stopargument>
  <stopargument>--domaindir</stopargument>
  <stopargument>C:\\glassfish4\\glassfish\\domains</stopargument>
  <stopargument>domain1</stopargument>
</service>

We are only interested in the following tags as they are the ones displayed in the Windows Services Manager

<service>
  <name>domain1 GlassFish Server</name>
  <description>GlassFish Server</description>
</service>

We simply need to update these two parameters and give our domain a more descriptive display name and description. I updated the values as follows and save the file.

<service>
  <name>GF Domain1</name>
  <description>Windows service for the GlassFish Domain1 domain</description>
</service>

After saving the file, you now need to uninstall the existing service and then install the service again using the domain1Service.exe file that got created in the previous step. Open a command prompt, go to the bin directory, and run the following commands:

C:\> cd \glassfish4\glassfish\domains\domain1\bin
C:\glassfish4\glassfish\domains\domain1\bin> domain1Service.exe uninstall
C:\glassfish4\glassfish\domains\domain1\bin> domain1Service.exe install
C:\glassfish4\glassfish\domains\domain1\bin> 

You now have your Glassfish 4.1 domain configured as a Windows service with a name and description of your choice.

No comments:

Post a Comment