I was busy at home some time ago, and I stopped updating for a long time. I would like to say sorry to the friends who are waiting for the update. I couldn’t explain it in advance. I am really sorry to make you wait for so long!
TomcatThe initialization and startup steps, then the next step is to enter the highlight! In this article, as before, I will lead my friends to understand a
tomcatWhat are the specific processing links?
1. Request analysis
The description of each component is noted in “Tomcat Source Code Learning Part 2”.
servletWhen a request comes, the first thing that goes through is
connectorComponent, which is used to receive requests.
After the component receives the request, it will encapsulate the relevant request and then pass it to
enginein the component.
engineComponents will lock the corresponding
contextas well as
wrapper, passed down layer by layer to find the servlet instance that finally processes the request.
2. In-depth exploration
I don’t know if you still have any impression. In the previous article, we
AccepterThere is also a thread group running above the entrance of the thread, but it does not explain what the thread is used for~
Click to jump to this class, we can see that it has been implemented
Runnableinterface, then we directly check his
run()method to see its operating logic.
We can know from the comments that this thread is mainly used to poll the connected socket, check whether an event is triggered, and hand over the associated socket to the corresponding processor when the event occurs. In the source code we can see
keyCountVariables record the number of pending requests and provide them for subsequent judgments.
keep going down through
keyCountDetermine whether there is a request that needs to be processed, and if necessary, pass
selector.selectedKeys()get what needs to be processed
channelCollection, for loop processing. exist
whileIn the loop we see that all ready channels call the
processKey(sk, socketWrapper)method to process.
Click to jump to this method, where you can see that he
skMake a read and write judgment. Since it is a request, it must be a read operation. Let’s take a look at the related methods first.
After entering, we can see that it first tries to obtain a processing thread in the cache pool. When there is no thread in the cache pool, it creates a new thread, and uses it directly if there is one.
Since it is a thread, then we only care about the core method of the thread. click
SocketProcessorBaseJump to view
doRun()Mark a breakpoint, click Next, and jump to
PollerThe thread is handed over to the thread here for processing, in which the current
socket, for further processing.
Once inside the method, we can see that it first calls the
wrapperMake a judgment, take it out if it is not empty
socket, and then try the
connectionsto obtain the corresponding
processor, if it cannot be obtained, try to obtain the connection that has been processed but has not been destroyed
processorTo get it, create it if you can’t get it. This avoids frequent object creation and destruction.
processorAfter that, call
processmethod to parse the message.
After entering this method, we can see that there is a pair of
socketEventTo judge the state, our current request is mainly to read the state, put a breakpoint here, and jump to this method to have a look.
Here we can see that we have entered
http11In the class, the message is parsed in this class, and the native
responseBecause we haven’t reached the step of returning, we just make a preliminary parameter setting. to be passed in later
AdapterProceed to the next step.
here for native
responseconvert to get
HttpServletResponse. Then find the one that can handle the current request based on the request information
In this method you can see that it will pass
getMapper()method to match the one that can handle the current request
host,context,wrapper. Some friends here may wonder why it is from
mapperto match? I leave this question to you to explore, and I will answer it for you in the next chapter.
In the previous method, by
serviceand then get the corresponding
mapper, but did not see the
mapperObject construction, where did the object come from?
I don’t know if you still remember that in the second article, in
mapperListenerMethod initialization and startup.
In this method find the corresponding
host, context, wrapper。
EvalusteWe can see the corresponding
host, context, wrapperand instance mappings are found.
The next thing to do is to make a level-by-level call according to the link component until the final extraction
host, after passing
hostContinue to call the next level component
get it here
wrapperAfter that, continue to execute downwards, from
wrapperobtained in the container
Next, put the obtained
servletAdd it to the filter chain (there may be other processing, which is not directly processed here), and it will be processed after calling the filter chain below.
Finally found a specific example, it is not easy! ! !
I have collected manycomputer e-books, if you need it, please mention it yourself~