push monolithic application into pcf – 1

In previous posts, we have seen how to create Microservices application with Spring Boot technology and push it into the pivotal cloud foundry. Since Spring Boot works on the concept of auto configuration, we easily pushed Spring Boot application into PCF without worrying about application configuration or running environment like Tomcat server and all.

When it come to normal Spring MVC application, Tomcat or any other web/application server is compulsory to use to run the application. Now the question is, how can we push monolithic Spring MVC application in Pivotal?

To get clarification about above question, we need to follow below steps.

  1. Create Spring MVC application in local environment.
  2. Test to get confirmation either working as expected or not in locally configured server.
  3. When it’s working in locally, push the same jar/war file into pivotal cloud foundry.
  4. Test by using application route provided by pivotal

Step1: Creating Spring MVC application

I am trying to create a sample application of using the below techstack to push that application into Pivotal Cloud Foundry. This application does not contain any database interaction.

Java – 1.8

Spring – 5.x

Maven – 3.5.x

STS – 4

Tomcat – 8.5

If you are not aware of how to create Spring MVC project in Spring Too Suite (STS) as Maven project. Follow the steps explain in this page.

The project might get generated in different structure. The below is my project structure look like,

pom.xml:

<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>com.monolithic.springmvc</groupId>
    <artifactId>monolithic-springmvc</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>monolithic-springmvc Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>5.1.2.RELEASE</spring.version>
    </properties>
    <dependencies>
 <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>3.8.1</version>
     <scope>test</scope>
 </dependency>
 <dependency>
            <groupId>org.springframework</groupId>
     <artifactId>spring-core</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>
 <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>jstl</artifactId>
     <version>1.2</version>
 </dependency>
    </dependencies>
    <build>
 <finalName>monolithic-springmvc</finalName>
 <plugins>
     <plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        <failOnMissingWebXml>false</failOnMissingWebXml>
  </configuration>
     </plugin>
 </plugins>
    </build>
</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <display-name>MonolithicSpringMVC</display-name>
 <description>Monolithic Spring MVC application created for
  pushing into pivotal cloud foundry</description>

 <welcome-file-list>
  <welcome-file>/index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
     <servlet-name>default</servlet-name>
     <servlet-class> 
                 org.springframework.web.servlet.DispatcherServlet 
            </servlet-class>
     <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring-servlet.xml</param-value>
            </init-param>
     <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
     <servlet-name>default</servlet-name>
     <url-pattern>/</url-pattern>
 </servlet-mapping>
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/spring-servlet.xml</param-value>
 </context-param>
 <listener>
     <listener-class> 
                org.springframework.web.context.ContextLoaderListener 
            </listener-class>
 </listener>
</web-app>

spring-servlet.xml: This file name you can change as you like. But, you should configure the same name in above web.xml file wherever configured. 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <context:component-scan
  base-package="com.monolithic.springmvc.controller" />

    <bean id="viewResolver"
     class="org.springframework.web.servlet.view.UrlBasedViewResolver">
 <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView"/>
 <property name="prefix" value="/WEB-INF/views/"/>
 <property name="suffix" value=".jsp"/>
    </bean>
</beans>
 
index.jsp:

<%@ 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>Monolithic Spring MVC - Index</title>
</head>
<body>
    <center>
 <h2>Hey Monolithic</h2>
 <h3>
     <a href="welcome?name=JavaIsOcean">Click Here</a>
 </h3>
    </center>
</body>
</html>

welcome.jsp:

<%@ 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>Monolithic Spring MVC - Welcome</title>
</head>
<body>
    <center>
 <h2>Hello ${name}</h2>
 <h2>
     ${message} 
 </h2>
    </center>
</body>
</html>

MonolithicController.java:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MonolithicController {
    String message = "Welcome to Monolithic Spring MVC Application!!!";
    @RequestMapping("/welcome")
    public ModelAndView showMessage(@RequestParam(value = "name", required = false, 
        defaultValue = "JavaIsOcean") String name) {
       ModelAndView mv = new ModelAndView("welcome");
       mv.addObject("name", name);
       mv.addObject("message", message);
       return mv;
    }
}

Let’s build the application source code by using the command of mvn clean install and get war file is generated or not in target directory. If the war file is not generated, your code is not correct. If you don’t wish to write all these code, you can also download from https://github.com/Nallamachu/Pivotal. So, now creating the sample Spring MVC project completed.

Step 2: Test application in local server:

To complete this step, we need a web/application server which is running locally. If you have already have configured any server, you can deploy the generated war file into it and test. Otherwise we need to download and configure.

To make it simple, I have downloaded the tomcat server with the version of 8.5 from official Tomcat website as a zip file and extracted locally. There might be a chance of facing startup issues. To avoid that, do the highlighted settings changes in your local as well.

Once you run the project successfully, you will get the output as index page As on when you hit the below URL. URL will be the combination of  host + port+ context name. Output look like below,

You will get the output as below, as on when you click on the Click Here hyper link.

So, our step 2 also completed.

Step 3: Push war file into Pivotal Cloud Foundry:

You should have valid credentials to login into Pivotal account prior doing any operations with Cloud foundry environment. If you don’t have follow these instructions to register and access in Pivotal and get 2 GB of free space.

