| 
                        副标题[/!--empirenews.page--]
                         从连接器(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接口,我们看看这个接口是何方神圣。我把其接口的注释拿下来解释下 
- /** 
 -  * Common interface for component life cycle methods. Catalina components 
 -  * may implement this interface (as well as the appropriate interface(s) for 
 -  * the functionality they support) in order to provide a consistent mechanism 
 -  * to start and stop the component. 
 -  * start() 
 -  * ----------------------------- 
 -  * | | 
 -  * | init() | 
 -  * NEW -»-- INITIALIZING | 
 -  * | | | | ------------------«----------------------- 
 -  * | | |auto | | | 
 -  * | | |/ start() |/ |/ auto auto stop() | 
 -  * | | INITIALIZED --»-- STARTING_PREP --»- STARTING --»- STARTED --»--- | 
 -  * | | | | | 
 -  * | |destroy()| | | 
 -  * | --»-----«-- ------------------------«-------------------------------- ^ 
 -  * | | | | 
 -  * | | |/ auto auto start() | 
 -  * | | STOPPING_PREP ----»---- STOPPING ------»----- STOPPED -----»----- 
 -  * | |/ ^ | ^ 
 -  * | | stop() | | | 
 -  * | | -------------------------- | | 
 -  * | | | | | 
 -  * | | | destroy() destroy() | | 
 -  * | | FAILED ----»------ DESTROYING ---«----------------- | 
 -  * | | ^ | | 
 -  * | | destroy() | |auto | 
 -  * | --------»----------------- |/ | 
 -  * | DESTROYED | 
 -  * | | 
 -  * | stop() | 
 -  * ----»-----------------------------»------------------------------ 
 -  * 
 -  * Any state can transition to FAILED. 
 -  * 
 -  * Calling start() while a component is in states STARTING_PREP, STARTING or 
 -  * STARTED has no effect. 
 -  * 
 -  * Calling start() while a component is in state NEW will cause init() to be 
 -  * called immediately after the start() method is entered. 
 -  * 
 -  * Calling stop() while a component is in states STOPPING_PREP, STOPPING or 
 -  * STOPPED has no effect. 
 -  * 
 -  * Calling stop() while a component is in state NEW transitions the component 
 -  * to STOPPED. This is typically encountered when a component fails to start and 
 -  * does not start all its sub-components. When the component is stopped, it will 
 -  * try to stop all sub-components - even those it didn't start. 
 -  * 
 -  * Attempting any other transition will throw {@link LifecycleException}. 
 -  * 
 -  * </pre> 
 -  * The {@link LifecycleEvent}s fired during state changes are defined in the 
 -  * methods that trigger the changed. No {@link LifecycleEvent}s are fired if the 
 -  * attempted transition is not valid. 
 
  
                                                (编辑:滁州站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |