JBoss hot deploy

JBoss application server has the ability to hot-deploy Java web apps (.ear, .war, .sar, …) . It is well known for JBoss users already, and the most popular method is “exploded EAR/WAR deployment” :

https://community.jboss.org/wiki/DeployTipsAndBuildSampleScripts

In this post, I just want to share some fun with batch scripting for such deployment! Shell scripts are more flexible than Ant/Maven scripts (build files), as it does not require JRE and easier to use OS commands as well as easier to integrate with CI build bots such as Jenkins/Hudson.

On Linux/Unix, shell scripting is very strong and can do almost everything ! On Windoze the batch scripting is kind of dull, and the new PowerShell is not too bad, but there will be fun when you ‘conquered’ the task with such dull ‘weapon’ .

First, a batch script to explode the EAR file (given it’s already copied to the deployed folder).

explode_EAR_deployment.bat

@echo off
title Explode EAR file for AppServer like JBoss - DucQuoc 2012

set DEPLOY_DIR=D:\USR\SOA\jb43\server\default\deploy
set TARGET_EAR=standard-edition-4.1.0-SNAPSHOT.ear
set BACKUP_DIR=%DEPLOY_DIR%\..

:explode_big_ear
MOVE /y %DEPLOY_DIR%\%TARGET_EAR% %BACKUP_DIR%\%TARGET_EAR%.zip
PUSHD %BACKUP_DIR%\%TARGET_EAR%.zip && POPD && echo Test_Directory
if NOT %errorlevel% EQU 0 (
  unzip %BACKUP_DIR%\%TARGET_EAR%.zip -d %DEPLOY_DIR%\%TARGET_EAR%
  rem DEL /f /s /q %BACKUP_DIR%\%TARGET_EAR%.zip
) ELSE (
  XCOPY /y /e %BACKUP_DIR%\%TARGET_EAR%.zip %DEPLOY_DIR%\%TARGET_EAR%\
  rem RMDIR /s /q %BACKUP_DIR%\%TARGET_EAR%.zip
)

:explode_wars_of_ear
for %%i in (%DEPLOY_DIR%\%TARGET_EAR%\*.war) do (
  MOVE /y %%i %BACKUP_DIR%\%%~nxi.zip 
  unzip %BACKUP_DIR%\%%~nxi.zip -d %%i
)
rem DEL /f /s /q %BACKUP_DIR%\*.war.zip

:explode_some_jars
set JAR_FILES_REGEX="^ui-sc ^pl-repository-old ^pm-validation"
for %%i in (%DEPLOY_DIR%\%TARGET_EAR%\lib\*.jar) do (
  echo %%~nxi | findstr /r %JAR_FILES_REGEX% > NUL
  if NOT errorlevel 1 (
    REN %%i %%~nxi.zip 
    unzip %%i.zip -d %%i
  )
)

:touch_ear_descriptor
set DESCRIPTOR_FILE=%DEPLOY_DIR%\%TARGET_EAR%\META-INF\application.xml
copy /b %DESCRIPTOR_FILE%+nul %DESCRIPTOR_FILE%
rem CD %DEPLOY_DIR%\%TARGET_EAR%\META-INF && copy /b application.xml +,,

:extra_tasks
rem CALL "%~dp0./make_appserver_debuggable.bat"

:end

.

To enable remote debug ability for Jboss AS, we can change JAVA_OPTS so that when run Java it has JDWP running at specified port.

set X64_OPTS=-Xmx2048m
set DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n 
echo %JAVA_OPTS% | findstr /c:jdwp || set JAVA_OPTS=%DEBUG_OPTS% %X64_OPTS% %JAVA_OPTS%
rem if %JAVA_OPTS%==%JAVA_OPTS:jdwp=% set JAVA_OPTS=%DEBUG_OPTS% %X64_OPTS% %JAVA_OPTS%

(these can be put in run.bat of JBoss, but can also in another env script, or run.conf respectively)

Note that the options -Xnoagent and -Djava.compiler=NONE can support remote debugging too, but can also affect the startup time of the application servers.

Besides, if you have some URL to download the latest archive file (maven public repo, or CI build server such as Jenkins), you can use below batch script to download it:

download_file.bat

@echo off
title Download installer archive - DucQuoc 2012

