Log4j configureAndWatch()产生了数千个线程 [英] Log4j configureAndWatch() spawning thousands of threads

查看:117
本文介绍了Log4j configureAndWatch()产生了数千个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So we have our J2EE application using Log4j like this

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();
     }
}

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

This way we can change the log-level realtime. Very handy. Most of our classes are set up just like this controller. We use the singleton-pattern so that we only have one instance of eash class; one call to PropertyConfigurator.configureAndWatch() for each class, once.

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

The problem: About twice a week our appserver dies and creates a heapdump. Using Heap Analyzer from IBM we can see that there seems to be alot of threads related to Log4j:

 808 (0%) [200] 9 org/apache/log4j/PropertyWatchdog 0x14282ad8

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

About 30,000 in total. So this is probably the reason for the sudden crash.


  1. 我们是否正确设置了这个?

  2. 重新部署EAR时,所有这些线程会发生什么?


推荐答案

您真正需要做的是使用应用服务器的启动过程(它们都是不同的)来初始化log4j系统。因为Log4j依赖于静态变量,所以它本身并不能独立工作(它可以,但这实际上取决于应用服务器)。在大多数情况下,整个应用程序服务器的配置确实是全局的。

Really what you need to do here is use the startup procedure of your application server (they are all different) to initialize the log4j system. Because Log4j relies on static variables, it can't really work independently in its own ear (it kind of can, but that will really depend on the application server). In most cases the configuration is really going to be global for the whole application server.

您需要确保只调用一次PropertyConfigurator.configureAndWatch方法。一种方法是在JNDI中放置一些东西。

You need to be sure that the PropertyConfigurator.configureAndWatch method is only called once. One way to do that is to put something in JNDI.

这很大程度上取决于应用服务器给你的东西。例如,我们使用JBoss,并将Log4J配置为其中的一部分,您只需更改log4j.xml文件以包含您的类所需的内容。 JBoss确保它是动态完成的。

A lot of this is going to depend on what the application server is giving you. For example, we use JBoss, and Log4J is configured as part of it, and you just change the log4j.xml file to include what your classes need. JBoss ensures that it is done dynamically.

编辑:这里是Websphere创建自定义服务的说明,内部您将创建log4J配置,并对文件进行监视。一些警告。您将不得不将log4j.jar添加到应用程序服务器本身的类路径中,以便它可用于战争或耳朵(我很确定无论如何都可以使用),并且自定义服务可能不会在耳边工作。

Here are the instructions for Websphere to create a custom service, and inside that you would create your log4J configuration, and do your monitoring of the file. A couple of caveats. You are going to have to add the log4j.jar to the classpath of the application server itself, so that it is available to the war or ear (I'm pretty sure that will work, anyway), and the custom service will likely not work inside the ear.

这里是一种替代方案,可以保留战争或耳朵中的所有内容,但代价是动态加载日志更改。

Here is an alternative that will keep everything in the war or ear, but at the expense of dynamically loading log changes.

这篇关于Log4j configureAndWatch()产生了数千个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