How to run Glassfish 4 Web Application on OpenShift PaaS

There are a few options to publish a Glassfish 4 application in the cloud. One of them is OpenShift Platform-as-a-Service (PaaS), and although it doesn't offer a Glassfish cartridge, it provides a Do It Yourself (DIY) cartridge or application type which could be used to deploy a Glassfish web application.

 If you choose to work with Glassfish application server rather than JBoss or WildFly which are offered by the platform, you workflow will be a little bit different. When working with the Red Hat products, supported by the PaaS, one can publish an application in similar way to publishing to a local application server, particularly when using tools like JBoss Developer Studio. In the case of Glassfish a DIY application containing a Glassfish distribution should be created and then published to OpenShift and a war-file containing your application should be placed in the autodeploy folder of the Glassfish. In fact, you are publishing not your app but a DIY application, which is your Glassfish 4, with a war as the payload, but cannot install Glassfish on OpenShift and push your application directly as in the case of Red Hat products such as WildFly. To publish a new version of your app you have to place a new version of war-file to autodeploy folder of the DIY application and then push it.

 Before publishing the Glassfish AS to the platform its domain.xml file should be edited and some additional action hooks to start and stop the server should be added as described here and here. However, in my opinion, the fastest way to hit the ground running is to use this GitHub repository where all the necessary configuration had been done for you and a sample web application placed into the autodeploy folder. All you need to do is to create your own application on OpenShift, clone its code to your machine, pull the code from the aforementioned Git repository with specifically tweaked Glassfish and then push the result. The sample application will be available at http://Your_App_Name-Your_Domain_Name.rhcloud.com/hello. Just follow the readme.

 Of course, your application will use a database and you will need to read the logs of your application server. To accomplish all the above, after choosing a database cartridge, you may use ssh to connect to you application via address provided to you by the PaaS and obtain all the necessary variables to configure connection pools and data sources in your domain.xml file as well as browse the directories of your application in order to read the output. If you created an app using command line tools all the addresses were printed immediately to you, including one to ssh. Also, the address for remote access can be obtained using the OpenShift web interface. Additionally, don't forget to put a JDBC driver to the lib folder of your Glassfish.

 For my application I used MySql, so the most important for our discussion environmental variables are $OPENSHIFT_MYSQL_DB_PORT, which holds the port of your database connection, $OPENSHIFT_MYSQL_DB_HOST which is an address of the MySql server. If you use, for example Postgres, its environment counterparts could be obtained by using a env | grep DB command. To print the variables use Linux echo command, e.g. echo $OPENSHIFT_MYSQL_DB_PORT. Also a directory structure of your DIY application can be found wherein the variable $OPENSHIFT_REPO_DIR points. There in the diy directory your Glassfish dir can be found, by navigating which the log file can be located.

Comments

Popular posts from this blog

Getting started with Dropwizard: Connecting to a Database using Hibernate

Getting Started with Dropwizard: Authentication, Configuration and HTTPS

Getting started with Dropwizard: First steps