在jetty

时间:2015-04-28 21:31:16

标签: jetty

我有两个可以单独部署或在同一个jetty服务器中部署的webapp。在我的情况下,一个webapp必须在另一个webapp完成其部署之前部署,所以我正在寻找双部署情况下的解决方案,我可以在其中定义部署顺序。

我已经使用以下内容定义了一个上下文xml文件:

<?xml version="1.0"  encoding="ISO-8859-1"?>
 <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure.dtd">
  <Configure class="org.eclipse.jetty.server.handler.HandlerList">
    <New class="org.eclipse.jetty.server.handler.HandlerList">
      <Set name="handlers">
        <Array type="org.eclipse.jetty.server.Handler">
          <Item>
            <New class="org.eclipse.jetty.webapp.WebAppContext">
             <Set name="contextPath">/security</Set>
             <Set name="war"><SystemProperty name="jetty.home"/>/security_app/war/</Set>
             <Call name="addAliasCheck">
               <Arg>
                <New class="org.eclipse.jetty.server.handler.ContextHandler$ApprovePathPrefixAliases"/>
               </Arg>
             </Call>
           </New>
         </Item>
         <Item>
           <New class="org.eclipse.jetty.webapp.WebAppContext">
             <Set name="contextPath">/</Set>
             <Set name="war"><SystemProperty name="jetty.home"/>/main_app/war/</Set>
             <Call name="addAliasCheck">
               <Arg>
                 <New class="org.eclipse.jetty.server.handler.ContextHandler$ApprovePathPrefixAliases"/>
               </Arg>
             </Call>
           </New>
         </Item>
       </Array>
     </Set>
   </New>
 </Configure>

但是,使用此解决方案,我收到以下错误:

java.lang.ClassCastException: org.eclipse.jetty.server.handler.HandlerList cannot be cast to org.eclipse.jetty.server.handler.ContextHandler
at org.eclipse.jetty.deploy.providers.WebAppProvider.createContextHandler(WebAppProvider.java:292)
at org.eclipse.jetty.deploy.App.getContextHandler(App.java:101)
at org.eclipse.jetty.deploy.bindings.StandardDeployer.processBinding(StandardDeployer.java:36)
at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186)
at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:498)
at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:146)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:605)
at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:528)
at org.eclipse.jetty.util.Scanner.scan(Scanner.java:391)
at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:313)

当然,这是真的,尽管它们最终都是AbstractHandlers。我可能做错了什么,还是有另一种方法可以解决这个问题?

使用Jetty 9.2.1

由于

1 个答案:

答案 0 :(得分:0)

您有硬编码部署,请勿使用deploy模块。

您获得的异常是deploy模块尝试自行查找和部署Web应用程序,并且无法找到它所期望的org.eclipse.jetty.server.handler.ContextHandler条目。

解决此问题的最佳方法是在部署/初始化期间没有单独依赖于彼此的Web应用程序(这超出了servlet规范的范围)。按需创建/main_app/war/ webapp init,而不是在启动时。

现在回到你的具体问题......

  

重要提示:请勿直接使用/编辑/运行/配置${jetty.home}目录。

     

${jetty.base}目录存在是有原因的,使用它,当你这样做时会更快乐。

     

http://www.eclipse.org/jetty/documentation/current/startup.html

