This blog is about how you can get started with OpenShift using JBoss Tools eclipse plugin to build a Spring MVC 3 applications. The sample application will be deployed on JBossEAP6(JBoss Enterprise Application Platform 6) application server running on OpenShift. OpenShift is the first Platform-as-a-Service to support JBoss application server, bringing Java Enterprise Edition 6 to the cloud free of charge.
Step 1 : Download the required tools
This blog require user to have following :
- Eclipse Juno IDE for JavaEE Developers : You can download it from here.
Step 2 : Installing JBoss Developer Tools Eclipse Plugin
Once you have downloaded the Eclipse Juno IDE ,extract the eclipse archive to a convenient folder and run the eclipse application. After eclipse is started, Go to Help > Install New Software > click Add button. Give the repository any logical name and enter the location as http://download.jboss.org/jbosstools/updates/development/juno/ and press OK as shown below.
Then it will show you a list of plugins to install and select "Abridged JBoss Tools 4.0" as shown below. Then press Next, accept the license and press finish button. This will take few minutes to install the JBoss Tools in Eclipse.
Step 3 : Sign up for an OpenShift Account
If you don’t already have an OpenShift account, head on over to the website and sign up. 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 4 : Creating SSH Keys
The next step is to create a RSA key to deploy the sample application. To create keys follow the steps mentioned below.
- Access the menu: Window> Preferences
- With the preferences window still open, go to: General> Network Connection> SSH
- Click on Tab Key Management and then the button Generate RSA Key ...
- Copy the code key
- Now click the Save Private Key , then Ok as image below
Step 5: Adding a new SSH key to OpenShift
After creating the ssh keys, the next step is to upload the public keys generated in previous step to OpenShift .Go to https://openshift.redhat.com/app/account/keys/new and add a new ssh key as shown below. You can find the public key in .ssh folder under your user home directory. The file will have a name id_rsa.pub. You can add multiple keys like one for your office and one for your home.
Step 6 : Create OpenShift Account Namespace
Every account needs to have one namespace which should be unique to the account only. One account can have only one namespace. Namespace form the part of the url that OpenShift assigns to an application. For example, if you application name is awesome and namespace is openshiftdemo, then the url of application will be http://awesome-openshiftdemo.rhcloud.com. To create a namespace go to https://openshift.redhat.com/app/account and under namespace session enter the namespace as shown below.
Step 7 : Creating OpenShift Application
After creating the namespace, we are ready to create applications using JBoss Tools OpenShift support. Go to your eclipse and click File > New > Other> OpenShift Application as shown below and click next as shown below.
After pressing 'Next', you will be asked to provide your OpenShift credentials you created in step 3. If you have not signed up on OpenShift, you can click the sign up here link on the wizard to create your OpenShift account. Check the 'Save password' checkbox so that you don't have to enter password with every command and click 'Next'.
Next you will be asked to enter the details of the application like name of the application, type of the application, gear profile(whether you want small instance or medium instance.For FreeShift users,you can only create small instances), scaled application or non scaled application, and whether you want to embed any or multiple cartridges like mysql, postgresql, mongodb etc. We will create an application named springmvc3 of type jbosseap-6.0 and embed postgresql-8.4 cartridge as shown below.
Next you will be asked to configure your project and server adapter settings. Choose the default and click next as shown below and click next.
The next screen will ask you to specify the location where you want to clone the git repository and name of the git remote. This is shown below.
Finally press the finish button and you are done. 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 clone the repository to your local system. Next, OpenShift will propagate the DNS to outside world. Finally, the project will be imported in your eclipse workspace. A new project will be created as shown in the image below.
Step 8 : Look at created application
The template application created by OpenShift is accessible at http://springmvc3-shekhargulati.rhcloud.com/. Replace 'shekhargulati' with your own domain name. The template project created by OpenShift is a JavaEE 6 application. Delete the following files in the created project:
- health.jsp
- images Directory
- index.html
- snoop.jsp
- faces-config.xml (in WEB-INF)
One thing that you need to be aware of in the default generated project is openshift profile in the pom.xml.This maven profile is invoked when you do a git push.This makes sure that the war file is created under deployments folder and has ROOT as its name. The profile is shown below.
<profiles>
<profile>
<id>openshift</id>
<build>
<finalName>springmvc3</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<outputDirectory>deployments</outputDirectory>
<warName>ROOT</warName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Step 9 : Update the pom.xml with Spring Dependencies
The pom.xml created by the OpenShift has JavaEE 6 dependencies. As we are going to use Spring MVC 3 in this blog, we have to delete those dependencies and add Spring related dependencies. The pom.xml is shown below. Right-click over the project, go to menu and click Update Maven Project ... (This step is necessary so that all dependencies are downloaded from maven).
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springmvc3</groupId>
<artifactId>springmvc3</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>springmvc3</name>
<properties>
<spring.version>3.1.3.RELEASE</spring.version>
</properties>
<repositories>
<repository>
<id>springsource-repo</id>
<name>SpringSource Repository</name>
<url>http://repo.springsource.org/release</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>openshift</id>
<build>
<finalName>springmvc3</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<outputDirectory>deployments</outputDirectory>
<warName>ROOT</warName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Step 10: Create Spring MVC controller
Now we will create a very simple Spring MVC controller. The controller will be invoked when someone will hit the url http://springmvc3-shekhargulati.rhcloud.com/hello and will return a jsp with a hello message.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SpringMVC3Controller {
@RequestMapping("/hello")
public String sayHelloToOpenShift(){
return "hello";
}
}
Step 11: Create Spring Application Context File
After creating the controller, we will create Spring web application context file with name web-context.xml(you can choose different name) under WEB-INF folder which will have component scan declaration as well view resolver bean.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.openshift.springmvc3.controllers" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Step 12: Adding DispatcherServlet in web.xml
Spring requires you to configure a servlet in web.xml. This servlet acts as a entry point for all the requests. Configure DispactherServlet as shown below in web.xml and remove the template code.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
metadata-complete="false">
<servlet>
<servlet-name>springmvc3</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc3</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Step 13: Creating Views
Next create two views one index.html and another hello.jsp which will be called when we will make request to /hello url. Create hello.jsp file in src/main/webapp/WEB-INF/views folder as shown below.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello From OpenShift</title>
</head>
<body>
<h3>Hello From OpenShift</h3>
</body>
</html>
Also create index.jsp in src/main/webapp folder.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC3 on OpenShift</title>
</head>
<body>
<h3>SpringMVC3 on OpenShift</h3>
</body>
</html>
Step 14 : Committing the code and Pushing to Cloud
The last step of this blog is to commit the code and pushing it to the git repository . To commit your code, ##Right click on project> Team>Commit.## Next it will ask you to enter commit message, so please enter any logical message.
Now we do commit our pages and configuration files. Now let's deploy our project by Right click on project> Team> Remote> Push as shown below.
After the code is pushed to git repository and maven build runs file , application will become accessible at http://springmvc3-shekhargulati.rhcloud.com/. Replace shekhargulati with your own domain name.
Conclusion
In this blog, we created a very simple Spring MVC application and deployed on JBoss EAP6 running on OpenShift. After following this blog, your development environment will be ready and you can deploy your own applications on OpenShift. In the next part of this series, we will add persistence support to the application.
What's Next?
Categories