Running different JBoss instances as separate windows service for multi-instance JBoss configuration


* Deploying JBoss instance as separate windows services *

Actually in my last couple of posts, I have explained how to cofigure JBoss for multi-instance run (like single machine-single JBoss with different instances OR single machine-multiple JBoss - multiple instances). So here I will explain how to deploy them as separate windows services, to start/stop/restart them independently. 

Note -
•    You must have necessary files, for installing JBoss as service.(In case you don't, Please read my previous Blog post - click here).
•    You have properly configured JBoss multi instance configurations.(Please read my previous posts if you don't know how to).

Steps :-
1)  In order to run each JBoss instance as a separate windows service, you must have a separate service wrapper for each JVM run. i.e. there should be a separate service.bat file for each instance of JBoss.
So copy, paste & rename these service.bat files.
e.g. consider you have four different JBoss instances and four service wrapper batch scripts for them namely
* service-default.bat
* service-node1.bat
* service-node2.bat
* service-node3.bat
So your JBoss server folder would look like -

and your JBoss bin directory like -


2) Now, Modify each service batch script file for each instance.
The original service.bat is like -
@echo off
REM JBoss, the OpenSource webOS
REM
REM Distributable under LGPL license.
REM See terms of license at gnu.org.
REM
REM -------------------------------------------------------------------------
REM JBoss Service Script for Windows
REM -------------------------------------------------------------------------


@if not "%ECHO%" == "" echo %ECHO%
@if "%OS%" == "Windows_NT" setlocal
set DIRNAME=%CD%

REM
REM VERSION, VERSION_MAJOR and VERSION_MINOR are populated
REM during the build with ant filter.
REM
set SVCNAME=JBAS326SVC
set SVCDISP=JBoss Application Server 3.2.6
set SVCDESC=JBoss Application Server 3.2.6/Platform: Windows x86
set NOPAUSE=Y

REM Suppress killing service on logoff event
set JAVA_OPTS=-Xrs

REM Figure out the running mode

if /I "%1" == "install"   goto cmdInstall
if /I "%1" == "uninstall" goto cmdUninstall
if /I "%1" == "start"     goto cmdStart
if /I "%1" == "stop"      goto cmdStop
if /I "%1" == "restart"   goto cmdRestart
if /I "%1" == "signal"    goto cmdSignal
echo Usage: service install^|uninstall^|start^|stop^|restart^|signal
goto cmdEnd

REM jbosssvc retun values
REM ERR_RET_USAGE           1
REM ERR_RET_VERSION         2
REM ERR_RET_INSTALL         3
REM ERR_RET_REMOVE          4
REM ERR_RET_PARAMS          5
REM ERR_RET_MODE            6

:errExplain
if errorlevel 1 echo Invalid command line parameters
if errorlevel 2 echo Failed installing %SVCDISP%
if errorlevel 4 echo Failed removing %SVCDISP%
if errorlevel 6 echo Unknown service mode for %SVCDISP%
goto cmdEnd

:cmdInstall
jbosssvc.exe -imwdc %SVCNAME% "%DIRNAME%" "%SVCDISP%" "%SVCDESC%" service.bat
if not errorlevel 0 goto errExplain
echo Service %SVCDISP% installed
goto cmdEnd

:cmdUninstall
jbosssvc.exe -u %SVCNAME%
if not errorlevel 0 goto errExplain
echo Service %SVCDISP% removed
goto cmdEnd

:cmdStart
REM Executed on service start
del .r.lock 2>&1 | findstr /C:"being used" > nul
if not errorlevel 1 (
  echo Could not continue. Locking file already in use.
  goto cmdEnd
)
echo Y > .r.lock
jbosssvc.exe -p 1 "Starting %SVCDISP%" > run.log
call run.bat < .r.lock >> run.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> run.log
del .r.lock
goto cmdEnd

:cmdStop
REM Executed on service stop
echo Y > .s.lock
jbosssvc.exe -p 1 "Shutting down %SVCDISP%" > shutdown.log
call shutdown -S < .s.lock >> shutdown.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> shutdown.log
del .s.lock
goto cmdEnd

