在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; }
所有的加载工作已经完成了。接下来要启动这些组件了。
时间: 2024-10-10 21:06:55