jetty eclipse 整合二

来源:互联网 发布:pc装机必备软件 编辑:程序博客网 时间:2024/06/06 01:30

JETTY嵌入式Web容器的开发(二)---部署外部WAR包

作为WEB容器,部署WAR包是基础功能.

通过JETTY直接在程序里部署,并启动服务,可以实现复杂的部署操作对使用者透明化.

但这种程序中的部署方式,会使JETTY启动速度变得非常慢(后台的解压和拷贝操作),因此部署WAR包方式不建议与程序调试工作在一起.

 

JETTY嵌入式开发步骤

  • 下载

同<JETTY嵌入式Web容器的开发(一)>,仍旧采用

jetty-hightide-8.1.4.v20120524.zip

 

  • ECLIPSE建立一个普通JAVA项目testjetty

建项目步骤同<JETTY嵌入式Web容器的开发(一)>,不做赘述

建成的项目的结构如下:

 JETTY嵌入式Web容器的开发(二)---部署外部WAR包

  • 修改/jetty/etc/jetty.xml

同<JETTY嵌入式Web容器的开发(一)>,不做赘述

 

  • 确定需要部署的WAR包的所在路径

我找了一个比较大的,集成了SPRING,STRUTS2,IBARTS的项目

 d:/ckxWeb.war

 JETTY嵌入式Web容器的开发(二)---部署外部WAR包

  • JETTY  Service类

Service类同<JETTY嵌入式Web容器的开发(一)>,这里只把对应部署功能的语句标红

package org.jetty.demo;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.server.handler.ContextHandlerCollection;

import org.eclipse.jetty.webapp.WebAppContext;

import org.eclipse.jetty.xml.XmlConfiguration;

import org.xml.sax.SAXException;

 

public class JettyCustomServer extends Server {

 

         private String xmlConfigPath;

        

         private String contextPath;

        

         private String warPath;

        

         private String resourceBase = "./webRoot";

        

         private String webXmlPath = "./webRoot/WEB-INF/web.xml";

        

 

 

        

         public JettyCustomServer(String xmlConfigPath,String contextPath,String resourceBase,String webXmlPath) {

                   this(xmlConfigPath,contextPath,resourceBase,webXmlPath,null);

         }

 

        

         public JettyCustomServer(String xmlConfigPath,String contextPath) {

                   this(xmlConfigPath,contextPath,null,null,null);

         }

        

        

         public JettyCustomServer(String xmlConfigPath,String contextPath,String warPath) {

                   this(xmlConfigPath,contextPath,null,null,warPath);

         }

        

        

         public JettyCustomServer(String xmlConfigPath,String contextPath,String resourceBase,String webXmlPath,String warPath) {

                   super();

                   if(StringUtils.isNotBlank(xmlConfigPath)){

                            this.xmlConfigPath = xmlConfigPath;

                            readXmlConfig();

                   }

                  

                   if(StringUtils.isNotBlank(warPath)){

                            this.warPath = warPath;

                            if(StringUtils.isNotBlank(contextPath)){

                                     this.contextPath = contextPath;

                                     applyHandle(true);

                            }

                   }else{

                            if(StringUtils.isNotBlank(resourceBase))

                                     this.resourceBase = resourceBase;

                            if(StringUtils.isNotBlank(webXmlPath))

                                     this.webXmlPath = webXmlPath;

                            if(StringUtils.isNotBlank(contextPath)){

                                     this.contextPath = contextPath;

                                     applyHandle(false);

                            }

                   }

                  

                  

         }

        

        

         private void readXmlConfig(){

        try {

                            XmlConfiguration configuration =  new XmlConfiguration(new FileInputStream(this.xmlConfigPath));

                            configuration.configure(this);

                   } catch (FileNotFoundException e1) {

                            e1.printStackTrace();

                   } catch (SAXException e1) {

                            e1.printStackTrace();

                   } catch (IOException e1) {

                            e1.printStackTrace();

                   } catch (Exception e) {

                            e.printStackTrace();

                   }

         }

        

        

         public void applyHandle(Boolean warDeployFlag){

 

        ContextHandlerCollection handler = new ContextHandlerCollection();  

               

        WebAppContext webapp = new WebAppContext();  

        webapp.setContextPath(contextPath);

        webapp.setDefaultsDescriptor("./jetty/etc/webdefault.xml");

       

        if(!warDeployFlag){

                 webapp.setResourceBase(resourceBase);

            webapp.setDescriptor(webXmlPath);            

        }else{

                 webapp.setWar(warPath);

        }

 

        handler.addHandler(webapp);  

       

        super.setHandler(handler);

         }

        

        

