tomcat学习(一)

初始tomcat

什么是tomcat

Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。–摘自百度百科

tomcat总览

image

  • Server是Tomcat最顶层的容器,代表着整个服务器
  • Service 逻辑业务组件
  • Connector使用ProtocolHandler来处理请求的
  • Engine:引擎、只有一个
    定义了一个名为Catalina的Engine
  • Host:站点、虚拟主机
    一个Engine包含多个Host的设计,使得一个服务器实例可以承担多个域名的服务,是很灵活的设计
  • Context:一个应用
    默认配置下webapps下的每个目录都是一个应用
  • Wrapper:一个Servlet

tomcat用到的设计模式和启动流程

tomcat主要用到了模板方法模式和责任链模式。tomcat的主要容器的启动都是用到了模板方法,模板方法模式,一般定义一个抽象类,抽象类里面包括模板方法和流程方法,流程方法留给子类继承,模板方法留给子类调用。tomcat的模板方法是lifecycleBase。

例如start,lifecycleBase包括start()启动的模板方法,start方法里面调用流程方法startInternal,startInternal就需要各个容器组件自己实现。

 /**
     * {@inheritDoc}
     */
    @Override
    public final synchronized void start() throws LifecycleException {
    	  log.info("进入生命周期管理的start()方法了");
    	//统一进行生命周期的管理(具体实现类在执行startInternal方法前)
        if (LifecycleState.STARTING_PREP.equals(state) || LifecycleState.STARTING.equals(state) ||
                LifecycleState.STARTED.equals(state)) {
            if (log.isDebugEnabled()) {
                Exception e = new LifecycleException();
                log.debug(sm.getString("lifecycleBase.alreadyStarted", toString()), e);
            } else if (log.isInfoEnabled()) {
                log.info(sm.getString("lifecycleBase.alreadyStarted", toString()));
            }

            return;
        }
        //检查各组件的生命状态,如果不符合或者有异常可以统一的处理
        if (state.equals(LifecycleState.NEW)) {
            init();
        } else if (state.equals(LifecycleState.FAILED)) {
            stop();
        } else if (!state.equals(LifecycleState.INITIALIZED) &&
                !state.equals(LifecycleState.STOPPED)) {
            invalidTransition(Lifecycle.BEFORE_START_EVENT);
        }

        try {
        	//设置组件状态
            setStateInternal(LifecycleState.STARTING_PREP, null, false);
            log.info("进入生命周期管理的start()方法了----事前处理");
            //调用具体实现的类的处理(这里是一个抽象方法)
            startInternal();
        	//统一进行生命周期的管理(具体实现类在执行startInternal方法后)
            log.info("进入生命周期管理的start()方法了----事后处理");
            if (state.equals(LifecycleState.FAILED)) {
                // 失败---组件放入了失败状态调用停止()以完成清理
                stop();
            } else if (!state.equals(LifecycleState.STARTING)) {
                // 不是已经在启动的状态
                invalidTransition(Lifecycle.AFTER_START_EVENT);
            } else {
            	//设置组件状态
                setStateInternal(LifecycleState.STARTED, null, false);
            }
        } catch (Throwable t) {
            // This is an 'uncontrolled' failure so put the component into the
            // FAILED state and throw an exception.
            ExceptionUtils.handleThrowable(t);
            setStateInternal(LifecycleState.FAILED, null, false);
            throw new LifecycleException(sm.getString("lifecycleBase.startFail", toString()), t);
        }
    }


    /**
     * Sub-classes must ensure that the state is changed to
     * {@link LifecycleState#STARTING} during the execution of this method.
     * Changing state will trigger the {@link Lifecycle#START_EVENT} event.
     *
     * If a component fails to start it may either throw a
     * {@link LifecycleException} which will cause it's parent to fail to start
     * or it can place itself in the error state in which case {@link #stop()}
     * will be called on the failed component but the parent component will
     * continue to start normally.
     *
     * @throws LifecycleException
     */
    protected abstract void startInternal() throws LifecycleException;
  • 容器的初始化流程
  • tomcat的启动流程
  • 一次请求处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值