您的位置:首页 > 运维架构 > Tomcat

zz[Java基础]Tomcat 5.5 的热部署支持

2008-03-18 16:03 483 查看
本来以为 Tomcat 是不支持热部署的。其实是支持的。

工作环境:

JDK 5,Eclipse 3.1.2,MyEclipse 4.1.1GA、Tomcat 5.5.16

假设工程名称是 Xnews,对应网站为 http://localhost:8080/Xnews

并假设 Tomcat 的安装目录为 %TomcatDir%

在 %TomcatDir%/conf/Catalina/localhost 目录下新建一个配置文件 Xnews.xml,内容如下:
<Context docBase="${catalina.home}/webapps/Xnews"
privileged="true" antiResourceLocking="true" antiJARLocking="true">
</Context>

重新启动 Tomcat,之后就可以在 MyEclipse 中使用 Redeploy 进行热部署了。

使用这种设置,每次重新启动 Tomcat 服务,会在 %TomcatDir%/temp 下新建一个项目副本

如 n-Xnews,其中 n 为从 0 开始的数字。

这种情况下使用 request.getRealPath() 方法得到的物理路径其实就是指向这个副本的物理路径而不是在 webapps 中的原始路径。

所以如果要使 request.getRealPath() 指向 webapps 原始路径,那么只能关闭这种热部署设定,即采用默认配置。

====================================================================================
另外一篇:

********************************************************************************************************
热部署TOMCAT,我自己摸索的方法,作为经验之谈.
  eclipse3.1.2 + myeclipse4.1 + struts1.1 + apache-tomcat-5.5.15
  新建web project的时候(假设工程名为StrutsTest)在tomcat/conf/Catalina/localhost下新建一个XML文件,内容为
  <Context docBase="${catalina.home}/webapps/StrutsTest"
   privileged="true" antiResourceLocking="true" antiJARLocking="true">

  </Context>

  保存后把文件名改为工程名 这里为StrutsTest.xml

  在新建部署的时候选择"Exploded Archive",而不是选择"Packaged Archive".

  启动tomcat之后就可以不用再关掉了.将来如果改动了工程内的任何内容,直接选择"Redeploy"
  不要选"Remove"(否则StrutsTest.xml将会被自动删掉).这样就不需要反复重启tomcat去预览
  做好的工程.也就做到了热部署.
  欢迎网友补充及纠正
*******************************************************************************************************

我就按照他的说法自己在tomcat/conf/Catalina/localhost下加了个xml文件:(我的工程名叫“OOTV”)
<Context docBase="${catalina.home}/webapps/OOTV"
   privileged="true" antiResourceLocking="true" antiJARLocking="true">
  </Context>

没想到竟然不成功!仔细检查!我的操作跟他写的几乎没有什么差别啊!连开发环境几乎都是一样的!难道还有什么其他的原因???

昨天晚上我们一起吃的饭,这是个好机会,绝对不能放过!饭后,硬把他拉到我家去解决热部署猫的问题!嘿嘿!

最后终于发现了问题的根源,原来我的server.xml文件中有这么一段代码(如下),跟吴迪说的方法冲突了。
<Context path="/OOTV" docBase="OOTV" >
<Resource name="jdbc/ootv"
auth="Container" type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="831225"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/>
</Context>

Resource里面的代码是用来连接数据库的,关键是<Context path="/OOTV" docBase="OOTV" >这句,没有定义解锁的属性!估计Tomcat是先执行server.xml文件的Context属性,然后才去运行OOTV.xml里面的Context!所以通过<Context path="/OOTV" docBase="OOTV" >这句,猫就已经把所有的配置文件和JAR包都锁住了!再运行OOTV.xml当然不起作用了!

为了证实这一点,我们把server.XML文件里面的那段代码剪切到OOTV.XML中,再重启猫,选择Redeploy。这时 看到eclipse底下的Console中蹦出一句信息: “Reloading context [/OOTV]”!表示重新发布成功,这时候猫就已经成功热部署了! 以后无论你怎么更改工程,都不需要通过重启猫来预览了,直接选择Redeploy就 可以!呵呵!:-)

附:Tomcat的热部署原理,估计是官方发布的!我是在5Dinfo上找到的!

********************************************************************************************************

Tomcat的热部署

It is possible to deploy web applications to a running Tomcat server.
If the Host autoDeploy attribute is "true", the Host will attempt to deploy and update web applications dynamically, as needed, for example if a new .WAR is dropped into the appBase. For this to work, the Host needs to have background processing enabled which is the default configuration.
autoDeploy set to "true" and a running Tomcat allows for:

Deployment of .WAR files copied into the Host appBase.

Deployment of exploded web applications which are copied into the Host appBase.

Re-deployment of a web application which has already been deployed from a .WAR when the new .WAR is provided. In this case the exploded web application is removed, and the .WAR is expanded again. Note that the explosion will not occur if the Host is configured so that .WARs are not exploded with a unpackWARs attribute set to "false", in which case the web application will be simply redeployed as a compressed archive.

Re-deployment of a web application if the /WEB-INF/web.xml file (or any other resource defined as a WatchedResource) is updated.

Re-deployment of a web application if the Context Descriptor file from which the web application has been deployed is updated.

Re-deployment of a web application if a Context Descriptor file (with a filename corresponding to the Context path of the previously deployed web application) is added to the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory.

Undeployment of a web application if its document base (docBase) is deleted. Note that on Windows, this assumes that anti-locking features (see Context configuration) are enabled, otherwise it is not possible to delete the resources of a running web application.

********************************************************************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: