tomcat源码分析(二)启动过程
2015-02-11 10:48
686 查看
在Catalina的load方法中,首先初始化Server组件。
在StandardServer中初始化service
在StandardService又初始化Connector
我们从Connector的初始化方法开始看:
Http11Protocol的init()的核心代码
endpoint的实现是JIoEndPoint
所有的加载工作已经完成了。接下来要启动这些组件了。
// 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; }
所有的加载工作已经完成了。接下来要启动这些组件了。
相关文章推荐
- Tomcat源码分析 -- Tomcat的启动过程(二)
- Tomcat7源码分析——启动过程和类加载器
- Tomcat 源码分析(-)启动过程分析
- tomcat 4.1.30启动过程的源码分析
- tomcat 4.1.30启动过程的源码分析
- Tomcat源码分析--启动过程
- Tomcat源码分析 -- Tomcat的启动过程(一)
- Tomcat8源码分析3--Bootstrap启动过程
- tomcat 4.1.30启动过程的源码分析
- TOMCAT源码分析及启动过程
- TOMCAT源码分析(启动框架)
- 源码分析vxWorks6.x的ARM板BSP启动过程(下)
- Tomcat6源码启动分析
- 源码分析vxWorks6.x的ARM板BSP启动过程
- U-Boot的启动过程源码分析(只有思路)
- Tomcat 5.5.26源代码分析——启动过程(一)
- TOMCAT源码分析(启动框架)
- Nginx源码分析-启动初始化过程(一)
- TOMCAT源码分析(启动框架)(转)
- tomcat启动分析(源码框架)