tomcat连接器-Connector之《how tomcat works》第四章 Tomcat Default Connector笔记

何志业
2023-12-01
A Tomcat connector is an independent module that can be plugged into a servlet

connector是一个可以嵌入到servlet容器的独立模块。

Examples include Coyote, mod_jk, mod_jk2, and mod_webapp.

现有的connector有Coyote, mod_jk, mod_jk2以及mod_webapp。参考:https://blog.csdn.net/weixin_42146366/article/details/98043043

A Tomcat connector must meet the following requirements: (注:连接器包含processor---见本章源码)
tomcat连接器必须满足以下条件:
	1. It must implement the org.apache.catalina.Connector interface.
	2. It must create request objects whose class implements the
	org.apache.catalina.Request interface.
	3. It must create response objects whose class implements the
	org.apache.catalina.Response interface.
Another point that needs attention is that the default connector implements all
features new to HTTP 1.1 as well as able to serve HTTP 0.9 and HTTP 1.0 clients.

需要注意的是默认的连接器实现了HTTP1.1协议,(协议是由connector来实现的!)

This section explains three new features of HTTP 1.1. Understanding them is crucial
to understanding how the default connector processes HTTP requests。

理解HTTP1.1是熟悉connector处理HTTP请求的关键。

considering that establishing and tearing down HTTP connections are expensive
operations.
The persistent connection is the default connection of HTTP 1.1. Also, to make it
explicit, a browser can send the request header connection with the value
keep-alive:
connection: keep-alive

考虑到HTTP连接的建立非常耗费资源,因此,目前HTTP1.1连接默认使用的是长连接,
浏览器可在请求头里面显示的设置属性 connection: keep-alive。

The consequence of establishing a persistent connection is that the server can send
byte streams from multiple resources, and the client can send multiple requests using
the same connection.

使用长连接带来的好处是:使用同一个连接服务器可以发送多个资源的字节流,而客户端可以使用同一个连接来发送多个请求。

A Tomcat connector must implement the org.apache.catalina.Connector interface.
Of many methods in this interface, the most important are getContainer,
setContainer, createRequest, and createResponse.

(Connector接口中最重要的4个方法):

setContainer is used to associate the connector with a container. getContainer
returns the associated container. createRequest constructs a request object for the
incoming HTTP request and createResponse creates a response object.

1、setContainer: 关联一个container(容器);
2、getContainer: 返回关联的container(容器);
3、createRequest:为输入的HTTP请求构建一个request对象;
4、createResponse:创建一个response对象。

In the default connector,
the HttpConnector has a pool of HttpProcessor objects and each instance of
HttpProcessor has a thread of its own. Therefore, the HttpConnector can serve
multiple HTTP requests simultaneously.

默认的connector具有HttpProcessor对象池,每一个HttpProcessor实例都有自己的线程。因此,HttpConnector可以同时处理多个HTTP请求。

The HttpConnector maintains a pool of HttpProcessor instances to avoid creating
HttpProcessor objects all the time.

HttpConnector维护了一个HttpProcessor实例池以避免一直创建HttpProcessor对象

After this point is reached and there
are still not enough HttpProcessor instances, the incoming HTTP requests will be ignored.

当HttpProcessor实例的数量达到最大的设定值时,输入进来的HTTP请求将会被忽略。

Therefore, each instance is associated with
a request object and a response object. The HttpProcessor class's constructor
contains calls to the HttpConnector class's createRequest and createResponse
methods.

每个HttpProcessor实例对应一个request对象和response对象,其中的reqeust对象和response对象在HttpProcessor的构造器中完成创建。

The HttpConnector class has its main logic in its run method, just like in Chapter 3.
The run method contains a while loop where the server socket waits for an HTTP
request until the HttpConnector is stopped.
	Socket socket = null;
	 try {
	 socket = serverSocket.accept();
	 ...

HttpConnector类在它的run方法中有自己的主要逻辑,run方法包括一个while循环:使用server socket等待HTTP请求一直到HttpConnector停止。

For each incoming HTTP request, it obtains an HttpProcessor instance by calling
the createProcessor private method.
	HttpProcessor processor = createProcessor();

HTTP请求到来后会生成一个socket后,然后会创建一个HttpProcessor对象用于处理这个socket(HTTP请求)。

However, most of the time the createProcessor method does not create a new
HttpProcessor object. Instead, it gets one from the pool.

大多数情况下不会直接new一个HttpProcessor对象而是从对象池里面取现成的。

If createProcessor does not return null, the client socket is passed to the
HttpProcessor class's assign method:
	processor.assign(socket);
It's now the HttpProcessor instance's job to read the socket's input stream and
parse the HTTP request.

生成的socket会被传递给HttpProcessor的assign(socket)方法,assgin方法会触发HttpProcessor的其他方法去处理输入流并解析HTTP请求。

An important note is this. The assign method must return
straight away and not wait until the HttpProcessor finishes the parsing, so the next
incoming HTTP request can be served.

值得注意的一点是:assign方法必须马上返回,而不能等HttpProcessor完成解析才返回,只有这样才能马上处理下一个输入的HTTP请求。

the HttpProcessor class makes its assign method
asynchronous so that the HttpConnector instance can serve many HTTP requests at
the same time.

因为HttpProcessor类的assign方法是异步的,所以HttpProcessor的实例可以同时处理很多HTTP请求。

如果不使用assign方法(机制),HttpProcessor就必须把当前的HTTP请求处理完成后才能处理下一个HTTP请求,见如下内容:

In Chapter 3, the HttpConnector runs in its own thread. However, it has to wait for
the currently processed HTTP request to finish before it can process the next:
request. Here is part of the HttpConnector class's run method in Chapter 3:
	public void run() {
	 ...
	 while (!stopped) {
	 Socket socket = null;
	 try {
	 socket = serversocket.accept();
	 } catch (Exception e) {
	 continue;
	 }
	 // Hand this socket off to an Httpprocessor
	 HttpProcessor processor = new Httpprocessor(this);
	 processor.process(socket);
	 }
	 }
The process method of the HttpProcessor class in Chapter 3 is synchronous.
Therefore, its run method waits until the process method finishes before accepting
another request.

在第3章中,HttpConnector运行在它自己的线程里。然后它必须等待当前HTTP请求处理完成后才能处理下一个HTTP请求。
处理方法process是同步的,因此,run方法必须等到process方法执行完成(返回后)才能进行下一轮while循环(接收HTTP请求),见如上代码。

Note The wait method causes the current thread to wait until another thread invokes
the notify or the notifyAll method for this object.

注意wait方法会是当前线程一直处于等待状态直到另外一个线程调用了这个对象(同一个对象)的notifyAll方法。

All applications in the upcoming chapters use the default connector.

在接下来数章中所有的应用都是采用的本章讨论的默认连接器。

 类似资料: