Openshift

Java EE example application on Openshift

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

In the first tutorial Openshift installation quick tutorial we have learnt how to install Openshift Origin and run an Helloworld application on it. Let's see a more complex example which uses our demo JEE application configured to use a RDBMs.

Start your openshift cluster at first:

$ oc cluster up --version=latest

Once done, access your console at: https://192.168.1.66:8443

Our demo application will be using an PostgreSQL RDBMs, so as first step let's add to the project from the Catalog the postgresql image:

Running a Java EE application on Openshift

Please note that we are using a PostgreSQL ephemeral service, without persistent storage. Any data stored will be lost upon pod destruction. Only use this template for testing

In the next screen accept the default parameters that will be used to define the Database service name and the security credentials:

Running a Java EE application on Openshift

In a few minutes the PostgreSQL service will be available and a Pod for it will be created:

Running a Java EE application on Openshift

Let's check the Deployment configuration so that we can copy the Environment Variables used to start the service:

Running a Java EE application on Openshift

Hint: if you access the YAML configuration file, it will be easier to copy and paste the environment variables in a notepad

Now let's add to the Project a WildFly image, which is contained in the Java Catalog:

Running a Java EE application on Openshift

Start by defining the resource name. Then enter the repository URL. I have uploaded the source code for this example on: https://github.com/fmarchioni/openshift-jee-sample

Running a Java EE application on Openshift

Next, expand the Advanced options and add to the Runtime environment variables, the variables defined by PostgreSQL ealier:

Running a Java EE application on Openshift

Now click on Create and wait that your service becomes available and the Pod is running:

Running a Java EE application on Openshift

Behind the scenes the WildFly template defines two extra Datasources: one bound at java:jboss/datasources/MySQLDS (To be used with a MySQL Database) and another bound at: java:jboss/datasources/PostgreSQLDS To be used with a PostgreSQL Database)

From the Pod logs we can verify that the application server has started correclty and that the application has been deployed to the ROOT Context:

=========================================================================

  JBoss Bootstrap Environment

  JBOSS_HOME: /wildfly

  JAVA: java

  JAVA_OPTS:  -server -XX:+UseSerialGC -Xms40m -Xmx256m -XX:+AggressiveOpts -Dorg.apache.tomcat.util.LOW_MEMORY=true -DOPENSHIFT_APP_UUID= -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Dorg.jboss.resolver.warning=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8 -Djboss.node.name=movie-1-ifprs -Djgroups.bind_addr=0.0.0.0 -Dorg.apache.coyote.http11.Http11Protocol.COMPRESSION=on

=========================================================================

09:48:07,045 INFO  [org.jboss.modules] (main) JBoss Modules version 1.5.1.Final

09:48:23,384 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "ROOT.war" (runtime-name : "ROOT.war") 
09:48:23,634 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://0.0.0.0:9990/management 
09:48:23,635 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://0.0.0.0:9990 
09:48:23,635 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.0.0.Final (WildFly Core 2.0.10.Final) started in 18261ms - Started 484 of 768 services (399 services are lazy, passive or on-demand)

If you check the Overview page for your Pod, you will see that a route has been created as well to the Service. The route will let you reach your pods transparently, whatever IP Address they are assigned. Click on the route link and verify that the application is available:

Running a Java EE application on Openshift

Voilà! You have your JEE application running in Openshift ! You might try swapping the database, for example by switching to the MySQL Database and use the appropriate environment variables. The only change required to the source code will be updating the file resources/persistence.xml so that the default MySQLDS Datasource available in the template is used instead:

<jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>

Follow us on Twitter