         public void startServer(){

        try {

                            super.start();

                            System.out.println("current thread:"+super.getThreadPool().getThreads()+"| idle thread:"+super.getThreadPool().getIdleThreads());

                 super.join();

                   } catch (Exception e) {

                            e.printStackTrace();

                   }

       

         }

 

         public String getXmlConfigPath() {

                   return xmlConfigPath;

         }

 

         public void setXmlConfigPath(String xmlConfigPath) {

                   this.xmlConfigPath = xmlConfigPath;

         }

 

         public String getContextPath() {

                   return contextPath;

         }

 

         public void setContextPath(String contextPath) {

                   this.contextPath = contextPath;

         }

 

         public String getResourceBase() {

                   return resourceBase;

         }

 

         public void setResourceBase(String resourceBase) {

                   this.resourceBase = resourceBase;

         }

 

         public String getWebXmlPath() {

                   return webXmlPath;

         }

 

         public void setWebXmlPath(String webXmlPath) {

                   this.webXmlPath = webXmlPath;

         }

 

         public String getWarPath() {

                   return warPath;

         }

 

         public void setWarPath(String warPath) {

                   this.warPath = warPath;

         }

 

        

}

 

 

  •  做一个简单的可执行的服务启动类

package org.jetty.demo;  

 

public class JettyServerStart {  

 

    public static void main(String[] args) {

    JettyCustomServer server = new JettyCustomServer("./jetty/etc/jetty.xml","/testContext","d:/ckxWeb.war");

        server.startServer();    

    }

 

 

  • 启动服务

执行JettyServerStart

2012-06-27 16:35:11.796:INFO:oejs.Server:jetty-8.1.4.v20120524

 

2012-06-27 16:35:36.281:INFO:oejw.WebInfConfiguration:Extract jar:file:/D:/ckxWeb.war!/ to C:\Documents and Settings\macg\Local Settings\Temp\jetty-0.0.0.0-8088-ckxWeb.war-_testContext-any-\webapp

系统停在这里足足有4分钟!全是后台解析WAR文件浪费的时间(从日志看,WAR包被展开到临时目录下),因此如果可能,还是建议采用目录部署的方式,WAR包部署启动太耗时间

2012-06-27 16:37:25.046:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/C:/Documents and Settings/macg/Local Settings/Temp/jetty-0.0.0.0-8088-ckxWeb.war-_testContext-any-/webapp/},d:/ckxWeb.war

2012-06-27 16:37:25.046:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/C:/Documents and Settings/macg/Local Settings/Temp/jetty-0.0.0.0-8088-ckxWeb.war-_testContext-any-/webapp/},d:/ckxWeb.war

2012-06-27 16:37:25.140:INFO:/testContext:Initializing Spring root WebApplicationContext

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).

log4j:WARN Please initialize the log4j system properly.

日志比想象中少,因为这个testjetty项目是个简单的例子,什么都不带,没有自己的log系统(不管部署多少项目,一个web容器只能有一个统一的日志系统),所以spring,struts的日志都没出来,但不影响系统运行和访问

 

2012-06-27 16:37:29,140 INFO MLog.<clinit>(80)-MLog clients using log4j logging.

2012-06-27 16:37:29,203 INFO C3P0Registry.banner(77)-Initializing c3p0-0.9.0 [built 11-July-2005 00:43:29 -0400; debug? true; trace: 10]

2012-06-27 16:37:30.281:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/C:/Documents and Settings/macg/Local Settings/Temp/jetty-0.0.0.0-8088-ckxWeb.war-_testContext-any-/webapp/},d:/ckxWeb.war

2012-06-27 16:37:30.281:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/C:/Documents and Settings/macg/Local Settings/Temp/jetty-0.0.0.0-8088-ckxWeb.war-_testContext-any-/webapp/},d:/ckxWeb.war

2012-06-27 16:37:30.281:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/C:/Documents and Settings/macg/Local Settings/Temp/jetty-0.0.0.0-8088-ckxWeb.war-_testContext-any-/webapp/},d:/ckxWeb.war

2012-06-27 16:37:32.484:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8088current thread:30| idle thread:26

 

 

  • 测试

测试访问项目页面

注意:

假设项目原来的访问的URL为: http://localhost:8080/XXX/admin/main.do,其中XXX是项目的CONTEXT NAME

被JETTY部署后,发生了CONTEXT转化,必须在URL里采用启动服务里设的CONTEXT名

http://localhost:8088/testContext/admin/main.do?

0 0