jetty学习笔记-应用部署

部署方式

1. 静态部署:直接把包拷贝到webapp下面,然后启动jetty。当然,目录可以增加的。

 <Set name="handler">
      <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
         <Array type="org.eclipse.jetty.server.Handler">
           <Item>
             <New class="org.mortbay.jetty.webapp.WebAppContext">                  <Set name="contextPath">/test</Set>    ##指定位置                                       <Set name="war">demo</Set>             ##url的context                      </New>            </Item>
         </Array>
        </Set>
      </New>
    </Set>

2. hot deploy:通过context中的配置,指向工程,实现动态部署。当然,也是可以修改的。

   <Call name="addLifeCycle">  
      <Arg>  
        <New class="org.mortbay.jetty.deployer.ContextDeployer">  
          <Set name="contexts"><Ref id="Contexts"/></Set>  
          <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>##指定目录  
          <Set name="scanInterval">1</Set>  
        </New>  
      </Arg>  
    </Call>  

实现方式

jetty启动时加入--ini=--ini=start.ini,比如文件内容如下:

etc/jetty.xml
etc/jetty-deploy.xml
etc/jetty-webapps.xml
etc/jetty-contexts.xml
etc/jetty-testrealm.xml

在执行Server的时候会将内容加入args,然后读取定义的xml,其中和部署相关的几个

jetty-deploy.xml,增加DeploymentManager,handles the lifecycle of deploying web  applications

<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <Call name="addBean">
      <Arg>
        <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
          <Set name="contexts">
            <Ref id="Contexts" />
          </Set>
          <Call name="setContextAttribute">
            <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
            <Arg>.*/jsp-api-[^/]*\.jar$|.*/jsp-[^/]*\.jar{1}lt;/Arg>
          </Call>
        </New>
      </Arg>
    </Call>
</Configure>

jetty-contexts.xml,增加动态部署provider

<Configure id="Server" class="org.eclipse.jetty.server.Server">
        <Ref id="DeploymentManager">
          <Call name="addAppProvider">
            <Arg>
              <New class="org.eclipse.jetty.deploy.providers.ContextProvider">
                <Set name="monitoredDir"><Property name="jetty.home" default="." />/contexts</Set>
                <Set name="scanInterval">1</Set>
              </New>
            </Arg>
          </Call>
        </Ref>
</Configure>

jetty-webapps.xml,增加静态部署provider
<Configure id="Server" class="org.eclipse.jetty.server.Server">
    <Ref id="DeploymentManager">
          <Call name="addAppProvider">
            <Arg>
              <New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
                <Set name="monitoredDir"><Property name="jetty.home" default="." />/webapps</Set>
                <Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>
                <Set name="scanInterval">1</Set>
                <Set name="contextXmlDir"><Property name="jetty.home" default="." />/contexts</Set>
                <Set name="extractWars">false</Set>
              </New>
            </Arg>
          </Call>
    </Ref>
</Configure>

addBeans方法的作用,在server启动后调用DeploymentManager的生命周期方法

    /**
     * Add an associated bean.
     * The bean will be added to the servers {@link Container}
     * and if it is a {@link LifeCycle} instance, it will be 
     * started/stopped along with the Server. Any beans that are also 
     * {@link Destroyable}, will be destroyed with the server.
     * @param o the bean object to add
     */

DeploymentManager.doStart(),启动appProviders

    @Override
    protected void doStart() throws Exception
    {
        if (_useStandardBindings)
        {
            Log.debug("DeploymentManager using standard bindings");
            addLifeCycleBinding(new StandardDeployer());
            addLifeCycleBinding(new StandardStarter());
            addLifeCycleBinding(new StandardStopper());
            addLifeCycleBinding(new StandardUndeployer());
        }

        // Start all of the AppProviders
        for (AppProvider provider : _providers)
        {
            startAppProvider(provider);
        }
        super.doStart();
    }
 private void startAppProvider(AppProvider provider)
    {
        try
        {
            provider.setDeploymentManager(this);
            provider.start();
        }
        catch (Exception e)
        {
            Log.warn("Unable to start AppProvider",e);
        }
    }


Provider类图

scanner负责扫描,内部通过time和timertask完成调度

listener定义扫描之后的操作

filenamefilter定义寻找文件名的规则

jetty学习笔记-应用部署_第1张图片

静态部署

如果文件不是目录或者以.war结尾,不处理

if (!file.isDirectory() && !lowername.endsWith(".war"))
            {
                return false;
            }

如果目是目录下面有war,丢弃

// is it a directory for an existing war file?
            if (file.isDirectory() && 
                    (new File(dir,name+".war").exists() ||
                     new File(dir,name+".WAR").exists()))
            {
                return false;
            }

app在createContextHandler的时候直接createContextHandler

hot deploy

以xml结尾并且不是目录就可以了
            public boolean accept(File dir, String name)
            {
                if (!dir.exists())
                    return false;
                String lowername = name.toLowerCase();
                return  (lowername.endsWith(".xml") && !new File(dir,name).isDirectory());
            }
        }

app在创建contextHandler的时候调用XmlConfiguration来解析配置文件并初始化

你可能感兴趣的:(exception,bean,server,String,File,Class)