set TARGET_DIR=D:\
set TARGET_FILE=installer-4.10-uncrippled.jar
set BUILD_URL=http://build-jenkins.myserver.com:8080/view/All/job/installer-4.10-build
REM set BUILD_URL=http://build-jenkins.myserver.com:8080/view/All/job/installer-ea-build/
set ARTIFACT_PATH=/lastSuccessfulBuild/artifact/distributions/installer/target/
set DOWNLOAD_PATH=%BUILD_URL%/%ARTIFACT_PATH%/%TARGET_FILE%

set BACKUP_DIR=%TARGET_DIR%
set CURR_DATE=%date:~10,4%_%date:~4,2%_%date:~7,2%
set CURR_TIME=%time:~0,2%_%time:~3,2%_%time:~6,2%

IF NOT [%1]==[] (
  set DOWNLOAD_PATH=%1
)

:back_up_old_file
MKDIR %BACKUP_DIR%
MOVE /y %TARGET_DIR%\%TARGET_FILE% %BACKUP_DIR%\%CURR_DATE%-%CURR_TIME%-%TARGET_FILE%

:download
wget --version
IF %ERRORLEVEL% EQU 0 (
  wget %DOWNLOAD_PATH% -O %TARGET_DIR%\%TARGET_FILE%
) ELSE (
  bitsadmin /transfer myJob /download /priority foreground %DOWNLOAD_PATH% %TARGET_DIR%\%TARGET_FILE%
)

:extra_tasks
CALL "%~dp0.\deploy_auto.bat"

:end

(Note: in my case, at the end of download script it will call deploy_auto script to install and deploy the EAR to Jboss, which later calls the explode_EAR_deployment.batch )

deploy_auto.bat

@echo off
title Deploy EAR from installer automatically (non-interactive) - dq 2012

set INSTALLER_FILE=D:\installer-4.10-uncrippled.jar
set CONFIG_FILE=D:\TEST_DATA\scripts_auto_jb43\configuration-properties.xml

set TARGET_ROOT=D:\SOA
set TARGET_HOME=%TARGET_ROOT%\4.10b_jb43

set BACKUP_DIR=%TARGET_HOME%\..
set CURR_DATE=%date:~10,4%_%date:~4,2%_%date:~7,2%
set CURR_TIME=%time:~0,2%_%time:~3,2%_%time:~6,2%

:check_java
IF NOT "%JRE_HOME%"=="" set JAVA_COMMAND="%JRE_HOME%\bin\java"
IF NOT "%JAVA_HOME%"=="" set JAVA_COMMAND="%JAVA_HOME%\bin\java"
IF "%JAVA_COMMAND%"=="" set JAVA_COMMAND="java"
%JAVA_COMMAND% -version
IF NOT %errorlevel% EQU 0 (
  echo ERROR: JAVA_HOME not found. Please set the JAVA_HOME variable, or include java.exe in your PATH
  goto end
)

:backup
MKDIR %BACKUP_DIR%
MOVE /y %TARGET_HOME% %BACKUP_DIR%\%CURR_DATE%-%CURR_TIME%-BACKUP
MKDIR %TARGET_HOME%

REM set APPSERVER_HOME=%TARGET_ROOT%\jb43
REM set ARCHIVE_APPSERVER=D:\BACKUP\Java_ALL\jboss-4.3.0.GA.zip
REM IF NOT EXIST %APPSERVER_HOME%\* unzip %ARCHIVE_APPSERVER% -d %APPSERVER_HOME%

:install
echo Checking config %CONFIG_FILE% && findstr /r "db.user db.password db.url shared.as" %CONFIG_FILE%
%JAVA_COMMAND% -jar %INSTALLER_FILE% --install-to %TARGET_HOME% --use-config %CONFIG_FILE% --debug %*

:extra_tasks
rem CALL "%~dp0.\explode_EAR_deployment.bat"

:end

make_appserver_debuggable.bat

@echo off
title Enable remote debug to app server and slim a bit - dq 2012

set APPSERVER_BIN_DIR=D:\SOA\jb43\bin
set TARGET_RUN_SCRIPT=run.bat
set DEBUG_RUN_SCRIPT=Z:\temp\qmduc\run_debug_jb43.bat

set DEPLOY_DIR=D:\SOA\jb43\server\default\deploy
set BACKUP_DIR=%DEPLOY_DIR%\..

