设为首页 收藏本站
云服务器等爆品抢先购,低至4.2元/月
查看: 647|回复: 0

[经验分享] 新手配置 Jetty + Eclipse (Hot code debugging)

[复制链接]

尚未签到

发表于 2017-2-26 11:03:22 | 显示全部楼层 |阅读模式
在用Tomcat 开发Java Web project 时,在调试的时候,由于我们不断改动java 源文件,因而我们需要不断重新启动Tomcat 更新我们的项目,我们把很多时间浪费在了更新上,这样开发效率大大降低。

Jetty 完全支持Hot code debugging ,也就是说,我们在更改java源文件,更改html,jsp时不用再重新启动服务器了,这样节省了大量时间(注意web.xml 等其他配置文件的更新还是需要重新启动服务器的)。

好了现在开始教你如何配置Jetty 到Eclipse.

http://www.eclipse.org/jetty/downloads.php这个地址可以安装eclipse 对jetty支持所需要的东西,可以通过eclipse自动安装

一、安装插件
jetty 在eclipse的插件现在名字叫run jetty run (2012年6月7日)

地址:http://run-jetty-run.googlecode.com/svn/trunk/updatesite


安装方法就不说了。安完后,点击eclipse的run configuration,查看安装成功


二、jetty 配置(可省略直接跳到三)

要想让Jetty启动Web服务器,则要告诉Jetty一些参数,比如端口是多少,哪里才是Web的根目录等。
这个XML配置文件是通用的,对于一般的应用,要修改的个性部分很少,所以保存一份,然后其它的项目只要复制粘贴就行了。

和得到Jar包依赖一样,也有两种方式得到该配置文件。
第一种方法是直接在Jetty官网中下载,然后根据自己的实际情况改下就行了:http://docs.codehaus.org/display/JETTY/jetty.xml

第二种方式是复制以下内容,然后粘贴保存就行了:

[html] view plaincopy

    <?xml version="1.0"encoding="GBK"?>  
    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTDConfigure//EN" "http://jetty.mortbay.org/configure.dtd">  
    <Configure id="Server"class="org.mortbay.jetty.Server">  
    <Set name="ThreadPool">  
       <New class="org.mortbay.thread.BoundedThreadPool">  
        <Set name="minThreads">10</Set>  
        <Set name="maxThreads">250</Set>  
        <Set name="lowThreads">25</Set>  
       </New>  
    </Set>  
      
    <Property name="org.mortbay.util.URI.charset"default="GBK"/>  
    <Call name="addConnector">  
       <Arg>  
        <NewclassNewclass="org.mortbay.jetty.nio.SelectChannelConnector">  
         <Set name="port">  
         <SystemPropertynameSystemPropertyname="jetty.port" default="80" /><!--端口号 -->  
         </Set>  
         <SetnameSetname="maxIdleTime">30000</Set>  
         <Set name="Acceptors">2</Set>  
         <Set name="statsOn">false</Set>  
         <Set name="confidentialPort">8443</Set>  
         <SetnameSetname="lowResourcesConnections">5000</Set>  
         <SetnameSetname="lowResourcesMaxIdleTime">5000</Set>  
        </New>  
       </Arg>  
    </Call>  
    <Set name="sessionIdManager">  
       <NewclassNewclass="org.mortbay.jetty.servlet.HashSessionIdManager">  
        <Set name="workerName">node1</Set>  
       </New>  
    </Set>  
    <Set name="handler">  
       <New id="Handlers"class="org.mortbay.jetty.handler.HandlerCollection">  
        <Set name="handlers">  
         <ArraytypeArraytype="org.mortbay.jetty.Handler">  
          <Item>  
           <New id="Contexts"class="org.mortbay.jetty.handler.ContextHandlerCollection" />  
          </Item>  
          <Item>  
           <New id="DefaultHandler"class="org.mortbay.jetty.handler.DefaultHandler" />  
          </Item>  
          <Item>  
           <New id="RequestLog"class="org.mortbay.jetty.handler.RequestLogHandler" />  
          </Item>  
         </Array>  
        </Set>  
       </New>  
    </Set>  
    <Set name="handler">  
       <New id="Handlers"class="org.mortbay.jetty.handler.HandlerCollection">  
        <Set name="handlers">  
         <ArraytypeArraytype="org.mortbay.jetty.Handler">  
          <Item>  
           <NewclassNewclass="org.mortbay.jetty.webapp.WebAppContext">  
           <Set name="contextPath">/</Set><!-- ContextPath-->  
           <Set name="resourceBase">./WebRoot</Set><!-- Web应用根目录 -->  
            <CallnameCallname="addServlet">  
            <Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg>  
             <Arg>/</Arg>  
            </Call>  
            <!-- 增加其它的Servlet -->  
           </New>  
          </Item>  
         </Array>  
        </Set>  
       </New>  
    </Set>  
    <Set name="UserRealms">  
       <ArraytypeArraytype="org.mortbay.jetty.security.UserRealm"/>  
    </Set>  
    <Set name="stopAtShutdown">true</Set>  
    <Set name="sendServerVersion">true</Set>  
    <Set name="gracefulShutdown">1000</Set>  
    </Configure>  



