Tag Archive: Tomcat


 yum install java 

To export your JDK (i.e. set your environment variable) for applications like Tomcat:

export JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64/ 

To start Tomcat you need to make the sh-scripts executable, e.g. execute in your tomcat/bin directory

 chmod 774 *.sh 

Download Apache 2 (I used 2.2.22 without crypto for windows) from http://httpd.apache.org/download.cgi

Install Apache 2 (I used localhost as the domain)

Add the following entry to httpd.conf located under C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf

LoadModule jk_module modules/mod_jk.so
<IfModule jk_module>
     JkWorkersFile conf/workers.properties
     JkLogFile logs/mod_jk.log
     JkLogLevel info
     JkMountFile conf/uriworkermap.properties
     JkMount /jkmanager/* jkstatus 
</IfModule>
#Enable the JK manager access from localhost only
<Location /jkmanager/>
    JkMount jkstatus
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>

Place the file workers.properties in the same location with the follwoing contents:

#
# This file provides minimal jk configuration properties needed to
# connect to Tomcat.
#
# The workers that jk should create and work with
#
worker.list=loadbalancer,jkstatus

#
# Define the servers in our cluster
#
worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009
worker.tomcat1.lbfactor=1

worker.tomcat2.type=ajp13
worker.tomcat2.host=localhost
worker.tomcat2.port=8010
worker.tomcat2.lbfactor=1

#
# Defining a load balancer
#
worker.loadbalancer.type=lb
#worker.loadbalancer.balance_workers=tomcat1
worker.loadbalancer.balance_workers=tomcat1,tomcat2
worker.loadbalancer.sticky_session=false

#
# Define status worker
#
worker.jkstatus.type=status

Now create the file uriworkermap.properties in the same directory and add the follwoing contents:

# This file provides sample mappings for example wlb
# worker defined in workermap.properties.minimal
# The general syntax for this file is:
# [URL]=[Worker name]

/*.*=loadbalancer

Now go to the Tomcat and change the configuration in server.xml (e.g. under C:\Tools\apache-tomcat-6.0.35worker1\conf):

The AJP connector should add a redirect port to the HTTP Connector that is already configured in the server.xml:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8080" />

The HTTP connector should like similar to this:

 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

Also specify the jvmRoute for the Engine:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

For the second tomcat (worker2) when also running on localhost, you should increase the following port numbers by 1:

  • AJP Connector (from 8009 to 8010 and redirectPort from 8080 to 8081)
  • HTTPConnector (from 8080 to 8081)
  • Server Port from (8005 to 8006)

and of course you should specify tomcat2 as jvmRoute in the engine.

On the second worker node (i.e. on the second tomcat) delete tomcat.gif under webapps/ROOT and rename tomcat-power.gif to tomcat.gif.

Now start both Tomcats and the Apache 2 server. Then start your browser and point it to localhost/tomcat.gif.

On each refresh you should see the two different tomcat pictures, alternating.

 

Under http://localhost/jkmanager/ you can find a management console for the tomcat workers. Here you can for example stop a tomcat from receiving requests from the loadbalancer and then start it again.

ContextListener

In die web.xml (z.B im Axis Web-INF Verzeichnis) können ContextListener eingetragen werden. Diese ermöglichen es beim Start von Tomcat Java Code auszuführen.

Ein ContextListener sieht wie folgt aus:

package de.fb12.sd;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ServerContextListener implements ServletContextAttributeListener, ServletContextListener
{

  public void attributeAdded(ServletContextAttributeEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void attributeRemoved(ServletContextAttributeEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void attributeReplaced(ServletContextAttributeEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void contextInitialized(ServletContextEvent arg0)
  {
    System.out.println("START******************************************");
  }

  public void contextDestroyed(ServletContextEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }
}

Die Interfaces ServletContextAttributeListener, ServletContextListener finden man im Verzeichnis TOMCAT_HOME/common/lib in der Datei servlet-api.jar

Der Eintrag in die web.xml sieht folgendermaßen aus:





  Apache-Axis
    
    
      
       org.apache.axis.transport.http.AxisHTTPSessionListener
      
      de.fb12.sd.ServerContextListener
    
...
    


ProtocolHandler

Für den Catalina Service kann in der Datei server.xml für den HTTP oder AJT Connector ein eigener Port angegeben werden. Wenn keine Angabe gemacht wird, wird standardmäßig der Coyote HTTP Connector verwendet.

  


Über das Attribut protocol kann angegeben werden, welcher ProtocolHandler verwendet werden soll.

Ein ProtocolHandler implementiert das Interface org.apache.coyote.ProtocolHandler. Ein Beispiel könnte wie folgt aussehen:

package de.fb12.pipt.tomcat;

import java.util.Hashtable;
import java.util.Iterator;

import org.apache.catalina.connector.CoyoteAdapter;
import org.apache.coyote.Adapter;
import org.apache.coyote.ProtocolHandler;

import de.fb12.pipt.VectorServer;

public class PiptProtocolHandler implements ProtocolHandler
{
  Adapter adapter = null;
  CoyoteAdapter coyoteAdapter = null;
  Hashtable attributes = new Hashtable();
  
  public PiptProtocolHandler() throws Exception
  {
    System.out.println("******Constructor*****");
  }

  public void setAttribute(String key, Object value)
  {
    System.out.println("******setAttribute*****");
    attributes.put(key, value);  
  }

  public Object getAttribute(String arg0)
  {
    // TODO Auto-generated method stub
    System.out.println("******getAttribute*****");
    return null;
  }

  public Iterator getAttributeNames()
  {
    // TODO Auto-generated method stub
    System.out.println("******getAttributeNames*****");
    return null;
  }

  public void setAdapter(Adapter adapter)
  {
    /*System.out.println("****Adapter**** " + arg0.toString());
    adapter = new PiptAdapter();*/
    //receive CoyoteAdapter
    this.adapter = adapter;
    if (adapter instanceof CoyoteAdapter)
    {
      coyoteAdapter = (CoyoteAdapter) adapter;
      System.out.println("Adapter is instanceof CoyoteAdapter");
    }
    System.out.println("******setAdapter*****");
  }

  public Adapter getAdapter()
  {
    System.out.println("******getAdapter*****");
    return adapter;
  }

  public void init() throws Exception
  {
    //Processor processor = new Processor()
    System.out.println("******init*****");
  }

  public void pause() throws Exception
  {
    // TODO Auto-generated method stub
    System.out.println("******pause*****");
  }

  public void resume() throws Exception
  {
    // TODO Auto-generated method stub
    System.out.println("******resume*****");
  }

  public void destroy() throws Exception
  {
    // TODO Auto-generated method stub
    System.out.println("******destroy*****");
  }

  public void start() throws Exception
  {
    System.out.println("******Start*****");
    VectorServer vs = VectorServer.getInstance();
    if (coyoteAdapter != null)
    {
      //vs.startServer(8001, "Dummy");
    }
  }
}

