WebAppContexts的部署处理 - jetty

半兽人 发表于: 2016-03-23   最后更新时间: 2016-03-25 18:11:43  
{{totalSubscript}} 订阅, 9,960 游览

WebAppContexts 的部署处理


进入服务前,web应用程序需要一定的处理:可能需要解压,为jar创建一个特殊的类加载器,web.xml和web-fragment.xml描述符处理,扫描类注解以及很多其他的东西,随着web应用程序变得更加复杂,我们增加方法来帮助你扩大或减少在部署时所做的处理量,在本节中,我们将看看这个处理,以及如何调节它。


相反,如果你正在寻找如何配置特定的WebAppContext - 如它的上下文路径配置,是否应该解压或不解压。那你应该看下一节。

类配置

web应用部署,关于一系列的org.eclipse.jetty.webapp.Configuration类都适用,每一个执行一个特定的功能,这些配置的顺序具有重要意义,后面的配置倾向于构建在信息提取或安装在上述配置, 这些默认的列表,命令,按顺序的应用到每 org.eclipse.jetty.webapp.WebAppContext;


表4.1。默认配置类

org.eclipse.jetty.webapp.WebInfConfiguration 提取 war, jars 和 定义的类路径
org.eclipse.jetty.webapp.WebXmlConfiguration 处理 WEB-INF/web.xml 文件
org.eclipse.jetty.webapp.MetaInfConfiguration 寻找容器和web应用中的jar,为 META-INF/resources 和 META-INF/web-fragment.xml
org.eclipse.jetty.webapp.FragmentConfiguration 处理所有找到的 META-INF/web-fragment.xml 文件
org.eclipse.jetty.webapp.JettyWebXmlConfiguration 处理 WEB-INF/jetty-web.xml 文件


解剖配置类

WebAppContext的5种不同的阶段的生命周期,
preConfigure

        作为WebAppContext启动前执行的这个阶段,发现任何在随后的阶段中将需要用到的资源。
configure
        这个阶段就是类的工作,通常使用的资源在preConfigure阶段发现的。
postConfigure
        这一阶段允许配置明确下创建的任何资源,可能是在前面的2阶段不需要的。
deconfigure

        这个阶段每当发生WebAppContext被停止,并允许撤销任何创建的资源/元数据。WebAppContext应该能够不占用资源的干净的启动/停止多次。
destroy
当WebAppContext实际上从服务中移除了。例如,与之关联的war文件从$JETTY_HOME/webapps目录中删除。


按顺序列出的每个阶段,例如,用我们默认的配置类作为例子,preConfigure()将调用WebInfConfiguration,WebXmlConfiguration, MetaInfConfiguration,FragmentConfiguration和JettyWebXmlConfiguration.  循环重新开始configure()和再一次postConfigure(),在相反的顺序循环重复的deconfigure()和最后的destroy()阶段;


通过创建额外配置扩展容器支持

正如我们所看到的,有一个支持web应用的基础部署配置的默认设置,你会发现,我们还没提到JavaEE的功能,如JNDI,也没有高级servlet规范功能,如注解。这是因为jetty的理念是允许用户根据自己的需要调整容器,如果你不需要这些功能,你可以不使用它们,一个重要的考虑因素是因为例如 注解需要大量且费时的去扫描WEB-INF/lib的jar,作为有思想的web应用可能具有这些jar的分值,也可以通过有效的部署延迟,我们将在其他单元看到另一个 jetty为减少花费分析jar的webapp,


jetty利用了配置的灵活性,使JNDI和注释支持可插拔。


首先,让我们看看如何配置帮助启用 JNDI。

JNDI查找在web应用程序中需要容器到容器的环境的web应用程序中定义的连接资源。为实现这一目标,我们使用 2 额外配置 ︰

表4.2。 JNDI配置类

org.eclipse.jetty.plus.webapp.EnvConfiguration 为 webapp,创建 java:comp/env, 适用于 WEB-INF/jetty-env.xml 文件
org.eclipse.jetty.plus.webapp.PlusConfiguration  JNDI进程相关的 WEB-INF/web.xml 和 钩子命名条目

