Skip to content
This repository has been archived by the owner on Feb 22, 2019. It is now read-only.

windows_service

Pavel Vlasov edited this page Jul 3, 2016 · 4 revisions

Windows Service

To set up NSF-based product (or any Equinox product) as a Windows service:

  • Build a launcher jar file for Service stopper project.
  • Create a component in your product listening for a system property to initiate framework shutdown, use FrameworkStopper as a template. Also see framework-stopper.xml component descriptor.
  • Use Apache Procrun to register the service launcher as a Windows service, add plugins\org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar (your launcher jar version may be different) to the service classpath in addition to the launcher jar. Use org.eclipse.equinox.launcher.Main as start class, and org.nasdanika.service.Stopper as stop class with the system property your component listens for as a stop parameter.

The code below is an adaptation of Tomcat service management batch file for WebTest Hub. This code assumes that procrun files prunsrv.exe and prunmgr.exe are renamed to webtesthub.exe and webtesthubw.exe respectively:

@echo off
rem Adaptation of Tomcat service.bat for WebTest Hub

rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements.  See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License.  You may obtain a copy of the License at
rem
rem     http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem NT Service Install/Uninstall script
rem
rem Options
rem install                Install the service using WebTest Hub as service name.
rem                        Service is installed using default settings.
rem remove                 Remove the service from the System.
rem
rem name        (optional) If the second argument is present it is considered
rem                        to be new service name
rem ---------------------------------------------------------------------------

setlocal

set "SELF=%~dp0%service.bat"
set "CURRENT_DIR=%cd%"
set "WEBTEST_HUB_HOME=%cd%"
if exist "%WEBTEST_HUB_HOME%\webtesthub.exe" goto okHome
echo The webtesthub.exe was not found...
echo The WEBTEST_HUB_HOME environment variable is not defined correctly.
echo This environment variable is needed to run this program
goto end
:okHome
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJdkHome
if not "%JRE_HOME%" == "" goto gotJreHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo Service will try to guess them from the registry.
goto okJavaHome
:gotJreHome
if not exist "%JRE_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHome
goto okJavaHome
:gotJdkHome
if not exist "%JAVA_HOME%\jre\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\jre\bin\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%\jre"
goto okJavaHome
:noJavaHome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
echo NB: JAVA_HOME should point to a JDK not a JRE
goto end
:okJavaHome
if not "%WEBTEST_HUB_BASE%" == "" goto gotBase
set "WEBTEST_HUB_BASE=%WEBTEST_HUB_HOME%"
:gotBase

set "EXECUTABLE=%WEBTEST_HUB_HOME%\webtesthub.exe"

rem Set default Service name
set SERVICE_NAME=WebTestHub
set DISPLAYNAME=WebTest Hub

if "x%1x" == "xx" goto displayUsage
set SERVICE_CMD=%1
shift
if "x%1x" == "xx" goto checkServiceCmd
:checkUser
if "x%1x" == "x/userx" goto runAsUser
if "x%1x" == "x--userx" goto runAsUser
set SERVICE_NAME=%1
set DISPLAYNAME=WebTest Hub %1
shift
if "x%1x" == "xx" goto checkServiceCmd
goto checkUser
:runAsUser
shift
if "x%1x" == "xx" goto displayUsage
set SERVICE_USER=%1
shift
runas /env /savecred /user:%SERVICE_USER% "%COMSPEC% /K \"%SELF%\" %SERVICE_CMD% %SERVICE_NAME%"
goto end
:checkServiceCmd
if /i %SERVICE_CMD% == install goto doInstall
if /i %SERVICE_CMD% == remove goto doRemove
if /i %SERVICE_CMD% == uninstall goto doRemove
echo Unknown parameter "%SERVICE_CMD%"
:displayUsage
echo.
echo Usage: service.bat install/remove [service_name] [/user username]
goto end

:doRemove
rem Remove the service
echo Removing the service '%SERVICE_NAME%' ...
echo Using WEBTEST_HUB_BASE:    "%WEBTEST_HUB_BASE%"

"%EXECUTABLE%" //DS//%SERVICE_NAME% ^
    --LogPath "%WEBTEST_HUB_BASE%\logs"
if not errorlevel 1 goto removed
echo Failed removing '%SERVICE_NAME%' service
goto end
:removed
echo The service '%SERVICE_NAME%' has been removed
goto end

:doInstall
rem Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using WEBTEST_HUB_HOME:    "%WEBTEST_HUB_HOME%"
echo Using WEBTEST_HUB_BASE:    "%WEBTEST_HUB_BASE%"
echo Using JAVA_HOME:        "%JAVA_HOME%"
echo Using JRE_HOME:         "%JRE_HOME%"

rem Try to use the server jvm
set "JVM=%JRE_HOME%\bin\server\jvm.dll"
if exist "%JVM%" goto foundJvm
rem Try to use the client jvm
set "JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%JVM%" goto foundJvm
echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
set JVM=auto
:foundJvm
echo Using JVM:              "%JVM%"

set "CLASSPATH=%WEBTEST_HUB_HOME%\serivce-launcher.jar;%WEBTEST_HUB_HOME%\plugins\org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar"

"%EXECUTABLE%" //IS//%SERVICE_NAME% ^
    --Description "WebTest Hub Server" ^
    --DisplayName "%DISPLAYNAME%" ^
    --Install "%EXECUTABLE%" ^
    --LogPath "%WEBTEST_HUB_BASE%\logs" ^
    --StdOutput auto ^
    --StdError auto ^
    --Classpath "%CLASSPATH%" ^
    --Jvm "%JVM%" ^
    --StartMode jvm ^
    --StopMode jvm ^
    --StartPath "%WEBTEST_HUB_HOME%" ^
    --StopPath "%WEBTEST_HUB_HOME%" ^
    --StartClass org.eclipse.equinox.launcher.Main ^
    --StopClass org.nasdanika.service.Launcher ^
    --StopParams org.nasdanika.webtest.hub.app.shutdown^
    --JvmOptions "-Declipse.ignoreApp=true;-Dosgi.noShutdown=true;-Dorg.osgi.service.http.port=18080" ^
    --JvmMs 128 ^
    --JvmMx 1024
if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto end
:installed
echo The service '%SERVICE_NAME%' has been installed.

:end
cd "%CURRENT_DIR%"
Clone this wiki locally