If you already have valid account details, login into pivotal by using Cloud Foundry CLI with the command of cf login. If you didn’t have installed Cloud Foundry, follow these instructions.

Navigate into the target directory or where your war file located, and issue the below command to push into cloud foundry.

cf push monolithic-springmvc -p monolithic-springmvc.war

cf push  – is the command to push application into cloud foundry
monolithic-springmvc –  is the application name I am passing
-p  – is the parameter to specify the pushing file path where it located

Once we issue the above command, we will see the pushing progress like below,

Once it completed pushing and starting the application, you can access the URL, which is called as route in Cloud foundry terminology. In this case, monolithic-springmvc.cfapps.io is the route to access application. Look at the below screen to know about application details in pivotal application manager.

So, pushing war file into cloud foundry account is completed. Let’s move on to last step of testing the application.

Step 4: Test by using application route:

As I explained in Step 3, once we push application successfully in pivotal cloud foundry, it will generate a route to access application. As on you open the route, you will get the same output what we have got in Step 2 of testing in local tomcat environment. Look at the below screens,

We will get the below screen as on when we hit the Click Here hyper link shown in above screen,

 

Note: If you observe the above URL / Route, we are not passing any application context name like how we access in local. That will be taken care by Pivotal.

Stack ADT using Array in Java

The current post contains the custom implementation of Stack ADT using Array in Java. StackArray class is the reference of Stack implementation using Array with the fixed size elements.
  • push(): is used to store elements in Stack. It will throw StackOverflowError, if stack is full.
  • pop(): is used to remove top element from Stack. It will throw EmptyStackException, if Stack if empty.
  • peek(): is used to get top most element from Stack. It will throw EmptyStackException, if Stack if empty.
  • search(): is used to find the element is present or not in Stack. It will throw EmptyStackException, if Stack if empty.
  • show(): is used to print total elements present in Stack. It will throw EmptyStackException, if Stack if empty.
  • isEmpty(): is used to find the Stack is empty or not. It will return true/false based on Stack.
  • isFull(): is used to find the Stack is full or not. It will return true/false based on Stack.
  • size(): is used to fine the Stack size.
package com.dsalgorithms.stack;

import java.util.EmptyStackException;
/*
* Author : Subbareddy
*
* StackArray class is the reference of Stack implementation using Array in Java.
*
*/
public class StackArray {

private int size;
private int index = 0;
private int[] ar;

/*
* StackArray constructor is used to create an array with passing size
*
* Input: integer value
*/
public StackArray(int size){
this.size = size;
ar = new int[size];
}

/*
* push() used to insert element into the stack
*
* Input: integer value
*
* Throws StackOverflowError error if stack is full
*/
public void push(int element){
if(isFull())
throw new StackOverflowError();

ar[index] = element;
index++;
}

/*
* pop() method removes the top most element and return the deleted element
*
* Throws EmptyStackException if Stack is empty
*/
public int pop(){
if(isEmpty())
throw new EmptyStackException();

return ar[--index];
}

/*
* peek() method returns the top most element in the Stack
*
* Throws EmptyStackException exception if Stack is empty
*/
public int peek(){
if(isEmpty())
throw new EmptyStackException();

return ar[index];
}

/*
* search() method verifies the element is present or not in the Stack
*
* Input: integer value
*
* Throws EmptyStackException exception if Stack is empty
*/
public int search(int element){
if(isEmpty())
throw new EmptyStackException();

for(int i=0;i<index;i++){
if(ar[i] == element)
return ar[i-1];
}
return -1;
}

/*
* show() method will print Stack elements
*
* Throws EmptyStackException exception if Stack is empty
*/
public void show(){
if(isEmpty())
throw new EmptyStackException();

for(int i=0;i<index;i++){
System.out.println(ar[i]);
}
}

/*
* isEmpty() method validates whether the Stack is empty or not
*
* Throws EmptyStackException exception if Stack is empty
*/
public boolean isEmpty(){
if(index == 0)
return true;

return false;
}

/*
* isFull() method will return true or false based on Stack is full or not
*/
public boolean isFull(){
if(index == size)
return true;

return false;
}

/*
* size() method will return total stack size
*/
public int size(){
return index;
}



public static void main(String[] args) {
//Creating an Empty stack with the size of 10
StackArray stack = new StackArray(10);
System.out.println("Initial Size of Stack is: "+stack.size());

//Pushing 1 to 10 integer type elements into Stack
System.out.println("\nPUSH 1-10 VALUES INTO STACK");

for(int i=0;i<10;i++){
stack.push(i+1);
}

//Printing the Stack size after pushing elements into Stack
System.out.println("\nAfter PUSH stack size: "+stack.size());

//Printing total number of elements from Stack
stack.show();

//Remove/Pop top 5 elements from Stack
System.out.println("\nPOP 5 elements from Stack");
for(int i=0;i<5;i++){
System.out.println(stack.pop());
}

//Printing total elements after performing pop
stack.show();

//Searching for specific element from Stack
int position = stack.search(2);
if(position == -1)
System.out.println("\nElement not present in stack");
else
System.out.println("\nElement located in the position of "+position);

}

}