tomcat 源码分析之service
service类提供服务的,可以配置多个它的初始化方法 @Override protected void initInternal() throws LifecycleException { super.initInternal(); if (container != null) { container.init(); } // Initialize any Executors for (Executor executor : findExecutors()) { if (executor instanceof LifecycleMBeanBase) { ((LifecycleMBeanBase) executor).setDomain(getDomain()); } executor.init(); } // Initialize our defined Connectors synchronized (connectors) { for (Connector connector : connectors) { try { connector.init(); } catch (Exception e) { String message = sm.getString( "standardService.connector.initFailed", connector); log.error(message, e); if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) throw new LifecycleException(message); } } } }
从上面我们看到service包含多个Executor和connector,connection接受客户的请求然后交给executor处理
Executor的默认实现是StandardThreadExecutor
@Override protected void startInternal() throws LifecycleException { taskqueue = new TaskQueue(maxQueueSize); TaskThreadFactory tf = new TaskThreadFactory(namePrefix,daemon,getThreadPriority()); executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf); executor.setThreadRenewalDelay(threadRenewalDelay); if (prestartminSpareThreads) { executor.prestartAllCoreThreads(); } taskqueue.setParent(executor); setState(LifecycleState.STARTING); }
从上面我们看到其实是创建一个线程池由他负责处理请求。
下面是Connector的的构造方法
public Connector(String protocol) { setProtocol(protocol); // 初始化协议处理类默认是org.apache.coyote.http11.Http11Protocol try { Class<?> clazz = Class.forName(protocolHandlerClassName); this.protocolHandler = (ProtocolHandler) clazz.newInstance(); } catch (Exception e) { log.error(sm.getString( "coyoteConnector.protocolHandlerInstantiationFailed"), e); } } 下面是他的初始化方法 @Override protected void initInternal() throws LifecycleException { super.initInternal(); //请求响应对象从连接器传送到容器需要一个桥梁 adapter = new CoyoteAdapter(this); protocolHandler.setAdapter(adapter); if( null == parseBodyMethodsSet ) { setParseBodyMethods(getParseBodyMethods()); } if (protocolHandler.isAprRequired() && !AprLifecycleListener.isAprAvailable()) { throw new LifecycleException( sm.getString("coyoteConnector.protocolHandlerNoApr", getProtocolHandlerClassName())); } try { protocolHandler.init(); } catch (Exception e) { throw new LifecycleException (sm.getString ("coyoteConnector.protocolHandlerInitializationFailed"), e); } // Initialize mapper listener mapperListener.init(); }
相关推荐
Dullonjiang 2020-07-05
adayan0 2020-05-19
yanqianglifei 2020-03-03
Oeljeklaus 2020-02-18
Dullonjiang 2020-01-23
Notzuonotdied 2020-01-21
YarnSup 2020-01-10
hooopo 2014-07-12
zhixingheyitian 2019-12-06
yanqianglifei 2019-10-23
weeniebear 2015-10-16
finalcola 2015-07-25
农村外出务工男 2015-05-28
lzzyok 2019-06-29
purpen 2013-01-31
chenjieit 2019-06-28