编程语言
首页 > 编程语言> > 为什么JBoss 5.1中的ShutdownHook中的java.util.Logger并不总是打印到server.log?

为什么JBoss 5.1中的ShutdownHook中的java.util.Logger并不总是打印到server.log?

作者:互联网

我在JBoss 5.1中有一个EJB3-Timer.
编辑:
ShutdownHook应设置一个标志,doTimeOut()可以正常终止(否则它将完成作业,并且在此计时器完成之前停止关闭).

@Stateless
class Timer {
    private static Thread hook;
    static {
       hook = new ShutdownHook();
    }
    @Timeout
    public void doTimeOut(){
       //some code
    }

    private class ShutdownHook implements Runnable(){

        Logger.getLogger(ShutdownHook.class.getName()).log(Level.INFO, "MyBEAN.ShutdownHook execute hook " + hook); // Not always printed to log
        Logger.getAnonymousLogger().log(Level.INFO, "MyBEAN.ShutdownHook execute hook " + hook); // throwns Exception
        System.out.println("MyBEAN.ShutdownHook execute hook " + hook);
        [...] // some code
    }
}

我发现这两个帖子,但它们是从2005年开始的,bug被标记为CLOSED:

http://www.pankaj-k.net/archives/2005/08/jboss_shutdown.html

http://jira.jboss.com/jira/browse/JBAS-2087

anonymousLogger抛出的异常是NullPointer:

2011-02-02 09:16:33,120 ERROR [STDERR] Exception in thread "Thread-33" 
2011-02-02 09:16:33,122 ERROR [STDERR] java.lang.NullPointerException
2011-02-02 09:16:33,122 ERROR [STDERR]  at org.apache.log4j.CategoryKey.<init>(CategoryKey.java:31)
2011-02-02 09:16:33,122 ERROR [STDERR]  at org.apache.log4j.Hierarchy.getLogger(Hierarchy.java:261)
2011-02-02 09:16:33,122 ERROR [STDERR]  at org.apache.log4j.Hierarchy.getLogger(Hierarchy.java:242)
2011-02-02 09:16:33,122 ERROR [STDERR]  at org.apache.log4j.LogManager.getLogger(LogManager.java:188)
2011-02-02 09:16:33,122 ERROR [STDERR]  at org.apache.log4j.Logger.getLogger(Logger.java:104)
2011-02-02 09:16:33,122 ERROR [STDERR]  at org.jboss.logbridge.LogBridgeHandler.publish(LogBridgeHandler.java:71)
2011-02-02 09:16:33,122 ERROR [STDERR]  at java.util.logging.Logger.log(Unknown Source)
2011-02-02 09:16:33,122 ERROR [STDERR]  at java.util.logging.Logger.doLog(Unknown Source)
2011-02-02 09:16:33,122 ERROR [STDERR]  at java.util.logging.Logger.log(Unknown Source)
2011-02-02 09:16:33,122 ERROR [STDERR]  at com.demo.MyBEAN$ShutdownHook.run(MyBEAN.java:202)

有任何想法吗?

解决方法:

由于Log4j已关闭,因此记录器可能不可用,这显然发生在运行JVM关闭挂钩之前.如果你需要在关机时记录一些东西,我建议你加入JBoss机制,例如使用@PreDestroy.

然后你的代码变成了

@Stateless
class Timer {

    @Timeout
    public void doTimeOut(){
       //some code
    }

    @PreDestroy
    public void onShutdown() {

        Logger.getLogger(Timer.class.getName()).log(Level.INFO, "MyBEAN.ShutdownHook execute hook " + hook);
        [...] // some code
    }
}

标签:java,jboss,ejb,java-util-logging,shutdown-hook
来源: https://codeday.me/bug/20190626/1297366.html