2010/01/23

A better startup script for jboss

One of the approaches to run multiple instances in one box is the Service Binding Manager. The other way, and the recommended one is to use multiple IPs attached to the box and this is for many reasons.
  1. When you have a port conflict, it makes it very difficult to troubleshoot, given a large amount of ports and app servers.
  2. Too many ports makes firewall rules too difficult to maintain.
  3. Isolating the IP addresses gives you a guarantee that no other app server will be using the ports.
  4. Each upgrade requires that you go in and re set the binding manager again. Most upgrades will upgrade the conf/jboss-service.xml file, which has the Service binding manager configuration in it.
  5. The configuration is much simpler. When defining new ports (either through the Service Binding manager or by going in and changing all the ports in the configuration), it's always a headache trying to figure out which ports aren't taken already. If you use a NIC per JBoss Instance, all you have to change is the Ip address binding argument when executing the run.sh or run.bat. (-b ip_address)
  6. Once you get 3 or 4 applications using different ports, the chances really increase that you will step on another one of your applications ports. It just gets more difficult to keep ports from conflicting.
  7. JGroups will pick random ports within a cluster to communicate. Sometimes when clustering, if you are using the same ip address, two random ports may get picked in two different app servers(using the binding manager) that conflict. You can configure around this, but it's better not to run into this situation at all.

In rhel this is relatively easy because the package comes with that ready to implement(i.e. you just need to create some symlinks and modify some text files), but in the case of jboss on windows you have to depend on jboss-native to install the application as a windows service. The problem is that as of jboss native 2.0.8 the service.bat script cannot handle multiples IPs.
For the startup there is no problem, because if we wanted we could "hard-code" the IPs in the run.bat and then the problem is the shutdown where we need to connect to the IP and port 1099 using jnp. If we do that, we end up with IPs in duplicated in the run.bat, and shutdown.bat.

Requirements:
  • jboss.home.dir = D:\Jboss-eap\jbossas
  • jboss.server.home.dir = D:\Jboss-eap\jbossas\server\ThisIsTheTestServer
  • ip = 192.168.10.40
  • jmx user configured (is necessary for the shutdown only)

Once the scripts are copied to the jboss bin directory you need to do the following:

Install the windows service:
D:\>cd \jboss-eap\jbossas\bin
D:\jboss-eap\jbossas\bin>service_ThisIsTheTestServer.bat install

Start the jboss instance from the windows services console (Start -> run... services.msc)
The services name is JB_ThisIsTheTestServer

service_ThisIsTheTestServer.bat is the service control script used to start, stop and restart.
@echo off
REM sets the version of runtime you run.
set JAVA_HOME=D:\Java\jdk1.6.0_16
REM sets the bind ip
set SERVERIP=192.168.10.40
REM sets the multicast ip, it's only used if you have configured the jboss profile to be a cluster and jgroups replicates to the other nodes.
set MCASTIP=229.11.0.4
REM sets the name of the profile used.
set SERVERCFG=ThisIsTheTestServer
REM sets the jmx user/password.
set ADMUSER=-u admin -p CHANGEME

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

set SVCNAME=JB_%SERVERCFG%
set SVCDISP=JB_%SERVERCFG%
set SVCDESC=JBoss_%SERVERCFG%
set NOPAUSE=Y

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

: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 -iwdc %SVCNAME% "%DIRNAME%" "%SVCDISP%" "%SVCDESC%" service_%SERVERCFG%.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
run_%SERVERCFG%.bat  -b %SERVERIP% -c %SERVERCFG% -u %MCASTIP% -Djboss.partition.name=%SERVERCFG% >>run_%SERVERCFG%.log
goto cmdEnd

:cmdStop
call shutdown.bat -S -s jnp://%SERVERIP%:1099 %ADMUSER% >shutdown_%SERVERCFG%.log
goto cmdEnd

:cmdRestart
call shutdown.bat -S -s jnp://%SERVERIP%:1099 %ADMUSER% >>shutdown_%SERVERCFG%log
call run_%SERVERCFG%.bat  -b %SERVERIP% -c %SERVERCFG% -u %MCASTIP% -Djboss.partition.name=%SERVERCFG% >>run_%SERVERCFG%.log
goto cmdEnd

:cmdSignal
@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


run_ThisIsTheTestServer.bat is called by service_ThisIsTheTestServer.bat
@echo off

set TEMP=D:\temp\%SERVERCFG%
set TMP=D:\temp\%SERVERCFG%

if not exist %TEMP% mkdir %TEMP%
if not exist %TMP% mkdir %TMP%

@if not "%ECHO%" == ""  echo %ECHO%
@if "%OS%" == "Windows_NT"  setlocal

set DIRNAME=.\
if "%OS%" == "Windows_NT" set DIRNAME=%~dp0%
set PROGNAME=run_%SERVERCFG%.bat
if "%OS%" == "Windows_NT" set PROGNAME=%~nx0%

pushd %DIRNAME%..
set JBOSS_HOME=%CD%
popd

