A word on Contexts[涉及上下文的一个词汇]
在谈到关于Web应用程序部署的时候,这个Context上下文的概念是必须明白的。. 一个上下文在Tomcat中称之为Web应用程序。
在Tomcat中为了配置一个上下文,一个上下文描述器文件是必须的。一个Context描述器是一个简答的XML文件,它包含了一个与Tomcat有关的Context的配置,例如命naming resources 或者session manager 配置.在Tomcat的早期版本这个上下文描述器配置的内容通常存储在Tomcat的主配置文件Server.xml里面。但是现在不推荐这样做了(虽然目前仍然支持)。
上下文描述不仅仅帮助Tomcat知道如何配置上下文,同时其他工具像这个Tomcat Manager和TCD通常使用这些上下文描述器去适当地完成他们的任务。
上下文描述器的位置如下:
1. $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml
2. $CATALINA_BASE/webapps/[webappname]/META-INF/context.xml
第一种方式文件被命名为 [webappname].xml 但是在第二中方式文件被命名为context.xml. 如果上下文描述器没有被提供,Tomcat将使用缺省值配置应用程序的上下文。.
Deployment on Tomcat startup[Tomcat启动的时候部署]
如果你没有兴趣采用Tomcat Manager, or TCD 部署你的应用程序, 那么你需要采用静态方式部署你的应用程序到Tomcat, 跟随Tomcat的启动[Tomcat启动的时候会一并启动你静态部署的应用程序]。你部署的应用程序的位置要和特定虚拟主机的appBase属性描述位置一致[webapps]. 你可以拷贝一个应用程序目录[就是未压缩目录]到这个位置或者一个经过压缩的应用程序资源文件[.WAR].
Web应用程序存在的位置由虚拟主机(缺省情况下主机名是"localhost")的appBase属性(缺省的appBase 是 "$CATALINA_BASE/webapps")指定。
它们仅仅在虚拟主机的deployOnStartup属性值是true的条件下随着Tomcat的启动被自动部署。
如果是那样的话Tomcat启动的时候将遵循以下部署顺序:
1. 任意上下文描述器文件将首先被部署。
2. 没有被任何上下文描述器引用的解压缩的Web应用程序将然后被部署。如果它们与个appBase描述路径下的.WAR文件关联并且它这个.WAR文件比展开的这个目录新,那么这个展开的目录将被删除然后这个Web应用程序将从这个.WAR文件重新部署。
3. .WAR 文件将被部署。
Note again that for each deployed web application, a Context Descriptor will be createdunless one exists already.
Deploying on a running Tomcat server[Tomcat服务器运行期间部署]
If true, the Tomcat classloader will take extra measures to avoid JAR file locking when resources are accessed inside JARs through URLs. This will impact startup time of applications, but could prove to be useful onplatforms or configurations where file locking can occur. If not specified, the default value isfalse.
antiJARLocking is a subset ofantiResourceLocking and therefore, to preventduplicate work and possible issues, only one of these attributes should be set totrue at any one time.
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
再次删除app1,就不会有上面的提示了。删除app1后我们观察日志输出:
信息: Undeploying context [/app1]
2012-6-3 23:27:02 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/app1] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@3ca56f]) and avalue of type [java.lang.Object[]] (value [[Ljava.lang.Object;@191394e]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
2012-6-3 23:27:02 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/app1] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@3ca56f]) and avalue of type [java.lang.Object[]] (value [[Ljava.lang.Object;@149494e]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
2012-6-3 23:27:02 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/app1] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@3ca56f]) and avalue of type [java.lang.Object[]] (value [[Ljava.lang.Object;@6bbb36]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
2012-6-3 23:27:02 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/app1] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@3ca56f]) and avalue of type [java.lang.Object[]] (value [[Ljava.lang.Object;@d006a7]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
二、通过为Web应用程序添加一个特定的Context文件来实现动态部署
我们找到以下位置:conf/Catalina/localhost,这个目录代表什么?
Catalina代表server.xml文件里面配置的默认的Service的名称。
Localhost代表Service里面的默认虚拟主机名称。
我们现在就是要在这个虚拟主机里面部署我们的应用。
做法:
1、 创建一个xml文件,文件名和Web应用名相同:app1.xml。
2、 这个文件的内容如下:
a) <Context path="/app1" docBase="d:/app1" debug="0" privileged="true" workDir="d:/app1work"></Context>
3、 将这个文件拷贝到conf/Catalina/localhost下面,完整路径就是conf/Catalina/localhost/app1.xml,这时Tomcat会自动扫描到这个文件然后根据里面的描述加载应用上下文的。
4、 观察现在的日志输出:
l 信息: Deploying configuration descriptor E:\WorkSpace\JavaWorkspace\Tomcat 7.0\conf\Catalina\localhost\app1.xml
l 2012-6-3 23:41:23 org.apache.catalina.startup.SetContextPropertiesRule begin
l 警告: [SetContextPropertiesRule]{Context} Setting property 'debug' to '0' did not find a matching property.
l 2012-6-3 23:41:24 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
l 信息: Parsing configuration file [struts-default.xml]
l 2012-6-3 23:41:24 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
l 信息: Parsing configuration file [struts-plugin.xml]
l 2012-6-3 23:41:24 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
l 信息: Parsing configuration file [struts.xml]
5、 删除这个文件后,日志输出:
l 信息: Undeploying context [/app1]
l 2012-6-3 23:43:06 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
l 严重: The web application [/app1] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@1f1a3a8])and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@12f9ee1]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.