Running Spring Java MongoDB Apps on OpenShift Express Using Spring Roo OpenShift Express Addon
Last couple of years Spring Roo has been one of my favorite tool to rapidly build Spring applications for demo, POC, and learning new technologies. With Spring Roo Cloud Foundry integration you can not only build applications rapidly but deploy to Cloud Foundry public cloud with a couple of commands. In case you are not aware of Spring Roo and Cloud Foundry you can refer to my Spring Roo series on IBM DeveloperWorks. From last 5-6 months I have been following OpenShift platform as a service and I am really in love with OpenShift Express because of its feature set and simplicity. In case you are not aware of OpenShift Express please refer to the documentation. There are two ways Java applications can be deployed to express --- using ruby command line tool called RHC and using Eclipse plugin. Personally I like rhc command line more than eclipse plugin. The rhc command line tool is great but you should have ruby runtime installed on your machine. Being Spring Roo afficianado I decided to write Spring Roo OpenShift Express add-on to create, deploy, add cartridges from with Roo shell. This can be thought of as a third way to deploy applications to OpenShift Express. The project is hosted on Google Code at http://code.google.com/p/spring-roo-openshift-express-addon/. In this blog I will show you how you can install the add-on, create a template OpenShift Express application, convert that application to a Spring MongoDB application using Spring Roo, and finally deploy application to OpenShift Express. All of this will be done from with in Roo shell.
Prerequisites
- Sign up at https://openshift.redhat.com/app/express
- Download Spring Roo 1.2 Release from Spring Roo website.
- Git. OpenShift uses git to deploy applications to OpenShift Express cloud.
- SSH keys. OpenShift uses ssh keys for authentication. Please refer to documentation for generating your ssh keys.
Lets Get Started
- After you have installed Spring Roo on your machine fire the roo command to load the roo shell.
- Install the add-on by executing the following command.
osgi start --url http://spring-roo-openshift-express-addon.googlecode.com/files/org.xebia.roo.addon.openshift-0.1.RELEASE.jar
This command will take couple of seconds to execute as it has to download the jar.
- Verify that add-on is active by viewing all the osgi process using osgi ps command. You will see output like as shown below that openshift add-on is Active.
[ 166] [Active ] [ 1] spring-roo-openshift-express-addon (0.1.0.RELEASE)
- You can view all the available add-on command by typing help command as shown below.
roo> help --command rhc * rhc app destroy - Destroys the application * rhc app restart - Restarts the deployed application * rhc app start - Starts the deployed application * rhc app status - Gives status of the deployed application * rhc app stop - stops the deployed application * rhc cartridge add - Adds a cartridge to the application * rhc cartridge list - Lists all the avaliable embeddable cartridges * rhc create jbossas7 app - Creates a new JBoss AS7 OpenShift Express Application * rhc create jenkins app - Creates a new Jenkins OpenShift Express Application * rhc deploy - Deploys code to OpenShift Express * rhc pom - Adds OpenShift Profile to pom.xml * rhc user info - Displays information about user's OpenShift Express applications
You can also view commands by typing rhc and then pressing tab. As you can see from the commands you can do a lot of stuff with this add-on. You can create the application (both jboss and jenkins), do application management (start, stop, destroy, status), view user information, add cartridges (MySQL, MongoDB, Jenkins client, RockMongo client, PhpMyAdmin etc ) and adding OpenShift profile in pom.xml.
- Now that add-on has been successfully installed we can create the application. But before you can create the application you have to make sure that you have created the domain. Currently the add-on does not support creating domain but I will be adding support for creating domain in next release. In case you have not created domain please refer to the documentation to create one. The domain name should be unique. I have created domain name called xke which stands for Xebia Knowledge Exchange.
- You can view the information of the user like all the applications created by user using rhc user info command. This is same as rhc-user-info command.
- Lets create a simple application called bookshop which will be simple CRUD application for creating books. The application will be using MongoDB as datastore. The Java applications created in Express are deployed on JBoss AS 7 server. To create the application type the command shown below.
rhc create jbossas7 app --applicationName bookshop --rhLogin <rhlogin> --password <please>
Please type the email and password with which you registered for express. You will see output like as shown below. This command first create the default application, clone the git repository, and deploy the application on express. You can view the application at
Loaded OpenShift configuration from /home/shekhar/.openshift/express.conf Waiting for OpenShift to propagate DNS Trying to contact https://bookshop-xke.rhcloud.com/ (attempt 1 of 500) Git Clone Command : git clone ssh://b85866270d85472886c7f2611b7774ad@bookshop-xke.rhcloud.com/~/git/bookshop.git/ bookshop Initialized empty Git repository in /home/shekhar/dev/playground/bookshop/.git/ Application Path ./bookshop Cloned the git repository Application created and deployed to https://bookshop-xke.rhcloud.com/
- Quit the Roo shell and cd into bookshop directory and you will see that
bookshop is a maven based project. Delete these files and commit using git as shown below.git rm -rf src pom.xml git commit -m "deleting default files"
- Fire the roo command again. Make sure that you are in bookshop directory.
- Add the MongoDB cartridge to your application using rhc cartridge add command as shown below.
roo> rhc cartridge add --applicationName bookshop --rhLogin <email> --password <password> --cartridge MONGODB Loaded OpenShift configuration from /home/shekhar/.openshift/express.conf Embedded cartridge: mongodb-2.0 MongoDB 2.0 database added. Please make note of these credentials: Root User: admin Root Password: j7eCdNsjANyY Database Name: bookshop Connection URL: mongodb://127.1.13.1:27017/ You can manage your new MongoDB by also embedding rockmongo-1.1 - Now that we have added the MongoDB cartridge to our application lets create a simple Spring MongoDB application using Spring Roo. Create the project using Spring Roo project command as shown below.
project --topLevelPackage com.xebia.bookshop --projectName bookshop
- After the project setup we need to add MongoDB support to our application using mongodb setup command as shown below. The MongoDB credentials will be the one that we got back from rhc cartridge command as shown above.
mongo setup --host 127.1.13.1 --username admin --password j7eCdNsjANyY --port 27017 --databaseName bookshop
- As this is not a Spring Roo tutorial so I am not going to walk through all the commands. Please refer to Spring Roo documentation or my IBM DeveloperWorks series for more information. Execute the following commands and a simple CRUD bookshop application will be ready.
entity mongo --class ~.domain.Book field string --fieldName title --notNull field string --fieldName author --notNull field number --type double --notNull --fieldName price repository mongo --interface ~.repository.BookRepository --entity ~.domain.Book service --interface ~.service.BookService --entity ~.domain.Book web mvc setup web mvc all --package ~.web
- There is a small bug in the generated code. The bug is that in applicationContext-mongo.xml dbname is referring to a property named mongo.database which does not exist. The correct name of the property is mongo.name. To fix this we can use database properties command as shown below.
database properties remove --key mongo.name database properties set --key mongo.database --value bookshop
- There is just one last thing left before we can push our code to cloud which is that pom.xml file should have a profile called openshift. When we deploy the code to cloud openshift profile gets executed. To add openshift profile please execute rhc pom command.
~.web roo> rhc pom Updated ROOT/pom.xml [added openshift profile]
- Deploy the application using rhc deploy command. This command first add all the files to git , then commit all the files, and then finally do git push. This command will take some time(less than 30 secs) as it has to execute the Maven build which has to download all the jars and then restart the application with new war file.
- You can view the status of the application to check if the war has been deployed or not using rhc app status command as shown below. If you don't see any error and see ROOT.war deployed. This means deployment was successful.
~.web roo> rhc app status --applicationName bookshop --rhLogin <email> --password <password> Loaded OpenShift configuration from /home/shekhar/.openshift/express.conf Status ... tailing /var/lib/libra/b85866270d85472886c7f2611b7774ad/bookshop//jbossas-7.0/standalone/log/server.log ------ Tail of bookshop application server.log ------ 14:31:58,042 INFO [org.jboss.as.deployment] (MSC service thread 1-3) Started FileSystemDeploymentService for directory /var/lib/libra/b85866270d85472886c7f2611b7774ad/bookshop/jbossas-7.0/standalone/deployments 14:31:58,387 INFO [org.jboss.as] (Controller Boot Thread) JBoss AS 7.0.2.Final "Arc" started in 6743ms - Started 81 of 106 services (22 services are passive or on-demand) 14:31:58,460 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) Starting deployment of "ROOT.war" 14:32:02,288 INFO [org.jboss.as.jpa] (MSC service thread 1-4) added javax.persistence.api dependency to ROOT.war 14:32:06,290 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/]] (MSC service thread 1-2) No Spring WebApplicationInitializer types detected on classpath 14:32:06,357 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/]] (MSC service thread 1-2) Initializing Spring root WebApplicationContext 14:32:09,455 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/]] (MSC service thread 1-2) Initializing Spring FrameworkServlet 'bookshop' 14:32:10,698 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-2) Hibernate Validator 4.2.0.Final 14:32:13,907 INFO [org.jboss.web] (MSC service thread 1-2) registering web context: 14:32:14,093 INFO [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployed "ROOT.war"
- Go to http://bookshop-xke.rhcloud.com/
Conclusion
In this blog I have not covered all the features of this add-on like Jenkins support, MySQL support etc. There are also couple of things which are still missing like creating domain and some sort of session management so that you don't have to provide username and password with every command. I will try and add them in next release. Please share your suggestions and give feedback.




Hi Shekhar,
Thanks for the informative article. I attended your presentation in JUDCOn2012 and got charged up and tried out a sample app built using Roo on openshift
Here are the additional steps I had to do to get my app running.
Update applciationcontext-mongo.xml to add the attributes mongo.username, mongo.password and mongo.dbname to the mon:dbFactory bean defintion
Access the mogo shell of openshift and create the collections.
Thought of letting you know about this.
regards,
Jayaram.
Nice article. A recap of the XKE sesson.