tomcat reload&autodeploy log4j static静态变量 空指针错误

默认情况下,Tomcat在启动时[color=red]会自动加载webapps目录下的项目(Host节的deployOnStartup属性)[/color],同时如果项目默认被监视的资源文件web.xml(context.xml中定义<WatchedResource>WEB-INF/web.xml</WatchedResource>)、class文件、或者lib目录下的jar文件发生变化,[color=red]Tomcat会自动进行重加载、或者重Deploy操作[/color]。
也可以使用probe项目对Tomcat下webapps中各项目进行管理,执行stop,start,reload等操作。

autoDeploy(HostConfig的check操作,然后调用StandContext的reload操作) 参考:[url=http://tomcat.apache.org/tomcat-6.0-doc/deployer-howto.html]Tomcat Web Application Deployment - Apache Tomcat 6.0[/url]
reloadable(WebappLoader的backgroundProcess操作,检查Context的reloadable属性,然后决定是否继续操作。) 参考:[url=http://tomcat.apache.org/tomcat-6.0-doc/config/context.html]The Context Container - Apache Tomcat Configuration Reference[/url]
由此可以看出[color=red]autoDeploy和reloadable分别对应不同的操作,配置检查通过后,最终都会调用StandContext的reload来实现的。[/color],需要注意的是,什么操作会触发[color=red]后台线程ContainerBase内部类ContainerBackgroundProcessor[/color]的检查,以及分别对应什么操作(reload还是deploy)。
[color=green]
web.xml文件的修改会触发AutoDeploy,受host节的autoDeploy配置影响
class类文件修改会触发Reload操作,受reloadable配置影响
[/color]
不管是上述的reloadable、autoDeploy、或者probe项目的reload操作,最终都会调用StandardContext的reload操作


而当执行完StandardContext的reload操作后,[color=red]当被访问的请求代码中使用了log4j,logger会报空指针错误。[/color]根本原因是,StandardContext做reload操作时,其中一个步骤会调用WebappClassLoader的stop操作,而在WebappClassLoader的stop操作时,[color=red]会根据变量ENABLE_CLEAR_REFERENCES的值(默认true),来清除static或者final变量的值,[/color]被清除后,再访问就自然会出错。
[color=green]
因此,
1,可以通过设置ENABLE_CLEAR_REFERENCES为false来解决这个问题(通过-Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES="false"来设置系统变量)
2,或者设置不进行重加载操作。context.xml中增加reloadable="false"属性
3,或者设置不进行自动Deploy操作。Host节中的autoDeploy="false"
4,Eclipse开发的话,Server属性中的把Automatically publish when resources chage改为Never publish automatically
[/color]

org.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES

If true, Tomcat attempts to null out any static or final fields from loaded classes when a web application is stopped as a work around for apparent garbage collection bugs and application coding errors.
There have been some issues reported with log4j when this option is true.
Applications without memory leaks using recent JVMs should operate correctly with this option set to false.
If not specified, the default value of true will be used.

[url=http://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html]System Properties - Apache Tomcat Configuration Reference[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值