该配置文件要修改的部分一般只有三处:端口号、上下文(ContextPath)和Web应用根目录。
三、启动jetty

目前发现的最好最快的直接在ECLIPSE中JETTY调试方式

适用于6.1.3以上,包括6.1.5的JETTY。

它主要是利用了JDK的代码自动更换性能(code hot replace),可以不用重启JETTY就调试、更换资源文件。注意:一定是DEBUG方式运行才有这项功能。

所以应该说这篇文章的方法更好:

在Run->Debug中,New一个Java Application的配置,填入:

org.mortbay.xml.XmlConfiguration

参数填入一个自己的JETTY配置文件:

完成的myjetty.xml配置文件,请将其中的相应目录修改成自己项目的目录:

[html] view plaincopy

    <?xml version="1.0"?>  
    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">  
      
    <!-- =============================================================== -->  
    <!-- Configure the Jetty Server                                      -->  
    <!--                                                                 -->  
    <!-- Documentation of this file format can be found at:              -->  
    <!-- http://docs.codehaus.org/display/JETTY/jetty.xml                -->  
    <!--                                                                 -->  
    <!-- =============================================================== -->  
      
      
    <Configure id="Server" class="org.mortbay.jetty.Server">  
      
        <!-- =========================================================== -->  
        <!-- Server Thread Pool                                          -->  
        <!-- =========================================================== -->  
        <Set name="ThreadPool">  
          <!-- Default bounded blocking threadpool  
          -->  
          <New class="org.mortbay.thread.BoundedThreadPool">  
            <Set name="minThreads">10</Set>  
            <Set name="maxThreads">250</Set>  
            <Set name="lowThreads">25</Set>  
          </New>  
      
          <!-- Optional Java 5 bounded threadpool with job queue   
          <New class="org.mortbay.thread.concurrent.ThreadPool">  
            <Set name="corePoolSize">250</Set>  
            <Set name="maximumPoolSize">250</Set>  
          </New>  
          -->  
        </Set>  
      
      
      
        <!-- =========================================================== -->  
        <!-- Set connectors                                              -->  
        <!-- =========================================================== -->  
        <!-- One of each type!                                           -->  
        <!-- =========================================================== -->  
      
        <!-- Use this connector for many frequently idle connections  
             and for threadless continuations.  
        -->      
        <Call name="addConnector">  
          <Arg>  
              <New class="org.mortbay.jetty.nio.SelectChannelConnector">  
                <Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>  
                <Set name="maxIdleTime">30000</Set>  
                <Set name="Acceptors">2</Set>  
                <Set name="statsOn">false</Set>  
                <Set name="confidentialPort">8443</Set>  
            <Set name="lowResourcesConnections">5000</Set>  
            <Set name="lowResourcesMaxIdleTime">5000</Set>  
              </New>  
          </Arg>  
        </Call>  
      
        <!-- Use this connector if NIO is not available.  
        <Call name="addConnector">  
          <Arg>  
              <New class="org.mortbay.jetty.bio.SocketConnector">  
                <Set name="port">8081</Set>  
                <Set name="maxIdleTime">50000</Set>  
                <Set name="lowResourceMaxIdleTime">1500</Set>  
              </New>  
          </Arg>  
        </Call>  
        -->  
      
        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->  
        <!-- To add a HTTPS SSL listener                                     -->  
        <!-- see jetty-ssl.xml to add an ssl connector. use                  -->  
        <!-- java -jar start.jar etc/jetty.xml etc/jetty-ssl.xml             -->  
        <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->  
         
        <!-- =========================================================== -->  
        <!-- Set up global session ID manager                            -->  
        <!-- =========================================================== -->  
        <!--  
        <Set name="sessionIdManager">  
          <New class="org.mortbay.jetty.servlet.HashSessionIdManager">  
            <Set name="workerName">node1</Set>  
          </New>  
        </Set>  
        -->  
      
        <!-- =========================================================== -->  
        <!-- Set handler Collection Structure                            -->   
        <!-- =========================================================== -->  
        <Set name="handler">  
          <New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">  
            <Set name="handlers">  
             <Array type="org.mortbay.jetty.Handler">  
               <Item>  
                 <New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>  
               </Item>  
               <Item>  
                 <New id="DefaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>  
               </Item>  
               <Item>  
                 <New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>  
               </Item>  
             </Array>  
            </Set>  
          </New>  
        </Set>  
         
    <Set name="handler">     
      <New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">     
        <Set name="handlers">     
          <Array type="org.mortbay.jetty.Handler">     
            <!--Item>     
              <New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>     
            </Item-->     
            <Item>     
              <New class="org.mortbay.jetty.webapp.WebAppContext">     
                <Set name="contextPath">/ebnms</Set>     
                <Set name="resourceBase">E:/Prj2/ForMe/Src/flower/src/main/webapp</Set>     
                <Call name="addServlet">     
                  <Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg>     
                  <Arg>/</Arg>     
                </Call>     
              </New>     
        </Item>     
          </Array>     
        </Set>     
      </New>     
    </Set>     
      
      
        <!-- =========================================================== -->  
        <!-- Configure Authentication Realms                             -->  
        <!-- Realms may be configured for the entire server here, or     -->  
        <!-- they can be configured for a specific web app in a context  -->  
        <!-- configuration (see $(jetty.home)/contexts/test.xml for an   -->  
        <!-- example).                                                   -->  
        <!-- =========================================================== -->  
        <Set name="UserRealms">  
          <Array type="org.mortbay.jetty.security.UserRealm">  
            <!--  
            <Item>  
              <New class="org.mortbay.jetty.security.HashUserRealm">  
                <Set name="name">Test Realm</Set>  
                <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>  
              </New>  
            </Item>  
        -->  
          </Array>  
        </Set>  
      
        <!-- =========================================================== -->  
        <!-- Configure Request Log                                       -->  
        <!-- Request logs  may be configured for the entire server here, -->  
        <!-- or they can be configured for a specific web app in a       -->  
        <!-- contexts configuration (see $(jetty.home)/contexts/test.xml -->  
        <!-- for an example).                                            -->  
        <!-- =========================================================== -->  
        <!--Ref id="RequestLog">  
          <Set name="requestLog">  
            <New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">  
              <Set name="filename"><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Set>  
              <Set name="filenameDateFormat">yyyy_MM_dd</Set>  
              <Set name="retainDays">90</Set>  
              <Set name="append">true</Set>  
              <Set name="extended">true</Set>  
              <Set name="logCookies">false</Set>  
              <Set name="LogTimeZone">GMT</Set>  
            </New>  
          </Set>  
        </Ref-->  
      
        <!-- =========================================================== -->  
        <!-- extra options                                               -->  
        <!-- =========================================================== -->  
        <Set name="stopAtShutdown">true</Set>  
        <Set name="sendServerVersion">true</Set>  
        <!--Set name="sendDateHeader">true</Set-->  
        <!--Set name="gracefulShutdown">1000</Set-->  
    </Configure>  



解决用run-jetty-run锁住css,js文件的问题。
开发中用run-jetty-run插件启动jetty调式tapestry5应用。tapestry5的live class loader用起来非常爽, 不管你改page class还是html模板都不用重启server。 但是有一个例外,那就是jetty起来之后css, js文件会被jetty锁住, 然后用eclipse修改不了。 所以改css js都非常麻烦, 每改一下就要重启下jetty。google之后发现原来:
引用

Jetty buffers static content for webapps such as html files, css files, images etc and uses memory mapped files to do this if the NIO connectors are being used. The problem is that on Windows, memory mapping a file causes the file to be locked, so that the file cannot be updated or replaced. This means that effectively you have to stop Jetty in order to update a file.


怪不得以前在ubuntu下没有这个问题,转到windows下就发现这个问题了。

解决办法就是找到run-jetty-run插件里面的jetty.jar。jetty.jar可以在eclipse中的jetty启动里面的Classpath中找到。 看下图



找到jetty.jar后解压,编辑org/mortbay/jetty/webapp/webdefault.xml这个文件。把useFileMappedBuffer改成false。这里也就是禁用memory mapped file.

Xml代码  收藏代码

    <init-param>  
      <param-name>useFileMappedBuffer</param-name>  
      <param-value>true</param-value> <!-- change to false -->  
    </init-param>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-347374-1-1.html 上篇帖子: Jetty嵌入式开发提供WEB服务 下篇帖子: Jetty实战之 嵌入式Jetty集成Spring运行
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表