I have a java application which combines embedded jetty and Solr in one application, and wrap it as a windows service.
This embedded jetty and Solr application should be able to be installed at different locations, and run as a different services.
To do this: I use Apache procrun, put prunsrv.exe and prunmgr.exe in bin folder, create several windowshowAdsenses bat files to install it as a widows service, to uninstall it, start and stop the services.
installService.bat
We can install the application with specified service name, service display name, service description, we can use start_params to specify parameters to the application like port number.
The script will first use %PRUNSRV% //IS, %PRUNSRV% //US to install it as a service, then copy prunmgr.exe to %service_name%.exe, so we can use %service_name%.exe to monitor, edit and manage the service.
installService.bat -service_name service1 -service_display_name display_name1 -service_description service_description1 -start_params "start;-port;9753" -stop_params shutdown
To uninstall the service, we just need specify service name:
uninstallService.bat -service_name service1
It will use "%PRUNSRV%" //DS to uninstall the service.
It's very easy to implement: just get service name, then use %PRUNSRV% //ES to start the service, use %PRUNSRV% //SS to stop the service.
startService.bat/stopService.bat -service_name service1
startService.bat
http://commons.apache.org/proper/commons-daemon/procrun.html
Commons Daemon Procrun as a Java Service Wrapper for ActiveMQ
This embedded jetty and Solr application should be able to be installed at different locations, and run as a different services.
To do this: I use Apache procrun, put prunsrv.exe and prunmgr.exe in bin folder, create several windowshowAdsenses bat files to install it as a widows service, to uninstall it, start and stop the services.
installService.bat
We can install the application with specified service name, service display name, service description, we can use start_params to specify parameters to the application like port number.
The script will first use %PRUNSRV% //IS, %PRUNSRV% //US to install it as a service, then copy prunmgr.exe to %service_name%.exe, so we can use %service_name%.exe to monitor, edit and manage the service.
installService.bat -service_name service1 -service_display_name display_name1 -service_description service_description1 -start_params "start;-port;9753" -stop_params shutdown
@echo off @echo off @setlocal enableextensions enabledelayedexpansion goto install :parseParams set key=%~1 set value=%~2 if "%key%" == "" goto :eof if "%key%" == "-PR_JAVA_OPTIONS" ( shift shift if "%value%" == "" ( echo Empty value for -PR_JAVA_OPTIONS goto end ) SET PR_JAVA_OPTIONS=%value% ) else if "%key%" == "-remotedebug" ( shift SET remotedebug=true ) else if "%key%" == "-action" ( shift shift if "%value%" == "" ( echo Empty value for -action goto end ) SET ACTION=%value% ) else if "%key%" == "-service_name" ( shift shift if "%value%" == "" ( echo Empty value for -service_name goto end ) SET SERVICE_JAVA=%value% ) else if "%key%" == "-service_display_name" ( shift shift if "%value%" == "" ( echo Empty value for -service_display_name goto end ) SET PR_DESPLAYNAME=%value% ) else if "%key%" == "-service_description" ( shift shift if "%value%" == "" ( echo Empty value for -service_description goto end ) SET PR_DESCRIPTION=%value% ) else if "%key%" == "-start_params" ( shift shift if "%value%" == "" ( echo Empty value for -start_params goto end ) SET START_PARAMS=%value% ) else if "%key%" == "-stop_params" ( shift shift if "%value%" == "" ( echo Empty value for -stop_params goto end ) SET STOP_PARAMS=%value% ) else if "%key%" == "-pr_jvm" ( shift shift if "%value%" == "" ( echo Empty value for -pr_jvm goto end ) SET PR_JVM=%value% ) else if "%key%" == "-uninstallIfExsit" ( shift SET UNINSTALL_IF_EXSIT=true ) else ( shift ) goto parseParams %* goto :eof :install set SERVICE_JAVA=MyApp set PR_DESPLAYNAME=MyApp set PR_DESCRIPTION="MyApp Service" set START_PARAMS=start set STOP_PARAMS=shutdown set PR_JVM=auto SET PR_JAVA_OPTIONS="-Xms512m;" SET CMD_OPTS=%* set APP_RUNNING_HOME= set "APP_HOME=%~dp0/../" set "PATH_PRUNSRV=%~dp0" set "PR_LOGPATH=%APP_HOME%/logs" set UNINSTALL_IF_EXSIT=false SET MYCLASSPATH= rem set PR_INSTALL=%PATH_PRUNSRV%prunsrv.exe call :parseParams %* set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe if "%PR_DESPLAYNAME%" == "" ( set PR_DESPLAYNAME=%SERVICE_JAVA% ) REM check whether service already exists echo Installing %SERVICE_JAVA% SC QUERY %SERVICE_JAVA% > NUL REM if ERRORLEVEL 1060 means it doesn't exists IF not ERRORLEVEL 1060 ( if "%UNINSTALL_IF_EXSIT%" == "true" ( "%PRUNSRV%" //DS//%SERVICE_JAVA% ) else ( echo %SERVICE_JAVA% already exists, and uninstallIfExsit is false. Do nothing goto end ) ) "%PRUNSRV%" //IS//%SERVICE_JAVA% --Install="%PRUNSRV%" if not errorlevel 1 goto installed echo Failed to install '%SERVICE_JAVA%' service goto end :installed echo The service '%SERVICE_JAVA%' has been installed. set libDir=%APP_HOME%\lib set MYCLASSPATH="%libDir%\*;%libDir%\ext\*;%libDir%\shared-libs\*;%APP_HOME%\etc" if "%remotedebug%" == "true" ( SET PR_JAVA_OPTIONS="%PR_JAVA_OPTIONS%;-agentlib:jdwp=transport=dt_socket,address=9999,server=y,suspend=n" ) echo Setting the parameters for %SERVICE_JAVA% echo "%PRUNSRV%" //US//%SERVICE_JAVA% --DisplayName "%PR_DESPLAYNAME%" --Description "%PR_DESCRIPTION%" --StdOutput auto --StdError auto ^ --Classpath=%MYCLASSPATH% --Jvm="%PR_JVM%" --JvmOptions="%PR_JAVA_OPTIONS%" --Startup=auto ^ --StartMode=jvm --StartClass=MainClass --StartParams="%START_PARAMS%" ^ --StopMode=jvm --StopClass=MainClass --StopParams="%STOP_PARAMS%" "%PRUNSRV%" //US//%SERVICE_JAVA% --DisplayName "%PR_DESPLAYNAME%" --Description "%PR_DESCRIPTION%" --StdOutput auto --StdError auto ^ --Classpath=%MYCLASSPATH% --Jvm="%PR_JVM%" --JvmOptions="%PR_JAVA_OPTIONS%" --StartPath %APP_HOME% --Startup=auto ^ --StartMode=jvm --StartClass=MainClass --StartParams="%START_PARAMS%" ^ --StopMode=jvm --StopClass=MainClass --StopParams="%STOP_PARAMS%" if not errorlevel 1 goto updated echo Failed updating '%SERVICE_JAVA%' service goto end :updated If NOT exist %SERVICE_JAVA%w.exe ( copy /B prunmgr.exe %SERVICE_JAVA%w.exe ) echo The service '%SERVICE_JAVA%' has been updated. echo Installation of %SERVICE_JAVA% is complete goto end :end endlocal @echo onuninstallService.bat
To uninstall the service, we just need specify service name:
uninstallService.bat -service_name service1
It will use "%PRUNSRV%" //DS to uninstall the service.
@echo off setlocal goto uninstall :parseParams set key=%~1 set value=%~2 if "%key%" == "" goto :eof if "%key%" == "-service_name" ( shift shift if "%value%" == "" ( echo Empty value for -service_name goto end ) SET SERVICE_JAVA=%value% ) else if "%key%" == "-delete_exe" ( shift SET DELETE_EXE=true ) else ( shift ) goto parseParams %* goto :eof :uninstall set MYPATH=%~dp0 set PATH_PRUNSRV=%MYPATH% set "PR_LOGPATH=%MYPATH%/../logs" set SERVICE_JAVA=MyApp set DELETE_EXE=false call :parseParams %* set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe echo Removing %SERVICE_JAVA% cmd /c stopService.bat -service_name %SERVICE_JAVA% "%PRUNSRV%" //DS//%SERVICE_JAVA% if "%DELETE_EXE%" == "true" ( del /Q %SERVICE_JAVA%.exe echo %SERVICE_JAVA%.exe deleted ) if not errorlevel 1 goto removed echo. echo Failed uninstalling '%SERVICE_JAVA%' service goto :eof :removed echo The service '%SERVICE_JAVA%' has been removed endlocal @echo onstartService.bat/stopService.bat
It's very easy to implement: just get service name, then use %PRUNSRV% //ES to start the service, use %PRUNSRV% //SS to stop the service.
startService.bat/stopService.bat -service_name service1
startService.bat
@echo off setlocal goto run :parseParams set key=%~1 set value=%~2 echo %key% %value% if "%key%" == "" goto :eof if "%key%" == "-service_name" ( shift shift if "%value%" == "" ( echo Empty value for -service_name goto end ) SET SERVICE_JAVA=%value% ) else if "%key%" == "-install_if_not_exist" ( shift SET INSTALL_IF_NOT_EXIST=true ) else if "%key%" == "-service_description" ( shift shift if "%value%" == "" ( echo Empty value for -service_description goto end ) SET service_description=%value% ) else if "%key%" == "-service_display_name" ( shift shift if "%value%" == "" ( echo Empty value for -service_display_name goto end ) SET service_display_name=%value% ) else if "%key%" == "-start_params" ( shift shift if "%value%" == "" ( echo Empty value for -start_params goto end ) SET start_params=%value% ) else ( shift ) goto parseParams %* goto :eof :run set "PATH_PRUNSRV=%~dp0" set SERVICE_JAVA=MyApp set INSTALL_IF_NOT_EXIST=true set service_display_name=MyApp set service_description="MyApp Service" set start_params="" call :parseParams %* set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe REM if INSTALL_IF_NOT_EXIST is true, check whether the service exists, if false, install the service if needed. SC QUERY %SERVICE_JAVA% > NUL 2>&1 REM if ERRORLEVEL 1060 means it doesn't exists IF ERRORLEVEL 1060 ( echo %SERVICE_JAVA% doesn't exists if "%INSTALL_IF_NOT_EXIST%" == "true" ( echo install_if_not_exist is true, install the service %SERVICE_JAVA%. cmd /c installEmbededJettyService.bat -service_name %SERVICE_JAVA% -service_display_name "%service_display_name%" -service_description "MyApp Service" -start_params "%start_params%" echo service %SERVICE_JAVA% is installed. ) else ( echo install_if_not_exist is false, exit with error. exit -1 ) ) %PRUNSRV% //ES//%SERVICE_JAVA% if %ERRORLEVEL% == 0 ( echo %SERVICE_JAVA% is started. ) else ( echo Failed starting '%SERVICE_JAVA%' service ) :end endlocal @echo onStopService.bat
@echo off setlocal goto run :parseParams set key=%~1 set value=%~2 echo %key% %value% if "%key%" == "" goto :eof if "%key%" == "-service_name" ( shift shift if "%value%" == "" ( echo Empty value for -service_name goto end ) SET SERVICE_JAVA=%value% ) else ( shift ) goto parseParams %* goto :eof :run set PATH_PRUNSRV=%~dp0 set SERVICE_JAVA=MyApp call :parseParams %* set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe "%PRUNSRV%" //SS//%SERVICE_JAVA% rem force kill the service as a safety bet Taskkill /F /FI "SERVICES eq %SERVICE_JAVA%" rem recheck service exists Tasklist /FI "SERVICES eq %SERVICE_JAVA%" 2>NUL | find /I /N "%SERVICE_JAVA%">NUL 2>&1 if %ERRORLEVEL% == 0 ( echo Failed stopping '%SERVICE_JAVA%' service ) else ( echo %SERVICE_JAVA% is stopped. ) :end endlocal @echo onResource
http://commons.apache.org/proper/commons-daemon/procrun.html
Commons Daemon Procrun as a Java Service Wrapper for ActiveMQ