- Create a Ruby 1.9 application
rhc app create -a railsdemo -t ruby-1.9
- After running the command railsdemo folder will get created in your directory. Run the command shown below. This will generate rails code in the railsdemo folder.It will ask you whether you want to override, say yes.
rails new railsdemo
- Next run commands shown below
cd railsdemo bundle install rails generate controller home index rm public/index.html
- Add the following route to config/routes.rb:
root :to => "home#index"
- Generate Post entity with fields as shown below
rails generate scaffold Post name:string title:string content:text
- Add Database support by uncommenting following lines from .openshift/action_hooks/deploy script
pushd ${OPENSHIFT_REPO_DIR} > /dev/null bundle exec rake db:migrate RAILS_ENV="production" popd > /dev/null - Add mysql cartridge
rhc cartridge add -a railsdemo -c mysql-5.1
- Update config/database.yml
production: adapter: mysql2 encoding: utf8 database: <%=ENV['OPENSHIFT_APP_NAME']%> pool: 5 host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%> port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%> username: <%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%> password: <%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%> socket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>
- Add gem in Gemfile
gem 'mysql2'
- Add, Commit, and Push the Code
git add . git commit -a -m "Initial setup" git push
- Finally you can view the application running at http://railsdemo-domainname.rhcloud.com and you can create new posts at http://railsdemo-domainname.rhcloud.com/posts
Author: shekhargulati
Deploy war on tomcat running on OpenShift
In this short blog, I will show you how you can deploy war file on Tomcat running on OpenShift. For Java, OpenShift supports three deployment options – JbossAS-7, JBossEAP-6, and JBossEWS-1.0 or Tomcat. In all of the three options it supports both binary deployment (war file) and source code deployment(maven project). I have talked about how you can do source code deployment on tomcat in my previous blog.
Step 1: Sign up for an OpenShift Account
If you don’t already have an OpenShift account, head on over to the website and signup with promo code ews. It is completely free and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.
Step 2: Install the client tools on your machine
Note: If you would rather watch a screencast of this step, check out the following videos where I demo how to install the client tools.
The OpenShift client tools are written in a very popular programming language called Ruby. With OSX 10.6 or later and most Linux distributions, ruby is installed by default so installing the client tools is a snap. Simply issue the following command on your terminal application:
sudo gem install rhc
Step 3 : Setting up OpenShift
The rhc client tool makes it very easy to setup your openshift instance with ssh keys, git and your applications namespace. The namespace is a unique name per user which becomes part of your application url. For example, if your namespace is cix and application name is bookshop then url of the application will be https://bookshop-cix.rhcloud.com/. The command is shown below.
rhc setup -l openshift_login
Step 4 : Creating Tomcat Application
After installing the client tools and setting up OpenShift account, next step is to create the bookshop application. This is a very simple Spring JPA application which has only one entity called Book. So, a user can do CRUD operations on book entity. To create a book entity, execute the command shown below.
rhc app create -a bookshop -t jbossews-1.0
This will create an application container for us, called a gear, and setup all of the required SELinux policies and cgroup configuration. OpenShift will also setup a private git repository for you and propagate your DNS out world wide.
Step 5 : Download the WAR file
Because we are doing binary deployment in this blog, we have to remove the src folder and pom.xml file from git repository and add the war file.
git rm -rf src/ pom.xml git commit -am &quot;removing default files&quot;
Next download the war file from http://bookshop-demo.googlecode.com/files/ROOT.war and copy the war file in webapps directory. The name ROOT for war makes sure that the application is accessible at the root context i.e. http://bookshop-cix.rhcloud.com/.
git add . git commit -am &quot;committing bookshop war file&quot;
Step 6 : Pushing the code to OpenShift
Finally push the code to OpenShift using git.
git push
The git push command upload the binary to the application gear and runs the action hooks specified in .openshift/action_hooks folder.
Configuring JNDI DataSource with OpenShift Tomcat Cartridge
This short blog post will tell you how to configure JNDI datasource with OpenShift Tomcat cartridge. After configuring, we will also make it work with a sample Spring MVC application. Let’s get started.
Step 1 : Create Tomcat Application
After you have signed up for OpenShift and setup your account. Execute the rhc app create command as shown below to create tomcat application as shown below.
rhc app create -a tomcatjndidemo -t jbossews-1.0
Step 2 : Adding PostgreSQL Cartridge
Next we will add postgresql cartridge to our application by executing the command as shown below.
rhc cartridge add -a tomcatjndidemo -c postgresql-8.4
Step 3 : Defining Datasource in Tomcat Configuration Files
OpenShift gives you the flexibility to edit the tomcat configuration files. These files are location in .openshift/config folders inside tomcatjndidemo folder. If you look into this directory you will find that there are 5 files. For adding datasource we have to make changes in two files — context.xml and server.xml
In server.xml you have to define a resource under GlobalNamingResource as shown below.
<Resource name="jdbc/postgresqldb" auth="Container" type="javax.sql.DataSource"
username="${env.OPENSHIFT_POSTGRESQL_DB_USERNAME}" password="${env.OPENSHIFT_POSTGRESQL_DB_PASSWORD}"
url="jdbc:postgresql://${env.OPENSHIFT_POSTGRESQL_DB_HOST}:${env.OPENSHIFT_POSTGRESQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}"
driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
maxActive="120" maxIdle="5" validationQuery="select 1"
poolPreparedStatements="true">
</Resource>
Next change that you have to make is in context.xml. You have to define resource link to the datasource as shown below.
<ResourceLink name="jdbc/postgresqldb" global="jdbc/postgresqldb" type="javax.sql.DataSource"/>
Step 4 : Updating Tomcat Classpath with PostgreSQL JDBC Driver
Tomcat will require PostgreSQL JDBC driver jar to make connection with PostrgreSQL. You can’t copy the jars to tomcat lib directory as it is not writable. To do that, first ssh into the application instance and then download the jar in $OPENSHIFT_DATA_DIR as shown below.
cd $OPENSHIFT_DATA_DIR wget http://repo1.maven.org/maven2/postgresql/postgresql/8.4-702.jdbc4/postgresql-8.4-702.jdbc4.jar
Next update the catalina.properties in your application .openshift/config folder to scan the OPENSHIFT_DATA_DIR as shown below. Please replace the openshift path with value of you OPENSHIFT_DATA_DIR environment variable.
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,/var/lib/openshift/2e110c16da18478aa607f742d34b70fb/app-root/data/*.jar
Step 5 : Pushing Changes to OpenShift
Now you can push the changes to OpenShift as shown below.
git commit -am "made changes for tomcat jndi datasource" git push
Step 6 : Testing the DataSource
The last step in this blog is to test the datasource configuration we added in step 3 and step 4. To do that pull the code from my github repository. The code is a simple Spring MVC application which do CRUD operations on Member entity. Please note that the code also contains the tomcat configuration changes. So, please update the catalina.properties with your $OPENSHIFT_DATA_DIR value.
git remote add jndi git://github.com/shekhargulati/tomcatjndidemo.git git pull -s recursive -X theirs jndi master
Next do git push which will build the war file and deploy the application to tomcat. The application should be accessible at http://tomcatjndidemo-cix.rhcloud.com/
Time To Move On And Start a New Journey
It has been a wonderful and amazing experience to work at Xebia India office. I have learned and matured a lot in last 2.5 years I spent at Xebia. I was referred to Xebia by Rajneesh Namta who has worked with me at GlobalLogic. I have enjoyed working on lot of technologies at Xebia including NoSQL datatores, various PaaS solutions, BigData technologies like Hadoop, and various Spring portfolio projects. This blog is a retrospective of why I joined Xebia, What I did at Xebia and What lies ahead for me.
Why I Joined Xebia?
I had lot of other Job offers at the time when I was planning to leave GlobalLogic. I joined Xebia because of following reasons :
- Anurag Shrivastava : My first interaction with Anurag was during my interview. He took final round in the interview process and I was very impressed by him. He asked questions which nobody has ever asked me in any other interview like what are your thoughts on Open source, what you want to be 5 years down the line, do you blog etc. I liked the fact that he was not only focussing on project work but talking about things which are also important for building a successful career as a Software developer.
- I wanted to work for a company which will give me a platform to discover myself. From GlobalLogic time I had interest in writing and speaking but because of lack of support and mentorship was not able to do much.
- I wanted to work for small organisation which values quality over quantity.
- Go beyond the conventional project work and do much beyond that and make my name in community.
- Xebia Values : I joined Xebia because I believed in Xebia values.
What I did at Xebia?
The three main things that I did at Xebia are :
- For most part of my 2.5 year stint at Xebia I worked on building a scalable de-duplication engine. I worked with some wonderful and knowledge people(in no particular order) — Guneet, Rahul, Sameer, Karan, Nancy, Rajneesh, Richa and Paritosh. I would like to thank all of them for their help and support.
- Writing : At Xebia I discovered my interest in writing and I have written at lot of technical or developers oriented portals like IBM DeveloperWorks, Developer.com, JavaLobby and Xebee and my blog. Recently a publisher has approached me to write a book. Lets see how it goes.
- Speaking : I discovered my passion for speaking while giving XKE’s at Xebia India office. XKE is Xebia Knowledge Exchange where in on every alternate Wednesday we have three hours of technical or non-technical sessions followed by dinner. I have spoken at most of developer oriented conference in India and have also spoken at RedHat Summit 2012 in Boston.
What lies ahead for me?
I am joining RedHat as OpenShift Evangelist on 21st September. I will be travelling around the world speaking at various conferences, writing about OpenShift, building cool applications and building active and vibrant community around OpenShift. I might also think considering about writing a book if time permits. Let see how things unfold in future.
My Advice to fellow Xebians
Although I am nobody to advice anybody but will still give my advice. Xebia is one of very few IT organisation in India which gives platform to do lot of other things apart from project work; please do make use of them and build an exciting and wonderful career.
Best of luck to everyone and stay connected!
IBM DeveloperWorks Spring Roo Part 5 : Writing Advanced and Wrapper Addons
Today fifth part of my Spring Roo series got published at IBM DeveloperWorks. This article-cum-tutorial talks about how you can write advanced and wrapper Spring Roo add-ons. Checkout the article at http://www.ibm.com/developerworks/opensource/library/os-springroo5/index.html
How to get Solr Up and Running On OpenShift
Full text search is a vital component in most enterprise or non-enterprise applications and Solr is one of the most popular choices. So, today I decided to spend sometime on getting Solr up and running on OpenShift. In this blog I am sharing all the steps required to get Solr running on OpenShift.
- Download the latest version of Solr. The current latest version is 3.5. You can get it from here http://www.apache.org/dyn/closer.cgi/lucene/solr/3.5.0
- Install the OpenShift rhc ruby gem. You can follow steps https://www.redhat.com/openshift/community/kb/kb-e1000/installing-openshift-express-client-tools-on-non-rpm-based-systems
- Create a new jbossas-7 application using rhc gem. Type the command shown below.
rhc-create-app -a solr -t jbossas-7 -d -l email
- Do a git remove the src and pom.xml files from the created solr maven project and commit the changes.
git rm -rf src/ pom.xml git commit -a -m "removing default files"
- Copy the solr.war file which exists in apache-solr-3.5.0/example/webapps directory to deployment directory under solr maven project.
- Next solr needs a solr home directory. This directory contains a conf directory and lib directory. The default conf directory comes with solr installation and you can find at apache-solr-3.5.0/example/solr/conf. The lib directory should contains apache-solr-velocity-3.5.0.jar, commons-beanutils-1.7.0.jar,commons-collections-3.2.1.jar,velocity-1.6.4.jar,velocity-tools-2.0.jar. The solr home directory will also have index.
- On oepenshift you can put data in $OPENSHIFT_DATA_DIR. So create a folder solr.home under $OPENSHIFT_DATA_DIR directory.
- Push the conf and lib directory to $OPENSHIFT_DATA_DIR/solr.home directory using rsync.I zipped both conf and lib directory in one solr.zip file and extracted on remote machine.
rsync -avz -e ssh solr.zip xxx@solr-india.rhcloud.com:$OPENSHIFT_DATA_DIR/solr.home
- Now you need to add solr.war, commit it and push it to openshift.
git add . git commit -a -m "committing solr war" git push
- The above line will stop the jboss and deploy the war. But you will get exception because you didn’t specified solr home. To do that ssh into the openshift application instance and execute the command shown below.
ctl_all stop export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=$OPENSHIFT_DATA_DIR/solr.home" ctl_all start
Finally you will see up and running solr http://solr-india.rhcloud.com/solr/admin/
Quick Tip on Mockito — Mocking Iterator
Today I was writing unit test for a piece of code which required me to mock a iterator. My requirement was that I wanted to return true first time when hasNext() method and return false in the second iteration. It took me sometime to figure out how to write such a test case. My class for which I was writing test looks like as shown below.
import java.util.Iterator;
public class ResultFetcher {
private ResultStore store;
private ResultProcessor processor;
public void fetchResults() {
Iterator<String> iterator = store.resultIterator();
while (iterator.hasNext()) {
String result = iterator.next();
processor.process(result);
}
}
}
The unit test code is shown below. The important line in the test is Mockito.when(iterator.hasNext()).thenReturn(true,false); which will return true first time and false second time.
import java.util.Iterator;
import org.junit.Test;
import org.mockito.Mockito;
public class ResultFetcherTest {
@Test
public void testFetchResults() {
ResultFetcher fetcher = new ResultFetcher();
ResultStore store = Mockito.mock(ResultStore.class);
ResultProcessor processor = Mockito.mock(ResultProcessor.class);
fetcher.store = store;
fetcher.processor = processor;
Iteratoriterator = Mockito.mock(Iterator.class);
Mockito.when(store.resultIterator()).thenReturn(iterator);
Mockito.when(iterator.hasNext()).thenReturn(true,false);
Mockito.when(iterator.next()).thenReturn("Hello");
fetcher.fetchResults();
Mockito.verify(processor).process("Hello");
}
}
Say Hello to Jelastic
These days Platform as a Service (PaaS) is one of my interest areas and I like to play with different PaaS providers to see how easy or difficult it is to develop and deploy application on them. The best thing about most of the current new generation PaaS systems is that they don’t require you to change your code or learn new programming paradigm. Google App Engine is thing of past and is losing ground in PaaS race. For last six months I have spend some of my spare time on OpenShift and Cloud Foundry and one thing I can say is that I love both of the platforms. Today I decided to spend some time on Jelastic — seeing how easy or difficult is to deploy a simple Spring MongoDB application on it. According to Jelastic website
Jelastic is the next generation of Java hosting platforms which can run and scale ANY Java application with no code changes required
Jelastic provides a web ui using which you can create the deployment environment and upload your war file to it.To check the usability of the UI I decided that I will not refer to Jelastic documentation and will try to deploy the application based on my understanding. So in this blog I am sharing the steps I performed to deploy a simple Spring MongoDB application to Jelastic.
- To start I created a very simple simple moviestore application using Spring Roo. For those of you who are not aware of Spring Roo can refer to my article series at IBM Developerworks on Spring Roo.Once you have installed Spring Roo, fire the Roo shell and execute following commands. This will create a Spring MVC web application with MongoDB as backend.
project --topLevelPackage com.shekhar.moviestore --projectName moviestore mongo setup --databaseName moviestore entity mongo --class ~.domain.Movie field string --fieldName title --notNull field string --fieldName description --notNull repository mongo --interface ~.repository.MovieRepository service --interface ~.service.MovieService web mvc setup web mvc all --package ~.web q
- You can test the application locally by first starting the MongoDB server and then starting the application using mvn tomcat:run.
- But the point is to test the application on Jelastic. So go to http://jelastic.com/ and sign up for free. You don’t need to pay anything. I choose North America hosting provider.
- Once you have registered at Jelastic login with your credentials at https://app.jelastic.servint.net/
- After you have logged in to Jelastic portal you will see a Create environment link on the left. In Jelastic you have to first create environment under which your application will run. Click on the environment link and choose MongoDB, Tomcat, Java 6 as the environment topology. This is shown in image below. I really liked the UI. It is sexy.

- When you press create it will take couple of minutes to create the environment. So please be patient.
- You will receive an email from Jelastic with the MongoDB connection details. It will give you a url to access MongoDB from web UI and an admin username and password.In my case I received url http://mongodb-moviestore.jelastic.servint.net/. I am not going to share username and password.
- The MongoDB UI is a RockMongo MongoDB web client. Login into it using admin username and password and Rock 🙂
- Next we need to create a MongoDB database and user with which our application can connect. To create database first click on databases and then “Create new Database”. Enter the name of database as moviestore and press create button. Next click on newly created moviestore database and click more then authentication and then click on add user to create a new user. Create a user with username as moviestore and password as password and press Add user.
- Now that we have created a user we should update the database.properties and applicationContext-mongo.xml files which were created by Spring Roo. By default they were pointing to localhost. Update the files as shown below.
database.properties#Updated at Tue Feb 28 12:26:32 IST 2012 #Tue Feb 28 12:26:32 IST 2012 mongo.host=mongodb-moviestore.jelastic.servint.net mongo.name=moviestore mongo.password=password mongo.port=27017 mongo.username=moviestore
applicationContext-mongo.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:cloud="http://schema.cloudfoundry.org/spring" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://schema.cloudfoundry.org/spring http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.8.xsd"> <mongo:db-factory dbname="${mongo.name}" host="${mongo.host}" id="mongoDbFactory" password="${mongo.password}" port="${mongo.port}" username="${mongo.username}"/> <mongo:repositories base-package="com.shekhar.moviestore"/> <!-- To translate any MongoExceptions thrown in @Repository annotated classes --> <context:annotation-config/> <bean class="org.springframework.data.mongodb.core.MongoTemplate" id="mongoTemplate"> <constructor-arg ref="mongoDbFactory"/> </bean> </beans> - Build the maven project by executing mvn clean install command.
- Then upload the war by clicking on upload link in the Jelastic web UI.This will take some time depending on your internet connection.
- After the war is uploaded you will see the war in deployment manager tab. Click deploy to moviestore environment to deploy to tomcat and select context as ROOT.
- Finally you will be able to view the application running at http://moviestore.jelastic.servint.net/
This was my first write up on Jelastic and I will continue experimenting with it and evaluating its capabilities. I will also spend time reading its documentation and see how it compare with other PaaS providers. Overall I was impressed with Jelastic and to me it looks like a good deployment option for Java applications.
Spring Roo PGP Exception
Today when I fired Spring Roo shell I started getting exception as shown below and I was not able to execute any command. I uninstalled Spring Roo (thinking that it might have got corrupted) but it didn’t helped after lot of firefighting I figured out the solution to overcome this problem. The problem was that yesterday I trusted some pgp keys and somehow Roo was not able to find them and started throwing exception. The solution is to remove a file name .spring_roo_pgp.bpg and restart Spring Roo. Spring Roo will create a new clean file. Continue reading “Spring Roo PGP Exception”
Logging JAXWS SOAP Request and Response using a Java Property
Today I was faced with the situation that I needed to log the SOAP requests and responses going in and out from a Java client. I just had the client jar no source code so I can’t any any log or change any other configuration. I needed to log the request and response because I was getting some weird exceptions which I was not able to understand. I got the hold of the SOAP request by passing a Java property
java -Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=true -jar client.jar