Log4j configureAndWatch()产生了数千个线程

Tom*_*mmy 2 java log4j java-ee

所以我们使用Log4j这样的J2EE应用程序

public class CustomerController 
{
    private static Logger logger = Logger.getLogger(CustomerController.class);

     public CustomerService customerservice = null;

     public CustomerController() throws Exception 
     {
           PropertyConfigurator.configureAndWatch("c:\log4j.property", 50000);

            customerservice = ServiceManagerSingleton.getCustomerServiceInstance();
     }
}
Run Code Online (Sandbox Code Playgroud)

这样我们就可以改变日志级别的实时性.非常便利.我们的大多数类都像这个控制器一样设置.我们使用单例模式,这样我们只有一个eash类的实例; 一次调用每个类的PropertyConfigurator.configureAndWatch().

问题:我们的appserver每周大约两次死亡并创建一个堆转储.使用IBM的Heap Analyzer我们可以看到似乎有很多与Log4j相关的线程:

 808 (0%) [200] 9 org/apache/log4j/PropertyWatchdog 0x14282ad8
Run Code Online (Sandbox Code Playgroud)

总共约30,000.所以这可能是突然崩溃的原因.

  1. 我们正确设置了吗?
  2. 在重新部署EAR时,所有这些线程会发生什么?

mat*_*t b 7

CustomerController实例创建的频率是多少?每次请求一次?因为我相信configureAndWatch()会在每次调用时产生一个新线程.

另外,如果您不知道,log4j文档警告不要在J2EE环境中使用此功能:

因为configureAndWatch启动了一个单独的wathdog线程,并且因为无法在log4j 1.2中停止此线程,所以configureAndWatch方法对于在回收应用程序的J2EE环境中使用是不安全的.

我知道你没有使用Spring,但在我看来,Spring类Log4jWebConfigurer 更好地解释了为什么这个特性在J2EE中是危险的:

警告: Log4j的监视程序线程在VM关闭之前不会终止; 特别是,它不会在LogManager关闭时终止.因此,建议不要在生产J2EE环境中使用配置文件刷新; 监视程序线程不会在应用程序关闭时停止.

更新:查看log4j源代码,每次调用configureAndWatch()确实会创建一个新线程.