Tomcat source depth analysis of the underlying framework source code analysis

Overall architecture

tomcat general architecture as shown below (taken from http://blog.csdn.net/jiaomingliang/article/details/47393141) 



As shown above, the tomcat Server, Service, Engine, Connerctor, Host, Context components, wherein the representative s with a plurality of components may exist on a tomcat instance, such as the Context (s), allow us to deploy multiple tomcat applications, each corresponding to a Context. These components can be found in the tomcat conf / server.xml file, the tuning of the need to change the file tomcat


server.xml

<Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"

           connectionTimeout="20000"

           redirectPort="8443" />


    <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />


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


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

      <Realm className="org.apache.catalina.realm.LockOutRealm">

        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"

               resourceName="UserDatabase"/>

      </Realm>

      <Host name="localhost"  appBase="webapps"

            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log" suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

    </Engine>

</Service>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Server



Server component corresponds org.apache.catalina.Server interface class shown above in FIG. 

- Server inheritance to LifeCycle, LifeCycle is a very important interface, the major components inherit this interface for managing the life cycle of tomcat, such as init, start, stop, destory; in addition, it uses the Observer pattern, LifeCycle is a listener, it will send various events to LifecycleListener registered observers 

- Server provides findService, getCatalina, getCatalinaHome, getCatalinaBase interfaces, supports finding, traversing Service component, there seem to see little connection components and Serivce


public interface Server extends Lifecycle {


    public NamingResourcesImpl getGlobalNamingResources();


    public void setGlobalNamingResources(NamingResourcesImpl globalNamingResources);


    public javax.naming.Context getGlobalNamingContext();


    public int getPort();


    public void setPort(int port);


    public String getAddress();


    public void setAddress(String address);


    public String getShutdown();


    public void setShutdown(String shutdown);


    public ClassLoader getParentClassLoader();


    public void setParentClassLoader(ClassLoader parent);


    public Catalina getCatalina();


    public void setCatalina(Catalina catalina);


    public File getCatalinaBase();


    public void setCatalinaBase(File catalinaBase);


    public File getCatalinaHome();


    public void setCatalinaHome(File catalinaHome);


    public void await();


    public Service findService(String name);


    public Service[] findServices();


    public void removeService(Service service);


    public Object getNamingToken();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

Service

The default implementation class Service is StardardService, and StardardServer class structure is very similar, is inherited to LifecycleMBeanBase, implements Service Interface


Not difficult to find by the Service Interface Service component of the internal structure 

- Engine instance hold 

- holding Server instance 

- can manage multiple instances Connector 

- hold Executor references


public class StandardService extends LifecycleMBeanBase implements Service {

    // Code omitted several

}


public interface Service extends Lifecycle {


    public Engine getContainer();


    public void setContainer(Engine engine);


    public String getName();


    public void setName(String name);


    public Server getServer();


    public void setServer(Server server);


    public ClassLoader getParentClassLoader();


    public void setParentClassLoader(ClassLoader parent);


    public String getDomain();


    public void addConnector(Connector connector);


    public Connector[] findConnectors();


    public void removeConnector(Connector connector);


    public void addExecutor(Executor ex);


    public Executor[] findExecutors();


    public Executor getExecutor(String name);


    public void removeExecutor(Executor ex);


    Mapper getMapper();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

Connector

Connector is listening on TCP port tomcat in the assembly, server.xml defines two default Connector, respectively, for monitoring http, ajp port. Corresponding code is org.apache.catalina.connector.Connector, it is an implementation class, and implements the interface Lifecycle


http

<Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

1

2

3

Connector http corresponding configuration as shown above, wherein the version of the protocol to specify the http protocol, also supports 2.0; connectionTimeout defines the connection time, in milliseconds; redirect the redirectPort is SSL port, it will redirect the request to this port 8443


AJP

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

1

Apache jserver Protocol (AJP) is a binary protocol that the web server from the inbound request to a web server located on the application server. If we want to Tomcat integration into existing (or new) Apache http server and you want Apache to handle static content web application contains, or use the Apache SSL processing, we will be able to use this protocol. However, in the actual project application, AJP protocol is not commonly used, most scenarios will use nginx + tomcat load.


Container

api org.apache.catalina.Container interface defines the container, it is a handle and returns the user servlet request object to the user's web module, which has four different containers: 

- Engine, represents the entire Catalina servlet engine 

- Host, has represented a number of Context Web Hosting 

- Context, represents a Web application, a context that contains one or more wrapper 

- Wrapper, represents a standalone servlet




Engine, Host, Context, Wrapper has a default implementation class StandardXXX, are inherited to ContainerBase. In addition, the container further comprising a series of Lodder, Logger, Manager, Realm Resources, and the like


A container may have one or more sub-containers on the lower level, and a Catalina feature does not necessarily require the deployment of all four containers. Context has one or more of a wrapper, the wrapper as a bottommost container hierarchy, the container can not contain child. Adding from one container to another container may be used () method defined in the interface definition Container addChild:


public void addChild(Container child); 

1

Container may be used to delete a defined removeChild Container Interface () method:


public void removeChild(Container child); 

1

Also look for container interface supports sub-interfaces and access methods findChild method and findChildren all sub-set of interfaces:


public Container findChild(String name); 

public Container[] findChildren(); 

1

2

Engine



Engine represents a Catalina Servlet engine, Engine if used, then it is Catalina top container, in StardardCataline of the setParent () method throws exception directly


public interface Engine extends Container {


    public String getDefaultHost();


    public void setDefaultHost(String defaultHost);


    public String getJvmRoute();


    public void setJvmRoute(String jvmRouteId);


    public Service getService();


    public void setService(Service service);

}


public class StandardEngine extends ContainerBase implements Engine {


    // other code...


    public void setParent(Container container) {

        throw new IllegalArgumentException(sm.getString("standardEngine.notParent"));

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

server.xml


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

  <Realm className="org.apache.catalina.realm.LockOutRealm">

    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"

           resourceName="UserDatabase"/>

  </Realm>


  <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

           prefix="localhost_access_log" suffix=".txt"

           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

  </Host>

</Engine>

1

2

3

4

5

6

7

8

9

10

11

12

Host

Host defines a virtual host, the so-called virtual host, of course, it can be used to deploy applications, Tomcat's Host as well. It is defined in server.xml of a localhost Host, application root directory webapps below, the default is to support the redeployment of decompression.


<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">...</Host>

1

Context



Context represents a separate web application, for each Context, tomcat are using different Classloader avoid class conflicts. If we want to use the directory as a custom deployment path, you can add to Context in server.xml


<Context path="/static" docBase="D:/static" reloadable="true"></Context>

1

Code Module Introduction

catalina package

Part of Tomcat core modules, including the HttpServlet, to achieve HttpSession, and the realization of major components, this piece of code amount is the largest, most complex


coyote follicles

This is mainly to support various protocols, such as http1.1, http2.0, ajp etc., smaller amount of code


Guess you like

Origin blog.51cto.com/14524696/2455532