Wednesday, March 13, 2013

Tomcat Connectors


Connector elements are Tomcat's links to the outside world, allowing Catalina to receive requests, pass them to the correct web application, and send back the results through the Connector as dynamically generated content.

Each Connector element represents a port that Tomcat will listen to for requests.

A Connector performs following jobs

  1. Routing user requests to appropriate Services
  2. To link Tomcat with other supporting web technologies, such as Apache webserver.

The Connector element does only one job - listening for requests, passing them on to an Engine, and returning the results to its specified port.  

On its own, the Connector can't function - the only information this element contains is a port to listen on and talk to, and some attributes that tell it exactly how to listen and talk. 

Connector elements are nested within hierarchies of Services and Engines, to achieve the desired functionality.

This is how a Tomcat server configuration looks like in server.xml file

<Server>
    <Service name="Catalina">
           <Connector port="8080"/>
           <Connector port="8443"/>
        <Engine name="Catalina">
             <Host name="yourhostname">
                    <Context path="/webapp1"/>
                    <Context path="/webapp2"/
             </Host>
        </Engine>
    </Service>
</Server>

Using the above arrangement, both Connectors(one listening on port 8080 and other on port 8443) will pass all requests to the same Engine, which will in turn pass all these requests to both of its contained web applications - webapp1 and webapp2.  This means that each request(either to port 8080 or 8443) will potentially generate two responses, one from each application(webapp1 and webapp2).

<Server>
  <Service name="Catalina">
    <Connector port="8080"/>
       <Engine>
          <Host name="yourhostname">
               <Context path="/webapp1"/>
          </Host>
       </Engine>
  </Service>

  <Service name="Catalina8443">
      <Connector port="8443"/>
         <Engine>
            <Host name="yourhostname">
                <Context path="/webapp2"/>
            </Host>
         </Engine>
  </Service>
</Server>


There are two Connector elements here, listening for connections on ports 8080 and 8443.  It is important to note that an OS will only allow one connector on each port, so every connector you define will require its own unique port.


Tomcat has two types of Connectors

  1. HTTP
  2. AJP

HTTP Connectors

Tomcat comes with a preconfigured HTTP connector that can handle incoming HTTP requests from a browser, because of this Tomcat can act as a standalone Web server, it can handle both HTTP and HTTPS requests.

HTTP connectors are Java classes that implement the HTTP protocol. By default, the HTTP Connector listens on port 8080.

There are a number of HTTP Connectors available

1) JIO(java.io) - Java based HTTP/1.1(Coyote) Connector :
     This is Tomcat’s default connector implementation. It is a pure Java TCP sockets server implementation that uses the java.io core Java network classes. It is a fully blocking implementation of both HTTP and AJP. Being written in pure Java, it is binary portable to all operating systems that fully support Java.

<!-- The stock HTTP JIO connector. -->
<Connector port="8080" protocol="HTTP/1.1"
    maxThreads="150" connectionTimeout="20000"
    redirectPort="8443" />


2) NIO(java.nio) - 
     This connector, written in Java like JIO, uses the java.nio core Java network classes that offer nonblocking TCP socket features. The main goal of this Connector design is to offer Tomcat administrators a Connector implementation that performs better than the JIO Connector by using fewer threads by implementing parts of the Connector in a nonblocking fashion. The fact that the JIO Connector blocks on reads and writes means that if the administrator configures it to handle 400 concurrent connections, the JIO Connector must spawn 400 Java threads. The NIO Connector, on the other hand, needs only one thread to parse the requests on many connections, but then each request that gets routed to a servlet must run in its own thread(a limitation mandated by the Java Servlet Specification). Since part of the request handling is done in nonblocking Java code, the time it takes to handle that part of the request is time that a Java thread does not need to be in use, which means a smaller threadpool can be usedto handle the same number of concurrent requests. A smaller threadpool usually means lower CPU utilization.

<!-- HTTP NIO connector. -->
<Connector port="8080"
   maxThreads="150" connectionTimeout="20000"
   redirectPort="8443"
   protocol="org.apache.coyote.http11.Http11NioProtocol"/>

3) APR (Apache Portable Runtime) -
    APR works well in Windows and Linux environment, it is written using APR and compiled to native code for optimized platform specific performance. It is not a complete Connector, it actually makes use of the standard Java-based connector for most of its operations. 

<!-- The HTTP APR connector. -->
<Connector port="8080"
      protocol="org.apache.coyote.http11.Http11AprProtocol"
      enableLookups="false" redirectPort="8443"
      connectionTimeout="20000"/>

AJP Connectors


For running Tomcat behind Apache(using Apache as a proxy server), AJP connectors are needed.

AJP Connectors work in the same way as HTTP Connectors, but they use the AJP protocol in place of HTTP.  Apache JServ Protocol, or AJP, is an optimized binary version of HTTP that is typically used to allow Tomcat to communicate with an Apache web server. 

The AJP protocol is a TCP packet-based binary protocol with the goal of relaying the essentials of HTTP requests to another server software instance significantly faster than could be done with HTTP itself. The premise is that HTTP is very plain-text oriented, and thus requires slower, more complex parsers on the server side of the connection, and that if we instead implement a binary protocol that relays the already-parsed text strings of the requests, the server can respond significantly faster, and the network communications overhead can be minimized.

mod_jk :
   This is an Apache httpd module that implements the client end of the AJP protocol (Tomcat’s AJP Connectors implement the server side of the protocol).

mod_proxy_ajp :
         This is mod_proxy’s AJP protocol connector support module. It connects with Tomcat via TCP to Tomcat’s AJP server port, sends requests through to Tomcat, waits for Tomcat’s responses, and then Apache httpd forwards the responses to the web client(s). The requests go through Apache httpd to Tomcat and back,and the protocol used between Apache httpd and Tomcat is the AJP protocol, just as it is with mod_jk. This connector became part of Apache httpd itself as of httpd version 2.2 and is already built into the httpd that comes with most operating systems (or it is prebuilt as a loadable httpd module). No extra compilation or installation is usually necessary to use it — just configuration of Apache httpd. Also, this module is a derivative of mod_jk, so mod_proxy_ajp’s code and features are very similar to those of mod_jk.

For Apache to serve as proxy for Tomcat via AJP, using mod_proxy module of Apache, in Apache configuration file, set the following

ProxyPass               /   ajp://127.0.0.1:8009/
ProxyPassReverse  /   ajp://127.0.0.1:8009/

mod_proxy_http
      This is mod_proxy’s HTTP protocol connector support module. Like mod_proxy_ajp, it connects with Tomcat via TCP, but this time it connects to Tomcat’s HTTP (web) server port. A simple way to think about how it works: the web client makes a request to Apache httpd’s web server, and then httpd makes that same request on Tomcat’s web server, Tomcat responds, and httpd forwards the response to the web client. All communication between Apache httpd and Tomcat is done via HTTP when using this module. This connector module is also part of Apache httpd, and it usually comes built into the httpd binaries found on most operating systems. It has been part of Apache httpd for a very long time, so it is available to you regardless of which version of Apache httpd we run.

For Apache to serve as proxy for Tomcat via HTTP, using mod_proxy module of Apache, in Apache configuration file, set the following

ProxyPass               /   http://127.0.0.1:8080/
ProxyPassReverse  /    http://127.0.0.1:8080/


No comments:

Post a Comment