Step by step guide about how to deploy your Tomcat 7 application on CloudBees

This is a step by step tutorial which shows you how to deploy your Tomcat 7 application and how to bind it with your MySQL database on CloudBees.

Pre-requisites:

  • You have an account on CloudBees. If not, sign up here. Please, be sure that you have a password on CloudBees platform. If you are signed up through GitHub or Google, we don’t force you to have a password, which is required to use the CloudBees SDK.

  • You have, at least, a free subscription in RUN@Cloud and MySQL. You can add new services on CloudBees once you are signed up here.

  • Install the CloudBees SDK on your computer.

Step 1: Create a blank application


$bees app:create -t tomcat7 -a appName

NOTE: This is an example for a Tomcat 7 application, if you would like to use another stack like Tomcat 6, JBoss or Jetty, please, take a look to our ClickStack documentation.

Step 2: Create a database


$bees db:create dbName

Step 3: Bind application to database

The way in which we bind your application to your MySQL database is through Java Naming and Directory Interface (JNDI). We will use the CloudBees SDK to inject a JNDI DataSource. 

We use JNDI because it permits you to have multiple environments without modifying your application. You can also use other Java standards to bind your application with your database.

Please, note that if you don’t want to use JNDI for the database binding, you will not need to do this step.

Launch this command with the CloudBees SDK:

 
$bees app:bind -a appName -db dbName -as alias
  • “-a accountName/appName”: the name of your account application

  • “-db accountName/dbName”: the name of your CloudBees MySQL Database

  • “-as alias”: the name of the binding which is used to identify the binding and to compose the name of the environment variables used to describe this binding (always prefer ‘_’ to ‘-’ for bindings because ‘-’ is not supported in linux environment variable names).

This binding will create:

  • A JNDI DataSource with name “java:comp/env/jdbc/alias” (also available at “jdbc/alias”)

  • The following System Properties

    DATABASE_URL_MYDB: url of the database starting with “mysql:” (e.g. “mysql://ec2-1.2.3.4.compute-1.amazonaws.com:3306/tomcat7-maven-clickstart-db”). Please note that this URL is not prefixed by “jdbc:”.
    DATABASE_USERNAME_MYDB: login of the database
    DATABASE_PASSWORD_MYDB: password of the database

Step 4: Use the DataSource in your application

You can now use your “java:comp/env/jdbc/alias” JNDI DataSource in your application. Please note that “jdbc/alias” is also available.

Java code sample:


Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(“java:comp/env/jdbc/alias”);
Connection conn = ds.getConnection();
ResultSet rst = stmt.executeQuery(“select 1”);
while (rst.next()) {
   out.print(“resultset result: ” + rst.getString(1));
}
rst.close();
stmt.close();
conn.close();

Step 5: Deploy your application

Once you have created your blank app, and you have created the binding, you can now deploy your application.


$bees app:deploy -a appName -t tomcat7 PATH_TO_WAR_FILE.war

NOTE: This is an example for a Tomcat 7 application, if you would like to use another stack like Tomcat 6, JBoss or Jetty, please, take a look to our ClickStack documentation.

Any Example?

We provide a lot of examples with the database binding already done in our ClickStarts. This tutorial is based on our Tomcat 7 maven ClickStart, you can use it as a template for your own project.

 

Blog Categories: 

Add new comment

Having trouble subscribing in Chrome?

Categories

Archive

2015
2014
2013

September

2012
2011