In previous posts, I introduced how to build a package to include embedded jetty, solr.war, and solr.home in one package, reduce size of the package, and to start/stop it programmatically.
Part 1: Shrink Solr Application Size
Part 2: Use Proguard to Shrink Solr Application Size
Part 3: Use Pack200 to Shrink Solr Application Size
Start Stop Embedded Jetty Programmatically
In this article, I would like to introduce how to use Apache commons-daemon to install it as a Windows Service.
First download latest commons-daemon-*-bin-windows.zip from here.
Download latest commons-daemon-*-src.tar.gz from here.
In src\samples folder in commons-daemon-*-src.tar.gz, you can learn how to create a service, and the scripts to install and remove windows services:
ProcrunService.java, ProcrunServiceInstall.cmd, ProcrunServiceRemove.cmd.
The java code to start/stop embedded jetty server is here.
Next we create 2 scripts to install it as a windows service, and remove the windows services.
installEmbededJettyService.bat
@echo off setlocal set MYPATH=%~dp0 set PATH_PRUNSRV=%MYPATH% set SERVICE_JAVA=EmbededJetty set PR_DESCRIPTION=Embedded Jetty Server set PR_DISPLAYNAME=Embedded Jetty Server set "PR_LOGPATH=%MYPATH%/../logs" set PATH="%MYPATH%/../lib/servlet-api-3.0.jar;%MYPATH%/../lib/jetty-all.jar;%MYPATH%/../lib/startjetty.jar" rem Allow prunsrv to be overridden if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv echo Installing %SERVICE_JAVA% %PRUNSRV% //DS//%SERVICE_JAVA% %PRUNSRV% //IS//%SERVICE_JAVA% --Install="%MYPATH%prunsrv" if not errorlevel 1 goto installed echo Failed installing '%SERVICE_JAVA%' service goto end :installed echo The service '%SERVICE_JAVA%' has been installed. set MY_JVMOPTIONS=-server;-Xms512M;-Xmx2048M echo Setting the parameters for %SERVICE_JAVA% %PRUNSRV% //US//%SERVICE_JAVA% --StdOutput auto --StdError auto ^ --Classpath=%PATH% --JvmOptions=%MY_JVMOPTIONS% --Startup=manual ^ --StartMode=java --StartClass=com.codeexample.solr.EmbeddedSolrJettyServer --StartParams=start;-dynamicPort;true;%1;%2;%3;%4;%5;%6 ^ --StopMode=java --StopClass=com.codeexample.solr.EmbeddedSolrJettyServer --StopParams=shutdown if not errorlevel 1 goto updated echo Failed updating '%SERVICE_JAVA%' service goto end :updated echo The service '%SERVICE_JAVA%' has been updated. echo Installation of %SERVICE_JAVA% is complete :end endlocal @echo onuninstallEmbededJettyService.bat
@echo off setlocal set MYPATH=%~dp0 echo %MYPATH% set PATH_PRUNSRV=%MYPATH% set SERVICE_JAVA=EmbededJetty set "PR_LOGPATH=%MYPATH%/../logs" if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv echo Removing %SERVICE_JAVA% %PRUNSRV% //DS//%SERVICE_JAVA% if not errorlevel 1 goto removed echo. echo Failed uninstalling '%SERVICE_JAVA%' service goto end :removed echo The service '%SERVICE_JAVA%' has been removed :end endlocal @echo on
We can copy prunmgr.exe to the bin folder, and rename it as ${service-name}w.exe, so user can run it to edit it, or start, stop it like below:
2 Scripts to start and stop the windows servers.
startService.bat
@echo off setlocal set MYPATH=%~dp0 set PATH_PRUNSRV=%MYPATH% set SERVICE_JAVA=EmbededJetty if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv if [%1] == [] goto startService echo Changing the parameters for %SERVICE_JAVA% %PRUNSRV% //US//%SERVICE_JAVA% --StartParams=start;-dynamicPort;true;%1;%2;%3;%4;%5;%6 if not errorlevel 1 goto updated echo Failed updating '%SERVICE_JAVA%' service goto end :updated echo The service '%SERVICE_JAVA%' has been updated. :startService "%PRUNSRV%" //ES//%SERVICE_JAVA% if not errorlevel 1 goto started echo Failed starting '%SERVICE_JAVA%' service goto end :started echo %SERVICE_JAVA% is started. :end endlocal @echo onstopService.bat
@echo off setlocal set MYPATH=%~dp0 set PATH_PRUNSRV=%MYPATH% set SERVICE_JAVA=EmbededJetty if "%PRUNSRV%" == "" set PRUNSRV=%PATH_PRUNSRV%prunsrv :startService %PRUNSRV% //SS//%SERVICE_JAVA% if not errorlevel 1 goto stopped echo Failed stopping '%SERVICE_JAVA%' service goto end :stopped echo %SERVICE_JAVA% is stopped. :end endlocal @echo on