2021-02-23 分类: 网站建设
从连接器(Connector)源码说起
既然是来解析连接器(Connector),那么我们直接从源码入手,后面所有源码我会剔除不重要部分,所以会忽略大部分源码细节,只关注流程。源码如下(高能预警,大量代码):
- 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的构造方法,其实只做了一件事情,就是根据协议设置对应的ProtocolHandler,根据名称我们知道,这是协议处理类,所以连接器内部的一个重要子模块就是ProtocolHandler。
关于生命周期
我们看到Connector继承了LifecycleMBeanBase,我们来看看Connector的最终继承关系:
我们看到最终实现的是Lifecycle接口,我们看看这个接口是何方神圣。我把其接口的注释拿下来解释下
这段注释翻译就是,这个接口是提供给组件声明周期管理的,并且提供了声明周期流转图。这里我们只需要知道正常流程即可:
- New--->Init()---->Start()---->Stop()--->Destory()
从生命周期探索连接器
根据上面的生命周期说明,我们可以知道连接器(Connector)就是按照如此的声明周期管理的,所以我们找到了线索,所以连接器肯定会先初始化然后再启动。我们查看其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);
- }
- }
- }
根据上面源码,我们发现主要是处理protocolHandler并初始化它,同时我们注意到了protocolHandler 设置了一个适配器,我们看看这个适配器是做啥的,跟踪源码如下:
- /**
- * The adapter, used to call the connector.
- *
- *&nbs
网页题目:查漏补缺:连接器在Tomcat中是如何设计的
浏览路径:https://www.cdcxhl.com/news44/102494.html成都网站建设公司_创新互联,为您提供搜索引擎优化、App设计、定制网站、网站建设、微信小程序、域名注册
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联
猜你还喜欢下面的内容