REM Add bin/native to the PATH if present
if exist "%JBOSS_HOME%\bin\native" set PATH=%JBOSS_HOME%\bin\native;%PATH%
if exist "%JBOSS_HOME%\bin\native" set JAVA_OPTS=%JAVA_OPTS% -Djava.library.path="%PATH%"

set RUNJAR=%JBOSS_HOME%\bin\run.jar
if exist "%RUNJAR%" goto FOUND_RUN_JAR
echo Could not locate %RUNJAR%. Please check that you are in the
echo bin directory when running this script.
goto END

:FOUND_RUN_JAR

if not "%JAVA_HOME%" == "" goto ADD_TOOLS

set JAVA=java

echo JAVA_HOME is not set.  Unexpected results may occur.
echo Set JAVA_HOME to the directory of your local JDK to avoid this message.
goto SKIP_TOOLS

:ADD_TOOLS

set JAVA=%JAVA_HOME%\bin\java

if not exist "%JAVA_HOME%\lib\tools.jar" goto SKIP_TOOLS

set JAVAC_JAR=%JAVA_HOME%\lib\tools.jar

:SKIP_TOOLS

if not "%JAVAC_JAR%" == "" set RUNJAR=%JAVAC_JAR%;%RUNJAR%
if "%JBOSS_CLASSPATH%" == "" set RUN_CLASSPATH=%RUNJAR%
if "%RUN_CLASSPATH%" == "" set RUN_CLASSPATH=%JBOSS_CLASSPATH%;%RUNJAR%

set JBOSS_CLASSPATH=%RUN_CLASSPATH%

set JAVA_OPTS=%JAVA_OPTS% -Dprogram.name=%PROGNAME%

"%JAVA%" -version 2>&1 | findstr /I hotspot > nul
if not errorlevel == 1 (set JAVA_OPTS=%JAVA_OPTS% -server)

set JAVA_OPTS=%JAVA_OPTS% -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000

set JAVA_OPTS=-Djava.awt.headless=true %JAVA_OPTS% 

REM Memory settings
set JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx200m -XX:MaxPermSize=128m
REM Variable definitions in case you need to pass parameters for the application
set JAVA_OPTS=-Dvariable=value %JAVA_OPTS%

REM This is required as it is reducing the signals received from the OS, if it is not setup
REM when you log off, the service stops
set JAVA_OPTS=%JAVA_OPTS% -Xrs

set JBOSS_ENDORSED_DIRS=%JBOSS_HOME%\lib\endorsed

echo ===============================================================================
echo.
echo   JBoss Bootstrap Environment
echo.
echo   JBOSS_HOME: %JBOSS_HOME%
echo.
echo   JAVA: %JAVA%
echo.
echo   JAVA_OPTS: %JAVA_OPTS%
echo.
echo   CLASSPATH: %JBOSS_CLASSPATH%
echo.
echo ===============================================================================
echo.

:RESTART
"%JAVA%" %JAVA_OPTS% "-Djava.endorsed.dirs=%JBOSS_ENDORSED_DIRS%" -classpath "%JBOSS_CLASSPATH%" org.jboss.Main %*
if ERRORLEVEL 10 goto RESTART

:END
if "%NOPAUSE%" == "" pause

:END_NO_PAUSE

shutdown.bat is also called by service_ThisIsTheTestServer.bat common to all the servers
@echo off

if not "%ECHO%" == ""  echo %ECHO%
if "%OS%" == "Windows_NT"  setlocal

set MAIN_JAR_NAME=shutdown.jar
set MAIN_CLASS=org.jboss.Shutdown

set DIRNAME=.\
if "%OS%" == "Windows_NT" set DIRNAME=%~dp0%
set PROGNAME=run.bat
if "%OS%" == "Windows_NT" set PROGNAME=%~nx0%

set ARGS=
:loop
if [%1] == [] goto end
        set ARGS=%ARGS% %1
        shift
        goto loop
:end

set MAIN_JAR=%DIRNAME%\%MAIN_JAR_NAME%
if exist "%MAIN_JAR%" goto FOUND_MAIN_JAR
echo Could not locate %MAIN_JAR%. Please check that you are in the
echo bin directory when running this script.
goto END

:FOUND_MAIN_JAR

if not "%JAVA_HOME%" == "" goto HAVE_JAVA_HOME

set JAVA=java

echo JAVA_HOME is not set.  Unexpected results may occur.
echo Set JAVA_HOME to the directory of your local JDK to avoid this message.
goto SKIP_SET_JAVA_HOME

:HAVE_JAVA_HOME

set JAVA=%JAVA_HOME%\bin\java

:SKIP_SET_JAVA_HOME

set JBOSS_CLASSPATH=%JBOSS_CLASSPATH%;%MAIN_JAR%;%DIRNAME%/../client/jbossall-client.jar

set JAVA_OPTS=%JAVA_OPTS% -Djboss.boot.loader.name=%PROGNAME%

"%JAVA%" %JAVA_OPTS% -classpath "%JBOSS_CLASSPATH%" %MAIN_CLASS% %ARGS%

:END
if "%NOPAUSE%" == "" pause

:END_NO_PAUSE

No comments: