jetty 远程调试怎么起用调试模式

使用Jetty加快调试速度 - samjavaeye - ITeye技术网站
博客分类:
为什么要用Jetty?
我们在开发过程中,经常遇到修改一个java类,哪怕只修改了一行代码,也要重启Tomcat才能看到修改结果的情况。这样也许修改代码只花了5秒钟,要看到结果却需要等50秒钟。有没有一种方法可以不用重应用启服务器,直接可以看到修改结果,就像修改Jsp一样呢?
***是有----那就是使用Jetty。
之前也有同事写过使用Jetty的案例,不过那是以插件的方式去用,我个人比较偏好“轻量”开发,不喜欢弄一大堆插件在eclipse里面,因此这里给大家提供另一种选择----直接以application的方式来运行Jetty,只需要将相应的jar包加入到类路径中去就行了。
第一步,引入相应的jar包
将附件中的压缩文件,解压到任意目录,然后在eclipse里面新建一个“User Libraries”,将这些jar添加进去。
然后将这个libraries添加到我们正在开发的项目中去。
第二步,编写Jetty启动程序
程序很简单,只有一个main方法即可:
public static void main(String[] args) throws Exception {
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(80); // 设置要***的端口.
server.addConnector(connector);
WebAppContext context = new WebAppContext();
context.setContextPath("/irm"); // 设置上下文路径
// 设置web应用根路径
context.setResourceBase("D:/java/workspace/EKP-IRM/WebContent");
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.addHandler(context);
server.setHandler(handlerCollection);
server.start();
第三步,开始调试
以debug方式运行第二步中编写的启动程序(注意一定要是debug方式,否则无法热加载改动过的Java类),打开浏览器,开始访问。
Jetty的局限性
只能热加载Java类,无法热加载资源文件、Struts配置文件、Spring配置文件等。
调试次数多了之后,容易出现热加载Java类失败的情况,此时不容易发觉,还以为自己改得不对。
samjavaeye
浏览: 85042 次
来自: 深圳
weirihai 写道访问都地址都打不开。
刚点了一遍,都能打 ...
访问都地址都打不开。您所在的位置: &
1.1.4.3 启动Jetty进行开发调试
1.1.4.3 启动Jetty进行开发调试
清华大学出版社
《Struts2技术内幕:深入解析Struts架构设计与实现原理》第1章厉兵秣马—开发环境准备,本章我们首先向读者介绍了开发Struts2应用程序所必需的开发环境的搭建过程。这一过程包含:***与配置JDK、***与配置Eclipse、***与配置Web服务器。除此之外,着重介绍了在Eclipse中搭建Web开发环境并进行源码级别调试的详细过程。本节为大家介绍如何启动Jetty进行开发调试。
1.1.4.3 启动Jetty进行开发调试
启动Jetty服务器进行基本的Web开发调试的步骤非常简单,只需要在项目中编写一个JettyStarter的Java类,并将这个类作为一个基本的Application执行即可。JettyStarter的源码如代码清单1-1所示。
代码清单1-1& JettyStarter.java
package& &&import&org.mortbay.jetty.C &import&org.mortbay.jetty.S &import&org.mortbay.jetty.nio.SelectChannelC &import&org.mortbay.jetty.webapp.WebAppC &&/** &*&Jetty&Server&启动类 &*& &*&@author &*/ &public&class&JettyStarter&{ &&/** &&&&&*&@param&args & *&@throws&Exception & */ &public&static&void&main(String[]&args)&throws&Exception&{ &&&&&long&begin&=&System.currentTimeMillis(); &&&&&Connector&connector&=&new&SelectChannelConnector(); &&&&&connector.setPort(Integer.getInteger("jetty.port",&80).intValue()); &&&&&&WebAppContext&webapp&=&new&WebAppContext("web",&"/struts_example"); &&&&&&&&& &&&&&Server&server&=&new&Server(); &&&&&server.setConnectors(new&Connector[]&{&connector&}); &&&&&server.setHandler(webapp); &&&&&server.start(); &&&&&&System.out.println("Jetty&Server&started,&use&"&+&(System.currentTimeMillis()&-&begin)&+&"&ms"); &&} &}&
有了JettyStarter这个类之后,我们可以直接将这个类以Application方式来运行。此时,Jetty服务器就会启动(如图1-10所示),从而成为一个Web容器的提供者。当然,我们也可以使用调试模式(Debug)执行这个类,从而使得整个Jetty 服务器处于调试模式。
(点击查看大图)图1-10 JettyStarter启动界面启动成功后,我们会在控制台看到成功启动的日志:[main]&INFO&org.mortbay.log&-&Logging&to& &org.slf4j.impl.SimpleLogger&via& &org.mortbay.log.Slf4jLog &[main]&INFO&org.mortbay.log&-&jetty-6.1.7 &[main]&INFO&org.mortbay.log&-&Started& &SelectChannelConnector@127.0.0.1:8080 &Jetty&Server&started,&use&1547&ms&
此时,Jetty 服务器被引入作为Web应用的开发调试服务器,Web开发调试环境也就成功搭建起来。我们可以通过浏览器对Web应用进行访问,而其中的IP地址、端口、Web应用的入口名称,都可以在JettyStarter这个Java类中进行API级别的相应设置。
如果JettyStarter运行在调试模式下,我们就可以在Web项目中加入熟悉的调试元素,例如加入断点(Breakpoint)、加入监视(Watch)、单步运行(Step by),从而对整个项目的运行状态进行调试和监控。
相比许多其他的开发环境搭建方式,本书所介绍的Web开发环境至少拥有以下的好处:
开发环境的建立并不依赖于任何IDE或者相关的插件,只需要运行某个Java类就可以进行调试
开发环境的建立不依赖于任何外部Web服务器的***,无论将项目迁移到什么样的IDE或者操作系统,只要是有Java运行环境的地方,都可以直接运行
在项目中内置开发环境的做法,降低了程序员的学习成本
【责任编辑: TEL:(010)】&&&&&&
关于&&&&的更多文章
这本书是写给程序员和项目经理的。作者结合自身的丰富成长历程,
本书描述了黑客用默默无闻的行动为数字世界照亮了一条道路的故事。
《C#高级编程(第8版)》是C# 2012和.NET 4.5高级技术的
《Android 4 游戏入门经典(第3版)》将赋予您惊人的灵
本书这一卷是介绍构建面向对象的联网与并发中间件的开
本书是由长期从事网络管理工作和网络工程人员培训工作的一线网管人员和教学人员精心编写,从现实的技术发展角度和实际应用的角度
51CTO旗下网站新手配置 Jetty + Eclipse
(Hot code debugging)
yuxinleipp
发布时间: 10:45:44
在用Tomcat 开发Java Web project 时,在调试的时候,由于我们不断改动java 源文件,因而我们需要不断重新启动Tomcat 更新我们的项目,我们把很多时间浪费在了更新上,这样开发效率大大降低。
Jetty 完全支持Hot code debugging ,也就是说,我们在更改java源文件,更改html,jsp时不用再重新启动服务器了,这样节省了大量时间(注意web.xml 等其他配置文件的更新还是需要重新启动服务器的)。
好了现在开始教你如何配置Jetty 到Eclipse.
这个地址可以***eclipse 对jetty支持所需要的东西,可以通过eclipse自动***
一、***插件
jetty 在eclipse的插件现在名字叫run jetty run (日)
***方法就不说了。安完后,点击eclipse的run configuration,查看***成功
配置(可省略直接跳到三)
要想让Jetty启动Web服务器,则要告诉Jetty一些参数,比如端口是多少,哪里才是Web的根目录等。
这个XML配置文件是通用的,对于一般的应用,要修改的个性部分很少,所以保存一份,然后其它的项目只要复制粘贴就行了。
和得到Jar包依赖一样,也有两种方式得到该配置文件。
第一种方法是直接在Jetty官网中下载,然后根据自己的实际情况改下就行了:
第二种方式是复制以下内容,然后粘贴保存就行了:
&?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&&
&Newclass=&org.mortbay.jetty.nio.SelectChannelConnector&&
&Set name=&port&&
&SystemPropertyname=&jetty.port& default=&80& /&&!--端口号 --&
&Setname=&maxIdleTime&&30000&/Set&
&Set name=&Acceptors&&2&/Set&
&Set name=&statsOn&&false&/Set&
&Set name=&confidentialPort&&8443&/Set&
&Setname=&lowResourcesConnections&&5000&/Set&
&Setname=&lowResourcesMaxIdleTime&&5000&/Set&
&/Arg& &/Call& &Set name=&sessionIdManager&&
&Newclass=&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&&
&Arraytype=&org.mortbay.jetty.Handler&&
&New id=&Contexts&class=&org.mortbay.jetty.handler.ContextHandlerCollection& /&
&New id=&DefaultHandler&class=&org.mortbay.jetty.handler.DefaultHandler& /&
&New id=&RequestLog&class=&org.mortbay.jetty.handler.RequestLogHandler& /&
&/New& &/Set& &Set name=&handler&&
&New id=&Handlers&class=&org.mortbay.jetty.handler.HandlerCollection&&
&Set name=&handlers&&
&Arraytype=&org.mortbay.jetty.Handler&&
&Newclass=&org.mortbay.jetty.webapp.WebAppContext&&
&Set name=&contextPath&&/&/Set&&!-- ContextPath--&
&Set name=&resourceBase&&./WebRoot&/Set&&!-- Web应用根目录 --&
&Callname=&addServlet&&
&Arg&org.mortbay.jetty.servlet.DefaultServlet&/Arg&
&Arg&/&/Arg&
&!-- 增加其它的Servlet --&
&/New& &/Set& &Set name=&UserRealms&&
&Arraytype=&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配置文件,请将其中的相应目录修改成自己项目的目录:
&?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&
&!-- 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&
&!-- =========================================================== --&
&!-- Set connectors
&!-- =========================================================== --&
&!-- One of each type!
&!-- =========================================================== --&
&!-- Use this connector for many frequently idle connections
and for threadless continuations.
&Call name=&addConnector&&
&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&
&!-- Use this connector if NIO is not available.
&Call name=&addConnector&&
&New class=&org.mortbay.jetty.bio.SocketConnector&&
&Set name=&port&&8081&/Set&
&Set name=&maxIdleTime&&50000&/Set&
&Set name=&lowResourceMaxIdleTime&&1500&/Set&
&!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --&
&!-- 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&
&!-- =========================================================== --&
&!-- 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&&
&New id=&Contexts& class=&org.mortbay.jetty.handler.ContextHandlerCollection&/&
&New id=&DefaultHandler& class=&org.mortbay.jetty.handler.DefaultHandler&/&
&New id=&RequestLog& class=&org.mortbay.jetty.handler.RequestLogHandler&/&
&Set name=&handler&&
&New id=&Handlers& class=&org.mortbay.jetty.handler.HandlerCollection&&
&Set name=&handlers&&
&Array type=&org.mortbay.jetty.Handler&&
&New id=&RequestLog& class=&org.mortbay.jetty.handler.RequestLogHandler&/&
&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&
&!-- =========================================================== --&
&!-- 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&&
&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&
&!-- =========================================================== --&
&!-- 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&
&!-- =========================================================== --&
&!-- 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插件启动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 , 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.&
&init-param&&&
&&¶m-name&useFileMappedBuffer&/param-name&&&
&&¶m-value&true&/param-value&&&!--&change&to&false&--&&&
&/init-param&&&
版权声明:本文为博主原创文章,未经博主允许不得转载。
来源:http://blog.csdn.net/yuxinleipp/article/details/7642180

参考资料

 

随机推荐