Thursday, October 14, 2010

Setting Up Second Tomcat JVM Instance


General

If you've gone through all the previous steps in this HOWTO, then the following steps should be very easy to follow and to understand without much explanations. Therefore, I'll provide here just the steps for setting up a second Tomcat JVM instance and an application called "Order".

Steps for Second Tomcat JVM Instance and Application

Login as root and execute the following steps to setup the second Tomcat JVM instance:
# mkdir -p /opt/tomcat-instance/order.example.com
# cd /opt/tomcat-instance/order.example.com
#
# cp -a /var/lib/apache-tomcat-6.0.18/conf .
# mkdir common logs temp server shared webapps work
#
# chown -R tomcat.tomcat /opt/tomcat-instance/order.example.com
#
# su - -s /bin/sh tomcat
$ cat > /opt/tomcat-instance/order.env << EOF
export JAVA_HOME=/usr/java/jdk1.6.0_10
export PATH=\$JAVA_HOME/bin:\$PATH
export CATALINA_HOME=/var/lib/apache-tomcat-6.0.18
export CATALINA_BASE=/opt/tomcat-instance/order.example.com
EOF
$
$ source /opt/tomcat-instance/order.env
$
For the second Tomcat JVM instance the default port numbers need to be changed in $CATALINA_BASE/conf/server.xml (/opt/tomcat-instance/order.example.com/conf/server.xml). In the following example I increased the port numbers by one:
<Server port="8006" shutdown="SHUTDOWN">

    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" />

    <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
Create a new application root directory:
$ mkdir $CATALINA_BASE/webapps/order
To configure the new web application, edit $CATALINA_BASE/conf/server.xml (/opt/tomcat-instance/order.example.com/conf/server.xml) and add the following entry in blue:
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <Context docBase="order" path="/myorder"/>
Create a new home page for the new "Order" application and include a link to the Java servlet that will be setup next:
$ cat > $CATALINA_BASE/webapps/order/index.html << EOF
<!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"></HEAD>
<BODY>
<H3>Apache Tomcat Order Home Page</H3>
<a href="/myorder/execute">Execute Order</a>
</BODY>
</HTML>
EOF
$
Now setup and create a new Java servlet:
$ mkdir -p $CATALINA_BASE/webapps/order/WEB-INF/classes
$ mkdir $CATALINA_BASE/webapps/order/WEB-INF/lib
$ cat $CATALINA_BASE/webapps/order/WEB-INF/classes/Order.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Order extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Order Page</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Executing Order ...</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}
Compile the new Java servlet:
$ cd $CATALINA_BASE/webapps/order/WEB-INF/classes
$ javac -classpath "$CATALINA_HOME/lib/*" Order.java
$ ls
Order.class  Order.java
$
Configure the Java servlet:
$ cat $CATALINA_BASE/webapps/order/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    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"
    version="2.5">

  <servlet>
    <servlet-name>servlet_order</servlet-name>
    <servlet-class>Order</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>servlet_order</servlet-name>
    <url-pattern>/execute</url-pattern>
  </servlet-mapping>

</web-app>
Now make sure to relogin as tomcat and start the second Tomcat JVM instance:
# su - -s /bin/sh tomcat
$ source /opt/tomcat-instance/order.env
$ $CATALINA_HOME/bin/startup.sh
After the second Tomcat JVM restarted, open the URL http://localhost:8081/myorder (or use the server name instead of localhost) and you should see the "Execute Order" link. Clicking on this link should invoke the Java servlet and display "Executing Order" in your browser. If you are presented with an empty page instead, review the above steps and make sure you didn't miss a step. Check also the log files under $CATALINA_BASE/logs.

No comments:

Post a Comment