Running Java apps in the Cloud with GlassFish

The OpenShift Platform-as-a-Service is changing the Java hosting game by not only making it easy to deploy Java apps to the cloud, but also making it FREE to get started.

By default, OpenShift offers excellent support for the JBoss application server, as well as, Jenkins for continuous integration. Not only that, OpenShift uses Apache Maven as the default build system to allow you to push your code via git, and watch the build happen on a remote cloud server.

What happens if you want to just use Tomcat or GlassFish for your Java app?The OpenShift Platform-as-a-Service is changing the Java hosting game by not only making it easy to deploy Java apps to the cloud, but also making it FREE to get started.

By default, OpenShift offers excellent support for the JBoss application server, as well as, Jenkins for continuous integration. Not only that, OpenShift uses Apache Maven as the default build system to allow you to push your code via git, and watch the build happen on a remote cloud server.

What happens if you want to just use Tomcat or GlassFish for your Java app? You simply extend OpenShift to support them using the DIY cartridge.

In this blog post I will detail how to install and configure the GlassFish 3 servlet container for deployment on the OpenShift PaaS.

For those who cannot wait to try GlassFish quickstart app, please jump to the final part :)

Step 1: Sign up and create an OpenShift account

All it takes is an email address to get access to 1.5 GB of memory and 3 GB of storage.

Step 2: Install the OpenShift client tools

Getting the rhc client tools installed takes just one command. Just follow the instructions on the Getting Started page. You are going to need to have Git installed and depending on whether you use Windows, Mac or Linux, it’s going to be some variant of “gem install rhc.”

Step 3: Download GlassFish and give it a few tweaks

Let’s head on over to glassfish.java.net and download the latest version of GlassFish 3.1.2.2.

The only file we need to modify is the config file of default domain(domain1) – glassfish3/glassfish/domains/domain1/config/domain.xml.

We’ll need to make the following modifications to GlassFish to get it to run on OpenShift:

  • Bind http listener to the app’s IP. It’s saved in the environment variable $OPENSHIFT_DIY_IP.
  • Disable other listener like admin console.
  • Update some ports to permitted ones.

Here’s the updated sample domain.xml, and the diff result between original and updated domain.xml:

57c57
<       <property value="localhost" name="serverName" />
---
>       <property value="OPENSHIFT_DIY_IP" name="serverName" />
69c69
<     <node name="localhost-domain1" type="CONFIG" node-host="localhost" install-dir="${com.sun.aas.productRoot}"/>
---
>     <node name="localhost-domain1" type="CONFIG" node-host="OPENSHIFT_DIY_IP" install-dir="${com.sun.aas.productRoot}"/>
75c75
<         <virtual-server id="server" network-listeners="http-listener-1,http-listener-2"/>
---
>         <virtual-server id="server" network-listeners="http-listener-1"/>
78c78
<       <iiop-service>
---
>       <!--iiop-service>
87c87
<       </iiop-service>
---
>       </iiop-service-->
89c89
<         <jmx-connector auth-realm-name="admin-realm" security-enabled="false" address="0.0.0.0" port="8686" name="system" />
---
>         <jmx-connector auth-realm-name="admin-realm" security-enabled="false" address="OPENSHIFT_DIY_IP" port="7600" name="system" />
111c111
<         <jms-host name="default_JMS_host" host="localhost" port="7676" admin-user-name="admin" admin-password="admin" lazy-init="true"/>
---
>         <jms-host name="default_JMS_host" host="OPENSHIFT_DIY_IP" port="5445" admin-user-name="admin" admin-password="admin" lazy-init="true"/>
202c202
<         <jvm-options>-Dosgi.shell.telnet.ip=127.0.0.1</jvm-options>
---
>         <jvm-options>-Dosgi.shell.telnet.ip=OPENSHIFT_DIY_IP</jvm-options>
231c231
<           <protocol security-enabled="true" name="http-listener-2">
---
>           <!--protocol security-enabled="true" name="http-listener-2">
236c236
<           </protocol>
---
>           </protocol-->
244,246c244,246
<           <network-listener port="8080" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool"></network-listener>
<           <network-listener port="8181" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool"></network-listener>
<           <network-listener port="4848" protocol="admin-listener" transport="tcp" name="admin-listener" thread-pool="admin-thread-pool"></network-listener>
---
>           <network-listener address="OPENSHIFT_DIY_IP" port="8080" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool"></network-listener>
>           <!--network-listener port="8181" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool"></network-listener-->
>           <!--network-listener port="4848" protocol="admin-listener" transport="tcp" name="admin-listener" thread-pool="admin-thread-pool"></network-listener-->
261c261
<              <virtual-server id="server" network-listeners="http-listener-1, http-listener-2" >
---
>              <virtual-server id="server" network-listeners="http-listener-1" >
266c266
<          <iiop-service>
---
>          <!--iiop-service>
275c275
<          </iiop-service>
---
>          </iiop-service-->
378c378
<              <jvm-options>-Dosgi.shell.telnet.ip=127.0.0.1</jvm-options>
---
>              <jvm-options>-Dosgi.shell.telnet.ip=OPENSHIFT_DIY_IP</jvm-options>
411c411
<                  <protocol security-enabled="true" name="http-listener-2">
---
>                  <!--protocol security-enabled="true" name="http-listener-2">
416c416
<                  </protocol>
---
>                  </protocol-->
439,441c439,441
<                  <network-listener address="0.0.0.0" port="${HTTP_LISTENER_PORT}" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool" />
<                  <network-listener address="0.0.0.0" port="${HTTP_SSL_LISTENER_PORT}" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool" />
<                  <network-listener port="${ASADMIN_LISTENER_PORT}" protocol="pu-protocol" transport="tcp" name="admin-listener" thread-pool="http-thread-pool" />
---
>                  <network-listener address="OPENSHIFT_DIY_IP" port="9999" protocol="http-listener-1" transport="tcp" name="http-listener-1" thread-pool="http-thread-pool" />
>                  <!--network-listener address="0.0.0.0" port="${HTTP_SSL_LISTENER_PORT}" protocol="http-listener-2" transport="tcp" name="http-listener-2" thread-pool="http-thread-pool" /-->
>                  <!--network-listener port="${ASADMIN_LISTENER_PORT}" protocol="pu-protocol" transport="tcp" name="admin-listener" thread-pool="http-thread-pool" /-->

