Hi,

In previous posts we have seen how to deploy our custom application on OpenShift, now this time we are going to have our own database on cloud ūüôā
We will deploy an application on JBoss AS 7 hosted on Openshift and that will lookup on a mysql database on cloud.

We are going to have a small application, which will have below structure :

mysqldsTest.war

        - WEB-INF
               - web.xml
        - index.jsp

index.jsp will have below code :

<%@page import="javax.sql.*,java.sql.*,java.util.*,javax.naming.*" %>
    <%
        String jndiName = "java:jboss/MySqlDSJndi";
        Connection con = null;
        try{
             Context ic = new InitialContext();
             System.out.println("ntAbout to look up jndi name " + jndiName);
             Object obj = ic.lookup(jndiName);
             System.out.println("lookup returned " + obj);
             javax.sql.DataSource ds = (javax.sql.DataSource) obj;
             System.out.println("ntGot the DataSource: " + ds);
             con=ds.getConnection();
             System.out.println("nt[ds.getConnection()].getClass().getName(): " + con.getClass().getName());
             System.out.println("nnt GOT Connection at: "+new java.util.Date());
           }
        finally
           {
             if(con!=null)
              {
	         System.out.println("ntfinally{}  if(con!=null) ");
	         try{    con.close();                       } catch(Exception e){e.printStackTrace(); }
              }
            }
        out.println("<h1>Successfully Got the Connection ...");
      %>

and web.xml will contain :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>provaJNDI</display-name>
<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
</web-app>

Follow below steps for the entire setup :

Step-1). Create a directory in which you are going to keep all the files related to project. For example I created below directory on my machine :

/home/nikhil/OpenShift/

Step-2).. We need to create an application entity here, run below commad from the directory you have created for your project :

cd /home/nikhil/OpenShift/
rhc app create -a mysqldsTest -t jbossas-7

enter the password that you use to login for OpenShift Account.

It generates a directory with your application name, in this case “mysqldsTest”

Step-3).. Navigate to the app directory and as we are using an exploded war deployment we will delete “pom.xml”

rm -f  pom.xml

Step-4).. Now copy your war inside deployments folder.

cp -r mysqldsTest.war /home/nikhil/OpenShift/mysqldsTest/deployments

Step-5). Create a blank file as “.war.dodeploy.”

ex :

cd /home/nikhil/OpenShift/mysqldsTest/deployments
touch mysqldsTest.war.dodeploy

This file is needed to deploy the war as it is in exploded format.

Step-6).. Copy “mysql-connector-java-5.1.13-bin.jar” to the same deployment dir where you put the war. This is driver for mysql database. You can find/download it from findjar.com

Step-7).. Now sync your local repository with OpenShift server using below command :

rsync -avz <deployments directory> <ssh-key of your application>@<application url>:~<application name>/repo/deployments

for example :

rsync -avz /home/nikhil/OpenShift/mysqldsTest/deployments/ pqrs7b6d3cf24ce12345a1ec3c1b7d99@mysqldsTest-mysqldsTestdomain.rhcloud.com:~/mysqldsTest/repo/deployments

where “pqrs7b6d3cf24ce12345a1ec3c1b7d99@mysqldsTest-mysqldsTestdomain.rhcloud.com” is the ssh key of your application which you can get by signing in to OpenShift MyApplications page.”/mysqldsTest/repo/deployments” is the remote directory on server to which you always need to sync your app, whenever you make any change. Basically it is a symlink for OpenShift JBoss server’s “deployents” directory

Step-8).We now need to allocate a cloud database for us, for this we need to add a mysql cartridge to our application :

[root@nikhil ~]# rhc app cartridge add -a mysqldsTest -c mysql-5.1Password:
RESULT:
MySQL 5.1 database added.  Please make note of these credentials:
Root User: admin   Root Password: HGDSWUD&amp;^$^%@Q   Database Name: mysqldsTest
Connection URL: mysql://10.10.10.10:3306/
You can manage your new MySQL database by also embedding phpmyadmin-3.4

You need to store above information for your reference. Note that the database instance name and our application name is same. Once this cartridge is added OpenShift creates a datasource “mysqlds” and binds the jndi name “java:jboss/MySqlDSJndi” given inside application on runtime.

Hit the application as :

http://<application name>-<domain name/name-space>.rhcloud.com/mysqldsTest/

ex : http://mysqldstest-nikhilmone.rhcloud.com/mysqldsTest/

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.