对于那些对加载顺序敏感的内容,请不要将deploy模块与这些工件一起使用。 (将deploy模块用于您没有加载顺序依赖项的其他Web应用程序是安全的。

为实现此目的,我们将配置${jetty.base}目录以在启动时使用自定义xml文件,并为这些依赖于加载顺序的/special/配置WebAppContexts目录,确保这些上下文已加载,并由deploy模块管理。

$ cd my-jetty-base
$ mkdir etc
$ gvim etc/special-deploy.xml
$ echo "etc/special-deploy.xml" >> start.ini
$ ls -F
etc/  special/  start.ini  webapps
$ ls -F special/ 
main.war  security.war
$ cat start.ini
--module=http
jetty.port=8080
--module=deploy
etc/special-deploy.xml

special-deploy.xml看起来像这样......

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- =============================================================== -->
<!-- Add Load Order Dependant Webapps                                -->
<!-- =============================================================== -->
<Configure id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection">

  <Call name="addHandler">
    <Arg>
      <New class="org.eclipse.jetty.webapp.WebAppContext">
       <Set name="contextPath">/security</Set>
       <Set name="war"><SystemProperty name="jetty.base"/>/special/security.war</Set>
       <Call name="addAliasCheck">
         <Arg>
          <New class="org.eclipse.jetty.server.handler.ContextHandler$ApprovePathPrefixAliases"/>
         </Arg>
       </Call>
     </New>
    </Arg>
  </Call>

  <Call name="addHandler">
    <Arg>
      <New class="org.eclipse.jetty.webapp.WebAppContext">
       <Set name="contextPath">/</Set>
       <Set name="war"><SystemProperty name="jetty.base"/>/special/main.war</Set>
       <Call name="addAliasCheck">
         <Arg>
          <New class="org.eclipse.jetty.server.handler.ContextHandler$ApprovePathPrefixAliases"/>
         </Arg>
       </Call>
     </New>
    </Arg>
  </Call>

</Configure>

当你跑步时,你会看到以下内容......

$ java -jar /path/to/jetty-distribution-9.2.10.v20150310/start.jar 
2015-04-29 12:18:55.929:INFO::main: Logging initialized @275ms
2015-04-29 12:18:56.093:WARN:oejsh.ContextHandler:main: ApprovePathPrefixAliases is not safe for production
2015-04-29 12:18:56.094:WARN:oejsh.ContextHandler:main: ApprovePathPrefixAliases is not safe for production
2015-04-29 12:18:56.097:INFO:oejs.Server:main: jetty-9.2.10.v20150310
2015-04-29 12:18:56.150:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /security, did not find org.eclipse.jetty.jsp.JettyJspServlet
2015-04-29 12:18:56.166:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@751d30f6{/security,file:/tmp/jetty-0.0.0.0-8080-security.war-_security-any-3863723758166154575.dir/webapp/,AVAILABLE}{/home/joakim/examples/load-order-example/special/security.war}
2015-04-29 12:18:56.181:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2015-04-29 12:18:56.183:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@4f79a28b{/,file:/tmp/jetty-0.0.0.0-8080-main.war-_-any-2704851460101920295.dir/webapp/,AVAILABLE}{/home/joakim/examples/load-order-example/special/main.war}
2015-04-29 12:18:56.184:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/home/joakim/examples/load-order-example/webapps/] at interval 1
2015-04-29 12:18:56.195:INFO:oejs.ServerConnector:main: Started ServerConnector@4ef6d773{HTTP/1.1}{0.0.0.0:8080}
2015-04-29 12:18:56.196:INFO:oejs.Server:main: Started @542ms
^C2015-04-29 12:19:09.594:INFO:oejs.ServerConnector:Thread-0: Stopped ServerConnector@4ef6d773{HTTP/1.1}{0.0.0.0:8080}
2015-04-29 12:19:09.599:INFO:oejsh.ContextHandler:Thread-0: Stopped o.e.j.w.WebAppContext@4f79a28b{/,file:/tmp/jetty-0.0.0.0-8080-main.war-_-any-2704851460101920295.dir/webapp/,UNAVAILABLE}{/home/joakim/examples/load-order-example/special/main.war}
2015-04-29 12:19:09.602:INFO:oejsh.ContextHandler:Thread-0: Stopped o.e.j.w.WebAppContext@751d30f6{/security,file:/tmp/jetty-0.0.0.0-8080-security.war-_security-any-3863723758166154575.dir/webapp/,UNAVAILABLE}{/home/joakim/examples/load-order-example/special/security.war}