:copy_debuggable_run
ECHO F | XCOPY /d /h %APPSERVER_BIN_DIR%\%TARGET_RUN_SCRIPT% %APPSERVER_BIN_DIR%\%TARGET_RUN_SCRIPT%.BACKUP
COPY /y %DEBUG_RUN_SCRIPT% %APPSERVER_BIN_DIR%\%TARGET_RUN_SCRIPT%

:slim_appserver
set SLIM_FILES_REGEX="^cache-invalidation ^client-deployer ^product-doc "
for %%i in (%DEPLOY_DIR%\*) do (
  echo %%~nxi | findstr /r %SLIM_FILES_REGEX%
  if NOT errorlevel 1 (
    MOVE /y %%i %BACKUP_DIR%\%%~nxi
  )
)

set SLIM_DIRS_REGEX="^jmx-console ^http-invoker ^admin-console ^jbossws"
for /d %%i in (%DEPLOY_DIR%\*) do (
  echo %%~nxi | findstr /r %SLIM_DIRS_REGEX% && MOVE /y %%i %BACKUP_DIR%\%%~nxi
)

:extra_tasks
rem CALL "%~dp0./serverstart.bat"

:end

glue them all: unlease_auto_install.bat

@echo off
title Unlease the power of scripting: automatic install - dq 2012

set START_TIME=%time%
set SCRIPTS_DIR=%~dp0
if NOT "%OS%"=="Windows_NT" (
  set SCRIPTS_DIR=.\
)
set SERVER_BIN_DIR=D:\SOA\4.10b_jb5\bin

:call_all
rem CALL "%SCRIPTS_DIR%\download_file.bat"

CALL "%SCRIPTS_DIR%\deploy_auto.bat"

CALL "%SCRIPTS_DIR%\explode_EAR_deployment.bat"

CALL "%SCRIPTS_DIR%\make_appserver_debuggable.bat"

CALL "%SCRIPTS_DIR%\touch_EAR_descriptor.bat"

echo Finished installation at %date% %time% (started from %START_TIME%). Now starting server...
CALL "%SERVER_BIN_DIR%\serverstart.bat"

:end

Some more tips for JBoss hot-deploy:

.

./.

About DucQuoc.wordpress.com

A coder, brother, and player at times...
This entry was posted in Coding, Skill. Bookmark the permalink.

2 Responses to JBoss hot deploy

  1. srinu says:

    In Jboss what is the soft deployement ? & what is the hard deployement?

  2. Hi Srinu, I think you can find the answers on JBoss forum or via Google. I’m not an JBoss expert – I just wanted to share some of my experience with JBoss here :) .

    .

    New Tip on JBoss AS 7+ (and EAP/WildFly):
    Besides deployment-scanner Auto-Deploy-Exploded,

    <deployment-scanner scan-interval="2000" auto-deploy-exploded="true" relative-to="jboss.server.base.dir" path="deployments" />

    there is the option (in standalone.xml or standalone-full.xml) to support JSP web deployment, and actually also support hot deploy for Java classes.

    <jsp-configuration development="true" check-interval="1" modification-test-interval="1" recompile-on-fail="true"/>

    Therefore, if the option is not set or set to development=”false”, users may change to “true” for hot-deploy support, which accelerate development process.

    Bonus: a simple batch script (using Win 7 PowerShell) to replace the jsp-configuration :


    :update_jboss7_jsp
    SET APPSERVER_CONFIG_FILE=D:\jboss7\standalone\configuration\standalone-full.xml
    powershell -Command "(gc %APPSERVER_CONFIG_FILE%) -replace '<jsp-configuration />', '<jsp-configuration development=\"true\"/>' | Out-File -encoding utf8 %APPSERVER_CONFIG_FILE%"
    powershell -Command "(gc %APPSERVER_CONFIG_FILE%) -replace '<jsp-configuration development=\"false\"/>', '<jsp-configuration development=\"true\"/>' | Out-File -encoding utf8 %APPSERVER_CONFIG_FILE%"
    powershell -Command "(gc %APPSERVER_CONFIG_FILE%) -replace '<jsp-configuration development=\"true\"/>', '<jsp-configuration development=\"true\" check-interval=\"1\" modification-test-interval=\"1\" recompile-on-fail=\"true\"/>' | Out-File -encoding utf8 %APPSERVER_CONFIG_FILE%

    .

    Hope this helps,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s