:cmdRestart
REM Executed manually from command line
REM Note: We can only stop and start
echo Y > .s.lock
jbosssvc.exe -p 1 "Shutting down %SVCDISP%" >> shutdown.log
call shutdown -S < .s.lock >> shutdown.log 2>&1
del .s.lock
:waitRun
REM Delete lock file
del .r.lock > nul 2>&1
REM Wait one second if lock file exist
jbosssvc.exe -s 1
if exist ".r.lock" goto waitRun
echo Y > .r.lock
jbosssvc.exe -p 1 "Restarting %SVCDISP%" >> run.log
call run.bat < .r.lock >> run.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> run.log
del .r.lock
goto cmdEnd

:cmdSignal
REM Send signal to the service.
REM Requires jbosssch.dll to be loaded in JVM
@if not ""%2"" == """" goto execSignal
echo Missing signal parameter.
echo Usage: service signal [0...9]
goto cmdEnd
:execSignal
jbosssvc.exe -k%2 %SVCNAME%
goto cmdEnd

:cmdEnd

    There are few things(marked in as red in above) you should be careful about -
  • SVCNAME – Name of windows service, it must be unique
  • SVCDISP – Display name of your service.
  • SVCDESC=Description of your service.
  • SVCSTARTPARAMS (this is a custom variable created)
  • SVCSHUTPARAMS (this is a custom variable created)
  • Run-time VM arguments passed to run.bat
  • Run-time VM arguments passed to shutdown.bat
  • lock file name (this name must be unique for each JBoss instance)
Here is my service wrapper batch script for my node1 (a JBoss instance) - i.e. service-node1.bat
@echo off
REM JBoss, the OpenSource webOS
REM
REM Distributable under LGPL license.
REM See terms of license at gnu.org.
REM
REM -------------------------------------------------------------------------
REM JBoss Service Script for Windows
REM -------------------------------------------------------------------------


@if not "%ECHO%" == "" echo %ECHO%
@if "%OS%" == "Windows_NT" setlocal
set DIRNAME=%CD%

REM
REM VERSION, VERSION_MAJOR and VERSION_MINOR are populated
REM during the build with ant filter.
REM
set SVCNAME=JBAS422SVC1
set SVCDISP=JBoss 4.2.2 node 1
set SVCDESC=JBoss Application Server 4.2.2 node 1/Platform: Windows x86
set NOPAUSE=Y
set SVCSTARTPARAMS=-c node1 -b 0.0.0.0 -Djboss.messaging.ServerPeerID=1 -Dorg.jboss.logging.Log4jService.catchSystemOut=false
set SVCSHUTPARAMS=-s 0.0.0.0:1199 -S

REM Suppress killing service on logoff event
set JAVA_OPTS=-Xrs

REM Figure out the running mode

if /I "%1" == "install"   goto cmdInstall
if /I "%1" == "uninstall" goto cmdUninstall
if /I "%1" == "start"     goto cmdStart
if /I "%1" == "stop"      goto cmdStop
if /I "%1" == "restart"   goto cmdRestart
if /I "%1" == "signal"    goto cmdSignal
echo Usage: service install^|uninstall^|start^|stop^|restart^|signal
goto cmdEnd

REM jbosssvc retun values
REM ERR_RET_USAGE           1
REM ERR_RET_VERSION         2
REM ERR_RET_INSTALL         3
REM ERR_RET_REMOVE          4
REM ERR_RET_PARAMS          5
REM ERR_RET_MODE            6

:errExplain
if errorlevel 1 echo Invalid command line parameters
if errorlevel 2 echo Failed installing %SVCDISP%
if errorlevel 4 echo Failed removing %SVCDISP%
if errorlevel 6 echo Unknown service mode for %SVCDISP%
goto cmdEnd

:cmdInstall
jbosssvc.exe -imwdc %SVCNAME% "%DIRNAME%" "%SVCDISP%" "%SVCDESC%" service-node1.bat
if not errorlevel 0 goto errExplain
echo Service %SVCDISP% installed
goto cmdEnd

:cmdUninstall
jbosssvc.exe -u %SVCNAME%
if not errorlevel 0 goto errExplain
echo Service %SVCDISP% removed
goto cmdEnd

:cmdStart
REM Executed on service start
del .rn1.lock 2>&1 | findstr /C:"being used" > nul
if not errorlevel 1 (
  echo Could not continue. Locking file already in use.
  goto cmdEnd
)
echo Y > .rn1.lock
jbosssvc.exe -p 1 "Starting %SVCDISP%" > node1-instance.log
call run.bat %SVCSTARTPARAMS% < .rn1.lock >> node1-instance.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> node1-instance.log
del .rn1.lock
goto cmdEnd

:cmdStop
REM Executed on service stop
echo Y > .sn1.lock
jbosssvc.exe -p 1 "Shutting down %SVCDISP%" > node1-instance-shutdown.log
call shutdown %SVCSHUTPARAMS% < .sn1.lock >> node1-instance-shutdown.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> node1-instance-shutdown.log
del .sn1.lock
goto cmdEnd

:cmdRestart
REM Executed manually from command line
REM Note: We can only stop and start
echo Y > .sn1.lock
jbosssvc.exe -p 1 "Shutting down %SVCDISP%" >> node1-instance-shutdown.log
call shutdown %SVCSHUTPARAMS% < .sn1.lock >> node1-instance-shutdown.log 2>&1
del .sn1.lock
:waitRun
REM Delete lock file
del .rn1.lock > nul 2>&1
REM Wait one second if lock file exist
jbosssvc.exe -s 1
if exist ".rn1.lock" goto waitRun
echo Y > .rn1.lock
jbosssvc.exe -p 1 "Restarting %SVCDISP%" >> node1-instance.log
call run.bat %SVCSTARTPARAMS% < .rn1.lock >> node1-instance.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> node1-instance.log
del .rn1.lock
goto cmdEnd

:cmdSignal
REM Send signal to the service.
REM Requires jbosssch.dll to be loaded in JVM
@if not ""%2"" == """" goto execSignal
echo Missing signal parameter.
echo Usage: service signal [0...9]
goto cmdEnd
:execSignal
jbosssvc.exe -k%2 %SVCNAME%
goto cmdEnd

:cmdEnd

IMP Note – 
a) The parameters passed to shutdown.bat for stopping a particular JBoss instance's JVM are different, please refer to its help in case you have as ambiguity.
b) I had used, JNDI URL for that instance  e.g. “shutdown.bat -s 0.0.0.0:1099 -S” which stops JNDI service deployed on this IP-Address and on this particular port.
c) If you have used separate IPs for each instance then we just need – “shutdown.bat -s <ip_address> -S” which by default halts JNDI service on port 1099.
d) In case JNDI service is started on different port change above command accordingly.

Here are some screen-shots to make it clear what parameters in what section of this service wrapper needs to be changed








3) Now install these service wrappers as windows service.

Which will install them as separate windows services,

4) Start these services one-by-one or whichever are required. It is advised to keep some time in between each instance start to avoid any conflict of file locks, start-ups,ports, etc.

Comments

  1. The SVCSTARTPARAMS and SVCSHUTPARAMS variables won't be assigned because there are spaces either side of the equals sign. Should be:

    set SVCSTARTPARAMS=-c node1 -b 0.0.0.0 -Djboss.messaging.ServerPeerID=1 -Dorg.jboss.logging.Log4jService.catchSystemOut=false
    set SVCSHUTPARAMS=-s 0.0.0.0:1199 -S

    ReplyDelete
    Replies
    1. Thanks Nick, I am updating it. Sorry for the inconvenience, while writing the blog I must have forgotten that. I will update it.

      Delete

Post a Comment

Popular posts from this blog

How to install / Configure Mantis For Apache-PHP-PostgreSQL combination

Modified ExtJS LOVCombo (List of Values) ux plugin - with select all, deselect all, text filter, bubble up and bubble down of selection

TriggerField with two triggers, TwinTriggerField with tooltips