J.APR connector monolithic block diagram (with SSL implementation analysis)

Source: Internet
Author: User


The idea of the APR connector and the overall architecture of the Bio,nio are similar, you can see the following overall block diagram: The first difference is that for the socket from the acceptor thread parsing this piece, whether it is nio or bio are inAcceptorthread within the direct blocking execution, for the APR channel, make a socketwithoptions thread, specifically to perform this socket parsing work, and then directly to the Poller thread for poll;second, SSL interaction and socket reception are all done by invoking Tomcat-native's JNI code. The following through the source code analysis, the entire block diagram of the comparison of the core parts.
0.LifecycleListener ForLifecyclelistener, each component that is placed in Tomcat, as long as it implements theLifecycle andLifecyclelistener will have life cycle events, and when the component starts in Tomcat, the stop will golifecycle event, and thencall the correspondingLifecycleListenermethod, let's take an example:A. Custom listener class, the code into any name of the jar package placed under the Tomcat/lib
Package Com.test.listener;
import org.apache.catalina.*;
Public class MyListener implements lifecyclelistener{
Public void Lifecycleevent (Lifecycleevent event) {
System.out.println ("Component type:" +event.getlifecycle ());
System.out.println ("Life cycle phase:" +event.gettype ());
       }
    } 

B. Configure Tomcat/conf/server.xml,
add a sentence <listener classname= "Com.test.listener.MyListener"/>

c. Get System.out.print output on run Tomcat,console

component type: standardserver[8005]
life cycle phase: Before_init
component type: standardserver[8005]
life cycle phase: After_init
component type: standardserver[8005]
life cycle phase: Before_start
component type: standardserver[8005]
life cycle phase: Configure_start
component type: standardserver[8005]
life cycle phase: Start
component type: standardserver[8005]
life cycle phase: After_start.....
These are the events that are triggered by each component in Tomcat:Before_init,After_init will trigger the components of the initinternal, will do some loading resources, libraries and other preparatory work;Configure_start is a configuration property set to an event in the component;Start,the start of the component is called between After_starttinternal, the real loading of the component is done;This is initiated when the relevant event, stop, destroy also have corresponding events and the above process is corresponding;we can see through these events from the log that the components running in Tomcat are in what state;
1.aprlifecyclelistener for APR channels, you need to configure a lifecyclelistener in Server.xml:
This means that when the APR channel is started, the APR component needs to load some content, the main content is the library that tomcat-native needs to rely on. For listener to inherit the Lifecyclelistener interface, implement its Lifecycleevent method, which is a few life-cycle events in the previous section. From the code can be analyzed, when the component init, first init loading native library, and then based on Apr and OpenSSL is loaded, to start the SSL configuration, after the component is destroyed, unload the native library, so as not to occupy the operating system resources. First look at the Init method: For the Init method, load the native resource through the library class, passing some parameters of the APR. The second step is the Initialize method, whichInitialize is primarily the start of OpenSSL:The way to call is through the Tomcat-native SSL class, this SSL class method is native, which through C to directly start Opensslengine, in addition, you can see the Tomcat-native home page mentioned in the United States FIPS security certification, Tomcat also provides support in this block.
2.http11aprprotocol
Http11aprprotocol is the total control of the HTTP protocol implementation of the APR channel, which, like the Nio,bio channel, holds endpoint and handler:Second, for the various attributes of the APR channel, thisHttp11aprprotocolit only acts as a proxy:
Its final properties are set to Aprendpoint and then distributed to logic when the channel is started.
3.AprEndpointAprendpoint is the main implementation class of APR channel, responsible for several thread pool boot, socket attribute processing, according to the Convention we still look at its bind (initialize), start (start) two processes.
The bind method is generally to start the serversocket binding, and for APR, its socket is the operating system socket, which is the process of JNI:
The APR-based implementation Logic first invokes the APR's memory pool, then creates the APR's ServerSocket pool and finally passesServerSocket Pool CreationServerSocket;All three of these calls are system calls, that is, the socket generation of different operating systems through JNI. second, the Bind method also calls the SSL boot, where the call will eventually configure the APR channel configured SSL parameters into OpenSSL:
Sslcontext.make is actually also the native method, which calls its internal sslcontext generation through OpenSSL, where the sslcontext is simply a return value that identifies whether the call was successful, and the rest of the operation is to set the APR's SSL property to OpenSSL This JNI invocation is also used:

4.acceptor/ poller/ SocketprocessorThe Bind method is initialized, and when start starts, several thread pools of Aprendpoint are started:
You can see 5 types of threads from the start code above.
First, take a look at the main function of the acceptor thread to receive the socket request, which is also the system call:
However, in the APR channel, because the socket setting property calls the system call, which is the JNI code, in order to prevent the loss of the performance of this block, make a separate thread for the socket options processing, that is socketoptionsprocessor:
Wrap the socket into a aprsocketwrapper and pass it toin Socketoptionsporcessor;Socketoptionsporcessor threads use a working thread pool, this needs to be noted;

Socketoptionsporcessor thread, the socket property is set, added to the addlist, passed to the Poller thread for the socket;
The Poller thread primarily maintains the socket's read and Write,acceptor threads when the socket comes in, subsequent reads and writes are delegated to Poller, and the Poller thread also has further packaging handling of Comet,socket, There are also time-out controls about Pollertimeout, which are processed and the rest are handed toSocketporcessor;the rest of the process is similar to other channels, and here is no longer redundant.
Summary:
can summarize:APR Channel whether the socket is created, or the SSL engine is started, and so on, these are made with JNI code calls to the underlying system calls, because the number of calls to the system is called, so for the entireApr call chain, the system calls more parts, more use a thread to do these things asynchronously, so as to improve the overall efficiency.



From for notes (Wiz)

J.APR connector monolithic block diagram (with SSL implementation analysis)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.