2021-02-23 分類: 網(wǎng)站建設(shè)
從連接器(Connector)源碼說起
既然是來解析連接器(Connector),那么我們直接從源碼入手,后面所有源碼我會(huì)剔除不重要部分,所以會(huì)忽略大部分源碼細(xì)節(jié),只關(guān)注流程。源碼如下(高能預(yù)警,大量代碼):
- public class Connector extends LifecycleMBeanBase {
- public Connector() {
- this("org.apache.coyote.http11.Http11NioProtocol");
- }
- public Connector(String protocol) {
- boolean aprConnector = AprLifecycleListener.isAprAvailable() &&
- AprLifecycleListener.getUseAprConnector();
- if ("HTTP/1.1".equals(protocol) || protocol == null) {
- if (aprConnector) {
- protocolHandlerClassName = "org.apache.coyote.http11.Http11AprProtocol";
- } else {
- protocolHandlerClassName = "org.apache.coyote.http11.Http11NioProtocol";
- }
- } else if ("AJP/1.3".equals(protocol)) {
- if (aprConnector) {
- protocolHandlerClassName = "org.apache.coyote.ajp.AjpAprProtocol";
- } else {
- protocolHandlerClassName = "org.apache.coyote.ajp.AjpNioProtocol";
- }
- } else {
- protocolHandlerClassName = protocol;
- }
- // Instantiate protocol handler
- ProtocolHandler p = null;
- try {
- Class<?> clazz = Class.forName(protocolHandlerClassName);
- p = (ProtocolHandler) clazz.getConstructor().newInstance();
- } catch (Exception e) {
- log.error(sm.getString(
- "coyoteConnector.protocolHandlerInstantiationFailed"), e);
- } finally {
- this.protocolHandler = p;
- }
- // Default for Connector depends on this system property
- setThrowOnFailure(Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE"));
- }
我們來看看Connector的構(gòu)造方法,其實(shí)只做了一件事情,就是根據(jù)協(xié)議設(shè)置對(duì)應(yīng)的ProtocolHandler,根據(jù)名稱我們知道,這是協(xié)議處理類,所以連接器內(nèi)部的一個(gè)重要子模塊就是ProtocolHandler。
關(guān)于生命周期
我們看到Connector繼承了LifecycleMBeanBase,我們來看看Connector的最終繼承關(guān)系:
我們看到最終實(shí)現(xiàn)的是Lifecycle接口,我們看看這個(gè)接口是何方神圣。我把其接口的注釋拿下來解釋下
這段注釋翻譯就是,這個(gè)接口是提供給組件聲明周期管理的,并且提供了聲明周期流轉(zhuǎn)圖。這里我們只需要知道正常流程即可:
- New--->Init()---->Start()---->Stop()--->Destory()
從生命周期探索連接器
根據(jù)上面的生命周期說明,我們可以知道連接器(Connector)就是按照如此的聲明周期管理的,所以我們找到了線索,所以連接器肯定會(huì)先初始化然后再啟動(dòng)。我們查看其initInternal()方法可以知道連接器初始化做了什么事情,源碼如下:
- @Override
- protected void initInternal() throws LifecycleException {
- super.initInternal();
- if (protocolHandler == null) {
- throw new LifecycleException(
- sm.getString("coyoteConnector.protocolHandlerInstantiationFailed"));
- }
- // Initialize adapter
- adapter = new CoyoteAdapter(this);
- protocolHandler.setAdapter(adapter);
- if (service != null) {
- protocolHandler.setUtilityExecutor(service.getServer().getUtilityExecutor());
- }
- // Make sure parseBodyMethodsSet has a default
- if (null == parseBodyMethodsSet) {
- setParseBodyMethods(getParseBodyMethods());
- }
- if (protocolHandler.isAprRequired() && !AprLifecycleListener.isInstanceCreated()) {
- throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprListener",
- getProtocolHandlerClassName()));
- }
- if (protocolHandler.isAprRequired() && !AprLifecycleListener.isAprAvailable()) {
- throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprLibrary",
- getProtocolHandlerClassName()));
- }
- if (AprLifecycleListener.isAprAvailable() && AprLifecycleListener.getUSEOpenssl() &&
- protocolHandler instanceof AbstractHttp11JsseProtocol) {
- AbstractHttp11JsseProtocol<?> jsseProtocolHandler =
- (AbstractHttp11JsseProtocol<?>) protocolHandler;
- if (jsseProtocolHandler.issslEnabled() &&
- jsseProtocolHandler.getsslImplementationName() == null) {
- // Openssl is compatible with the JSSE configuration, so use it if APR is available
- jsseProtocolHandler.setsslImplementationName(OpensslImplementation.class.getName());
- }
- }
- try {
- protocolHandler.init();
- } catch (Exception e) {
- throw new LifecycleException(
- sm.getString("coyoteConnector.protocolHandlerInitializationFailed"), e);
- }
- }
- }
根據(jù)上面源碼,我們發(fā)現(xiàn)主要是處理protocolHandler并初始化它,同時(shí)我們注意到了protocolHandler 設(shè)置了一個(gè)適配器,我們看看這個(gè)適配器是做啥的,跟蹤源碼如下:
- /**
- * The adapter, used to call the connector.
- *
- *&nbs
網(wǎng)頁題目:查漏補(bǔ)缺:連接器在Tomcat中是如何設(shè)計(jì)的
瀏覽路徑:http://jinyejixie.com/news44/102494.html成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、App設(shè)計(jì)、定制網(wǎng)站、網(wǎng)站建設(shè)、微信小程序、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容