这些配置必须严格上面的顺序添加。并应紧接着之前的配置列表中org.eclipse.jetty.webapp.JettyWebXmlConfiguration类插入。为了充分支持JNDI,你需要做一些其他的事情,你可以在这里找到其中的全部细节。

如何设置配置列表

你有很多的选项,如何使Jetty使用不同的配置列表。

直接在WebAppContext设置列表

如果你只有一个web应用,你想要影响,这可能是最简单的选择,但是,要么需要一个你自己的web应用程序上下文的xml,或者你需要在代码中调用相应的。让我们看看如何添加JNDI和注释的配置的示例。

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/my-cool-webapp</Set>
 
  <Set name="configurationClasses">
    <Array type="java.lang.String">
      <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
      <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
      <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
      <Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
    </Array>
  </Set>
 
</Configure>
当然,你也可以用这种方法来减少应用于特定WebAppContext配置。


设置列表部署所有的webapps


你可以设置配置类的列表上的WebAppProvider。这样就将应用于每个部署人员部署的 WebAppContext:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_0.dtd">
 
<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 refid="Contexts" />
        </Set>
        <Call id="webappprovider" name="addAppProvider">
          <Arg>
            <New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
              <Set name="monitoredDirName"><Property name="jetty.home" default="." />/webapps</Set>
              <Set name="configurationClasses">
                <Array type="java.lang.String">
                  <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
                  <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
                  <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
                  <Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item>
                  <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
                </Array>
              </Set>
            </New>
          </Arg>
        </Call>
      </New>
    </Arg>
  </Call>
</Configure>
不用列举完整的列表,您可以简单地指定您想要添加的类,并指定你希望它们插入列表中的位置。让我们看看用这种方法在JNDI配置支持添加的一个列子 - 像以前一样,你可以在xml文件或通过代码。此示例使用XML文件,实际上它是$JETTY_HOME/etc/jetty-plus.xm。
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure id="Server" class="org.eclipse.jetty.server.Server">
 
  <!-- =========================================================== -->
  <!-- Add plus Configuring classes to all webapps for this Server -->
  <!-- =========================================================== -->
  <Call class="org.eclipse.jetty.webapp.Configuration$ClassList" name="setServerDefault">
    <Arg><Ref refid="Server" /></Arg>
    <Call name="addAfter">
      <Arg name="afterClass">org.eclipse.jetty.webapp.FragmentConfiguration</Arg>
      <Arg>
        <Array type="String">
          <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
        </Array>
      </Arg>
    </Call>
  </Call>
 
</Configure>
org.eclipse.jetty.webapp.Configuration.ClassList类提供这些方法插入:

addAfter
        在给定的配置类名称后插入提供的配置类名称的列表。
addBefore
        在给定配置类名称之前插入配置类名称提供的列表。


其他配置

org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern

这是一个上下文属性,可以设置在org.eclipse.jetty.webapp.WebAppContext去控制容器的类路径的哪些部分应该处理注释、META-INF/resources, META-INF/web-fragment.xml和META-INF里的tld。

通常情况下,容器类路径没有包括处理。不过,有时你需要包括一些,例如,你可能需要共享一些libraries,因此你把它们放进$JETTY_HOME/lib目录中。libraries包含注解,因此必须扫描。

下面是一个例子:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
    <Call name="setAttribute">
      <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
      <Arg>.*/foo-[^/]*\.jar$|.*/bar-[^/]*\.jar$|.*/classes/.*</Arg>
    </Call>
 
</Configure>

注意:模式的顺序限定了jar或类目录的扫描顺序。


org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern

类似于前面的上下文属性,该属性控制处理jar,像注释、META-INF/resources, META-INF/web-fragment.xml 和META-INF数据的tld。不管怎样,从web应用的类路径的这个属性控制(通常是WEB-INF/lib)进行处理,当你有几个jar包的时候,但是你只需要扫描一些jar包,这就特别有用了。

例子:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_0.dtd">
 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
    <Call name="setAttribute">
      <Arg>org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern</Arg>
      <Arg>.*/spring-[^/]*\.jar$</Arg>
    </Call>
 
</Configure>
注意:模式的顺序限定了jar或类目录的扫描顺序。
更新于 2016-03-25

查看jetty更多相关的文章或提一个关于jetty的问题,也可以与我们一起分享文章