在Catalina的load方法中,首先初始化Server组件。

  // Start the new server
if (server instanceof Lifecycle) {
try {
server.initialize();
} catch (LifecycleException e) {
log.error("Catalina.start", e);
}
}

在StandardServer中初始化service

    public void initialize()
throws LifecycleException
{
if (initialized) {
log.info(sm.getString("standardServer.initialize.initialized"));
return;
}
lifecycle.fireLifecycleEvent(INIT_EVENT, null);
initialized = true; if( oname==null ) {
try {
oname=new ObjectName( "Catalina:type=Server");
Registry.getRegistry(null, null)
.registerComponent(this, oname, null );
} catch (Exception e) {
log.error("Error registering ",e);
}
} // Register global String cache
try {
ObjectName oname2 =
new ObjectName(oname.getDomain() + ":type=StringCache");
Registry.getRegistry(null, null)
.registerComponent(new StringCache(), oname2, null );
} catch (Exception e) {
log.error("Error registering ",e);
} // Initialize our defined Services
for (int i = 0; i < services.length; i++) {
services[i].initialize();
}
}

在StandardService又初始化Connector

    public void initialize()
throws LifecycleException
{
// Service shouldn't be used with embeded, so it doesn't matter
if (initialized) {
if(log.isInfoEnabled())
log.info(sm.getString("standardService.initialize.initialized"));
return;
}
initialized = true; if( oname==null ) {
try {
// Hack - Server should be deprecated...
Container engine=this.getContainer();
domain=engine.getName();
oname=new ObjectName(domain + ":type=Service,serviceName="+name);
this.controller=oname;
Registry.getRegistry(null, null)
.registerComponent(this, oname, null); Executor[] executors = findExecutors();
for (int i = 0; i < executors.length; i++) {
ObjectName executorObjectName =
new ObjectName(domain + ":type=Executor,name=" + executors[i].getName());
Registry.getRegistry(null, null)
.registerComponent(executors[i], executorObjectName, null);
} } catch (Exception e) {
log.error(sm.getString("standardService.register.failed",domain),e);
} }
if( server==null ) {
// Register with the server
// HACK: ServerFactory should be removed... ServerFactory.getServer().addService(this);
} // Initialize our defined Connectors
synchronized (connectors) {
for (int i = 0; i < connectors.length; i++) {
connectors[i].initialize();
}
}
}

我们从Connector的初始化方法开始看:

public void initialize()
throws LifecycleException
{
if (initialized) {
if(log.isInfoEnabled())
log.info(sm.getString("coyoteConnector.alreadyInitialized"));
return;
} this.initialized = true; if( oname == null && (container instanceof StandardEngine)) {
try {
// we are loaded directly, via API - and no name was given to us
StandardEngine cb=(StandardEngine)container;
oname = createObjectName(cb.getName(), "Connector");
Registry.getRegistry(null, null)
.registerComponent(this, oname, null);
controller=oname;
} catch (Exception e) {
log.error( "Error registering connector ", e);
}
if(log.isDebugEnabled())
log.debug("Creating name for connector " + oname);
} // Initializa adapter
adapter = new CoyoteAdapter(this);//创建一个适配器,该适配器会完成请求的真正处理
protocolHandler.setAdapter(adapter); IntrospectionUtils.setProperty(protocolHandler, "jkHome",
System.getProperty("catalina.base")); try {
protocolHandler.init();//对于不同的协议,会有不同的ProtocolHandler实现类,我们来看Http11Protocol,它用来处理HTTP请求
} catch (Exception e) {
throw new LifecycleException
(sm.getString
("coyoteConnector.protocolHandlerInitializationFailed", e));
}
}

Http11Protocol的init()的核心代码

 public void init() throws Exception {
endpoint.setName(getName());
endpoint.setHandler(cHandler);
...
try {
endpoint.init();
} catch (Exception ex) {
log.error(sm.getString("http11protocol.endpoint.initerror"), ex);
throw ex;
}
}

endpoint的实现是JIoEndPoint

public void init()
throws Exception { if (initialized)
return; // Initialize thread count defaults for acceptor
//默认处理线程数
if (acceptorThreadCount == 0) {
acceptorThreadCount = 1;
}
//得到服务端默认Socket即DefaultServerSocketFactory
if (serverSocketFactory == null) {
serverSocketFactory = ServerSocketFactory.getDefault();
}
//创建服务端Socket,为客户端请求做准备
if (serverSocket == null) {
try {
if (address == null) {
serverSocket = serverSocketFactory.createSocket(port, backlog);
} else {
serverSocket = serverSocketFactory.createSocket(port, backlog, address);
}
} catch (BindException be) {
if (address == null)
throw new BindException(be.getMessage() + "<null>:" + port);
else
throw new BindException(be.getMessage() + " " +
address.toString() + ":" + port);
}
}
//if( serverTimeout >= 0 )
// serverSocket.setSoTimeout( serverTimeout ); initialized = true; }

所有的加载工作已经完成了。接下来要启动这些组件了。

最新文章

  1. ASP.NET corrupt assembly “Could not load file or assembly App_Web_*
  2. -Three.js开发指南---用three.js创建你的第一个三维场景(第一章)
  3. 分布式流式处理框架:storm简介 + Storm术语解释
  4. UVA 590 二十一 Always on the run
  5. input 的 placeholder属性在IE8下的兼容处理
  6. Linux下彻底删除oracle步骤【转】
  7. Oracle EBS-SQL (WIP-1):检查非标任务没挂需求.sql
  8. SZU:A12 Jumping up and down
  9. Linux学习之文件系统权限及表示
  10. C++ 初始化函数
  11. PHP中让json_encode不自动转义斜杠“/”的方法
  12. 【代码笔记】Web-HTML-布局
  13. (stringstream toupper 空格) 词组缩写 hdu2564
  14. MVC模式的原理
  15. Java虚拟机1
  16. Python字符串与容器
  17. MPAndroidChart的具体属性方法
  18. PHP学习必读的20本书
  19. Scrum立会报告+燃尽图(Final阶段第一次)
  20. TI XDC工具入门简介

热门文章

  1. Tomcat7性能优化
  2. jmeter命令行运行-分布式测试
  3. 笔记本光驱位安装固态硬盘及window系统一些过程记录
  4. VB ListBox 添加横向滚动条
  5. yahoo给出的关于网站优化的建议
  6. WPF 限制Textbox输入的内容
  7. .net导出不规则Excel
  8. 关于css3的fixed布局
  9. MVC视图路径修改方法
  10. hadoop MapReduce - 从作业、任务(task)、管理员角度调优