NOTE: The string OPENSHIFT_DIY_IP here is actually a placeholder for a later update, not the real env var $OPENSHIFT_DIY_IP. You can use any other string here as well.

Step 4: Create a DIY application and customize it

Since GlassFish is not an officially supported application type on OpenShift, we’ll need to create a DIY application, named glassfish for example:

`rhc app create –a glassfishapp –t diy-0.1`  

If a namespace hasn’t been created for your account yet, you’ll be asked to do it before create an app.
rhc domain create -n namespace

Next, move the GlassFish files into the git repo of your app

`cp  -r  path/of/glassfish3  glassfishapp/diy/`

The other code modification is for the start/stop action hooks. This means GlassFish will now be started/stopped when the app is switched on/off like normal apps.

glassfishapp/.openshift/action_hooks/start

#nohup $OPENSHIFT_REPO_DIR/diy/testrubyserver.rb $OPENSHIFT_DIY_IP $OPENSHIFT_REPO_DIR/diy > $OPENSHIFT_DIY_LOG_DIR/server.log 2>&1 &
cd $OPENSHIFT_REPO_DIR/diy/glassfish3/glassfish/domains/domain1/config/
mv domain.xml domain.xml_2
sed 's/'$( grep serverName domain.xml_2 | cut -d\" -f 2 )'/'$OPENSHIFT_DIY_IP'/g' domain.xml_2 > domain.xml
../../../bin/asadmin start-domain &> $OPENSHIFT_DIY_LOG_DIR/server.log

glassfishapp/.openshift/action_hooks/stop

kill `ps -ef | grep glassfish3 | grep -v grep | awk '{ print $2 }'` > /dev/null 2>&1
exit 0

Here are the sample start file and sample stop file.

Step 5: Run!

Let’s commit the GlassFish files and push them to your app:

`cd glassfishapp`  
`git add .`  
`git commit -am "commit glassfish3 files"`  
`git push`

Now, a new GlassFish3 server is running on your OpenShift gear. It will show a GlassFish default page here – http://glassfishapp-yournamespace.rhcloud.com

Step 6: Deploy your war

Put your war in glassfishapp/diy/glassfish3/glassfish/domains/domain1/autodeploy, and push.

For example, I save a openshift.war in here. It will be showed when visiting – http://glassfishapp-yournamespace.rhcloud.com/openshift

That’s it! You now have GlassFish 3 running in the cloud with a little help for a PaaS.

The GlassFish Quickstart sample app

Just wanna run your GlassFish on OpenShift without above steps? Then just use the GlassFish 3 quickstart app on OpenShift’s GitHub page!

Categories
Java, OpenShift Online
Tags
Comments are closed.