Jetty 6.1.5的配置

       Jetty以其高效、小巧、可嵌入式等优点深得人心,让我一看就喜欢上了。尤其是启动速度Tomcat 6简直望尘莫及,于是卸载了Tomcat安装了Jetty。大家有兴趣也可以比较一下。
       现在Jetty最新版本是6.1.5,目前还没有可用的Eclipse插件,但我们可以自己搞定。
       打开Eclipse在Run中新建一个Java Application,Project就用自己的web项目,MainClass填上org.mortbay.xml.XmlConfiguration,然后打开Arguments,在Program arguments中填上etc/jetty.xml当然这个配置文件自己可以改的,具体可以看下文介绍。现在已经配置好了,只需要运行/调试这个Application就可以相应的运行/调试自己的web项目。
       打开jetty安装目录下的etc/jetty.xml文件,会发现这个配置文件有点似曾相识。最外层节点是Configure,它定义了一个Server对象,这就是我们的Jetty服务器了。
      下面的代码定义了一个线程池,并将其提供给Server使用。
xml 代码
 
  1. <Set name="ThreadPool">  
  2.   <New class="org.mortbay.thread.BoundedThreadPool">  
  3.      <Set name="minThreads">10</Set>  
  4.      <Set name="maxThreads">250</Set>  
  5.      <Set name="lowThreads">25</Set>  
  6.   </New>  
  7. </Set>  

       <Set>标签用来设置属性,类似spring中的<property>标签。内嵌<New>标签初始化一个新实例,并将引用赋值给Server的threadPool属性。对比如下Spring代码:
xml 代码
 
  1. <property name="threadPool">    
  2.    <bean class="org.mortbay.thread.BoundedThreadPool">    
  3.     <property name="minThreads" value="10" />    
  4.     <property name="maxThreads" value="250" />    
  5.     <property name="lowThreads" value="25" />    
  6.   </bean>    
  7. </property>    
往下的代码有一个<Call  name="addConnector">标签,这是反射调用addConnector方法,<Arg>标签定义了方法使用的参数。<Array type="org.mortbay.jetty.Handler">标签定义了一个org.mortbay.jetty.Handler数组,<Item>标签则定义了数组中的每一项。<Ref>标签则引用一个已经定义的实例对象。很简单吧,这种IOC方式和spring异曲同工。
       下面来重点介绍一下有关Web项目的配置。
       <New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>这段代码定义了一个上下文处理器的集合和下文的
xml 代码
 
  1. <Call name="addLifeCycle">  
  2.   <Arg>  
  3.     <New class="org.mortbay.jetty.deployer.ContextDeployer">  
  4.       <Set name="contexts"><Ref id="Contexts"/></Set>  
  5.       <Set name="configurationDir"><SystemProperty name="jetty.home" default="."/>/contexts</Set>  
  6.       <Set name="scanInterval">1</Set>  
  7.     </New>  
  8.   </Arg>  
  9. </Call>  
配套使用,目的就是不断扫描$jetty.home/context目录下的xml配置文件,因此我们可以把自己的web应用的上下文配置放到这个目录下,就可以自动部署web应用。
       另外一段代码扫描$jetty.home//webapps下的目录和war文件,并自动部署之: 
xml 代码
 
  1. <Call name="addLifeCycle">  
  2.   <Arg>  
  3.     <New class="org.mortbay.jetty.deployer.WebAppDeployer">  
  4.       <Set name="contexts"><Ref id="Contexts"/></Set>  
  5.       <Set name="webAppDir"><SystemProperty name="jetty.home" default="."/>/webapps</Set>  
  6.       <Set name="parentLoaderPriority">false</Set>  
  7.       <Set name="extract">true</Set>  
  8.       <Set name="allowDuplicates">false</Set>  
  9.       <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>  
  10.     </New>  
  11.   </Arg>  
  12. </Call>  
看完上面的我们基本就可以写自己的配置文件了,由于采用Eclipse启动Jetty,所以/contexts和/webapps目录对我来说没有用,干掉。另外添加自己的Handler来处理web应用。我定义的启动wicket的jetty-wicket.xml代码如下:
xml 代码
 
  1. <Set name="handler">  
  2.   <New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">  
  3.     <Set name="handlers">  
  4.       <Array type="org.mortbay.jetty.Handler">  
  5.         <Item>  
  6.           <New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>  
  7.         </Item>  
  8.         <Item>  
  9.       <New class="org.mortbay.jetty.webapp.WebAppContext">  
  10.         <Set name="contextPath">/</Set>  
  11.         <Set name="resourceBase">d:/Workspace/wanged_wicket_app/web/</Set>  
  12.         <Call name="addServlet">  
  13.           <Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg>  
  14.           <Arg>/</Arg>  
  15.         </Call>  
  16.       </New>  
  17.     </Item>  
  18.       </Array>  
  19.     </Set>  
  20.   </New>  
  21. </Set>   
这样一来,启动时就不需要加载多余的web应用了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值