Service
Um einen eigenen Service zu schreiben muss man das org.apache.catalina.Service Interface implementieren. In der initialize() Methode können Server u.ä. gestartet werden. Da das Interface Service benötigt wird, muss catalina.jar auf den Classpath gelegt werden.


package de.fb12.sd.old;

import java.io.File;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.catalina.Container;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.fb12.sd.Utils;
import de.fb12.sd.VectorServer;

public class TestTomcatService implements Service
{

  public Container getContainer()
  {
    // TODO Auto-generated method stub
    return null;
  }

  public void setContainer(Container arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public String getInfo()
  {
    // TODO Auto-generated method stub
    return null;
  }

  public String getName()
  {
    // TODO Auto-generated method stub
    return null;
  }

  public void setName(String arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public Server getServer()
  {
    // TODO Auto-generated method stub
    return null;
  }

  public void setServer(Server arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void addConnector(Connector arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public Connector[] findConnectors()
  {
    // TODO Auto-generated method stub
    return null;
  }

  public void removeConnector(Connector arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void initialize() throws LifecycleException
  {
    System.out.println("START********************************");
    VectorServer server = VectorServer.getInstance();
    
    try
    {
      DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = fac.newDocumentBuilder();
      Document doc=db.parse(new File(de.fb12.sd.Constants.SERVER_CONFIG_WSDD));
      NodeList services = doc.getElementsByTagName("service");
      for (int i = 0; i < services.getLength(); i++)
      {
        Node serv = services.item(i);
        Element service = (Element) serv;
        
        NamedNodeMap map = service.getAttributes();
        Node attr = map.getNamedItem("name");
        String serviceName = attr.getNodeValue();
        System.out.println("**********************" + serviceName);
        String locations = Utils.getLocationForParameters(serviceName);
        if (locations != null)
        {
          StringTokenizer st = new StringTokenizer(locations, " ");
          while(st.hasMoreElements())
          {
            int j = 0;
            String location = st.nextToken();
            if (!location.equals("-"))
            {
              int port = Utils.getPortFromLocation(location);
              server.startServer(port, serviceName + j);
            }
          }
        }
      }
    }
    catch(Exception e)
    {
      System.out.println(e.toString());
      e.printStackTrace();
    } 
  }
}

Die Serviceklasse muss nach %TOMCAT_HOME%/server/classes kopiert werden.
Folgender Eintrag muss in die %TOMCAT_HOME%/conf/server.xml gemacht werden: