How to launch the hosted mode with Jonas and Maven

GWT uses an embedded Jetty server. It is used at the beginning of the development of an application. During the prototype phase.
Personally, I used it for about a year, until we got the need to use JMS.
And since Jetty is not a Java EE server – it does not implement the JMS API – it was time to say goodbye to Jetty and use the application server that is used in production : Jonas.
There is not much info about the configuration to use in order to launch the hosted mode with a server different from Jetty. I found that the official website of the gwt-maven-plugin lacks information in that area.

I spent a few hours finding out the correct configuration for that. So here is how to do it :
1) Start Jonas
2) Deploy the application (EAR or WAR)
3) Run the goal gwt:run

And the configuration for the gwt-maven-plugin plugin is the following :


<plugins>
  <plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>gwt-maven-plugin</artifactId>		
					
	<!-- Old configuration, to run the hosted mode with JETTY. -->
	<!-- 
		<configuration>
		<runTarget>identify.html</runTarget>
		<hostedWebapp>${project.build.directory}/${project.build.finalName}</hostedWebapp>
		<modules>
			<module>${project.groupId}.foo.bla.MainApplication</module>
			<module>${project.groupId}.foo.bla.SecondEntry</module>
		</modules>
		<copyWebapp>true</copyWebapp>										
		<extraJvmArgs>-XX:MaxPermSize=512M  -Xms512M -Xmx1024M </extraJvmArgs>
	</configuration>
	 -->
	
	<!-- New configuration, to run the hosted mode with JONAS -->
	<configuration>					
		<modules>
			<module>${project.groupId}.foo.bla.MainApplication</module>
			<module>${project.groupId}.foo.bla.SecondEntry</module>
		</modules>					
		<extraJvmArgs>-XX:MaxPermSize=512M  -Xms512M -Xmx1024M </extraJvmArgs>
		
		<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory> 
		<runTarget>http://localhost:9000/app-context/main.html</runTarget> 
		<copyWebapp>false</copyWebapp> 
		<!--  do not start JETTY -->
		<noServer>true</noServer>
		<!-- the folder where the exploded WAR is located, inside Jonas -->
		 <!--  Constant path if jonas.development=false in conf/jonas.properties --> 					
		<hostedWebapp>${env.JONAS_BASE}\work\webapps\jonas\ear\myapp-ear-SNAPSHOT.ear\${project.build.finalName}.war</hostedWebapp>
		<bindAddress>localhost</bindAddress>   <!--  other possible value : 0.0.0.0 -->
		<logLevel>INFO</logLevel> 
		<style>OBF</style> 			
	</configuration>
	<executions>
		<execution>
			<id>gwtcompile</id>
			<phase>prepare-package</phase>
			<goals>
				<goal>compile</goal>
			</goals>
		</execution>
	</executions>
   </plugin>
...
</plugins>			

Line 35 is the most important : it specifies the path where the exploded war is deployed in Jonas.
That configuration could work with another application server of course.

How to configure the module mod_jk with Apache and Jonas ?

Apache is often the web server used in front of an application server (for instance Jonas).
You need a module like mod_jk to configure Apache with Jonas. Here are the details to configure that module (under RedHat Linux) :

1) Download and install the module mod_jk
mod_jk is an Apache module which can be used to forward a client HTTP request to an internal application server, using the Apache JServ Protocol (AJP).
To get it, type the following commands :

cd /usr/lib64/httpd/modules
wget http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/x86_64/mod_jk-1.2.31-httpd-2.2.x.so
chmod 755 mod_jk.so
/etc/init.d/httpd restart

The module will show up in the modules folder of Apache : /etc/httpd/modules

2) Configure Apache and mod_jk
First, you need to load the mod_jk module into Apache when it starts.
In the main Apache configuration file, /etc/httpd/conf/httpd.conf, add the following line :

LoadModule jk_module modules/mod_jk.so

Then you need to specify the path to the workers properties file and configure the contexts and the workers which will handle these contexts :

<IfModule jk_module>

JkWorkersFile /etc/httpd/conf/workers.properties
JkShmFile /etc/httpd/logs/mod_jk.shm
JkLogFile /etc/httpd/logs/mod_jk.log
JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkRequestLogFormat "%w %m %V %T"

JKMountCopy All

# Send requests for context /blabla/* to worker named worker1
JkMount /blabla/* worker1
# Send requests for context /blabla* to worker named worker1
jkMount /blabla* worker1
# Send requests for context /blabla to worker named worker1
jkMount /blabla worker1
# Send requests for context /blabla/ to worker named worker1
jkMount /blabla/ worker1
# Send requests for context /anotherContext* to worker named worker1
jkMount /anotherContext* worker1

The line JKMountCopy All is very important. It copies the mount point definitions in all the virtual hosts.
Create and add the file workers.properties to /etc/httpd/conf/ :

# Worker list
worker.list=worker1
# Define worker1
worker.worker1.port=8009
worker.worker1.host=127.0.0.1
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
worker.worker1.cachesize=10
# Load-balancer
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=worker1
worker.loadbalancer.sticky_session=1
worker.loadbalancer.local_worker_only=1

Here I have created only one worker (worker1) because I just want to forward requests to an instance of Jonas. There is no load-balancing in this configuration since there is only one single instance.
loadbalancer is not a real worker, it is responsible for the management of several “real” workers.

3) Configure Jonas
To enable AJP connections to the 8009 port of the Jonas server, you need to create an AJP connector in the $Jonas_Base/conf/tomcat6-server.xml and $Jonas_Base/conf/tomcat7-server.xml files :

  <!-- Define an AJP 1.3 Connector on port 9009 -->    
    <Connector port="8009" protocol="AJP/1.3" redirectPort="9043" />
  <!-- An Engine represents the entry point (within JOnAS/Tomcat) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host). -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="JOnAS" defaultHost="localhost" jvmRoute="worker1">

Here is a schema that sums things up:

Links :
http://tomcat.apache.org/connectors-doc/ajp/ajpv13a.html
http://jonas.ow2.org/current/doc/doc-en/integrated/configuration_guide.html#N11C7D
http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html

Start JoNaS server from Jenkins with a shell script

Continuous integration is a process that involves the build of the project but also the deployment of the artefacts (the term used with Maven). These artefacts are archives such as EAR, WAR, JAR files.
I had to write a shell script in Jenkins that would run just after the build and that would stop the JoNaS server, deploy the artefacts and restart JoNaS.

The project is based on Maven. However to create a job that executes a shell script, the first option “Build a free-style software project” seems best, instead of the “Build a maven2/3 project” option.
The process is quite simple :
1) stop JoNaS
2) delete the previous EAR file
3) copy the EAR file that was just built to the JoNaS deploy folder
4) restart JoNaS

I chose to poll the SCM every 10mn. So that means JoNaS checks every 10mn if a commit occured during the last 10mn. If so, it builds the project again. And deploys it.

Notice the highlighted line export BUILD_ID=dontKillMe. This is very important.
I spent about 2 hours wondering why JoNaS would start and then would stop after 20 or 30 seconds.
The reason is that Jenkins attempts to clean up after itself, so all processes that have that build ID are killed by default. So that processes do not accidentally leak and run the machine out of memory, for one.

The issue is described in this page with a funny title :
https://issues.jenkins-ci.org/browse/JENKINS-2729
Other interesting link :
http://wiki.hudson-ci.org/display/HUDSON/ProcessTreeKiller

Starting JOnAS as a Service on Linux

Here is a startup script for JOnAS, on Linux. It is a nice way to automatically start JOnAS when Linux reboots. It is quite trivial to write but I am sure it will turn out useful for anyone who is still not familiar with startup scripts on Linux. Call it jonas and save it in the directory /etc/init.d. You must be root to do that.

#! /bin/bash
# chkconfig: 2345 95 20
# description: Description of the script
# processname: jonas 
#
#jonas Start the jonas server.
#

NAME="Jonas 5.2.1"
JONAS_HOME=/home/test/jonas-full-5.2.1
JONAS_USER=test
LC_ALL=fr_FR
export JONAS_HOME  JONAS_USER LC_ALL
cd $JONAS_HOME/logs
case "$1" in
  start)
    echo -ne "Starting $NAME.\n"
    /bin/su $JONAS_USER -c "$JONAS_HOME/bin/jonas -bg start "
    ;;

  stop)
    echo -ne "Stopping $NAME.\n"
    /bin/su $JONAS_USER -c "$JONAS_HOME/bin/jonas stop "
    ;;

  *)
    echo "Usage: /etc/init.d/jonas {start|stop}"
    exit 1
    ;;
esac

exit 0

Continue reading