Solr: Run Embedded Jetty As Windows Service


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 on
uninstallEmbededJettyService.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 on
stopService.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

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)