Java 关闭时的Execute方法引发log4j非法状态异常无法注册关闭

Java 关闭时的Execute方法引发log4j非法状态异常无法注册关闭,java,log4j,shutdown-hook,Java,Log4j,Shutdown Hook,我想在退出swing应用程序时关闭连接。 因此,正如这里所建议的: 我在主线程中添加了以下代码,最终得到以下堆栈跟踪。它确实执行我的closeConnections方法 /** on exit */ Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { DeviceManager.getI

我想在退出swing应用程序时关闭连接。 因此,正如这里所建议的:

我在主线程中添加了以下代码,最终得到以下堆栈跟踪。它确实执行我的closeConnections方法

/** on exit */
         Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

                public void run() {
                     DeviceManager.getInstance().closeConnections();
                    //shutdown log4j2
                    if( LogManager.getContext() instanceof LoggerContext ) {
                        LogManager.getLogger().info("Shutting down log4j2");
                        Configurator.shutdown((LoggerContext)LogManager.getContext());
                    } else{
                        LogManager.getLogger().warn("Unable to shutdown log4j2");}

                }

            }, "Shutdown-thread"));

ERROR StatusLogger catching java.lang.IllegalStateException: Shutdown in progress
    at java.lang.ApplicationShutdownHooks.add(Unknown Source)
    at java.lang.Runtime.addShutdownHook(Unknown Source)
    at org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry.addShutdownHook(DefaultShutdownCallbackRegistry.java:136)
    at org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry.start(DefaultShutdownCallbackRegistry.java:125)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.initializeShutdownCallbackRegistry(Log4jContextFactory.java:123)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.<init>(Log4jContextFactory.java:89)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.<init>(Log4jContextFactory.java:54)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:96)
    at com.guitarjwindows.repositories.midi.WindowsMidiCommunicator.init(WindowsMidiCommunicator.java:46)
    at com.guitarjwindows.repositories.midi.WindowsMidiCommunicator.<init>(WindowsMidiCommunicator.java:33)
    at com.guitarjwindows.repositories.midi.WindowsMidiCommunicator.getInstance(WindowsMidiCommunicator.java:37)
    at com.guitarjwindows.service.DeviceManager.closeConnections(DeviceManager.java:35)
    at com.guitarjwindows.starter.Starter$1.run(Starter.java:33)
    at java.lang.Thread.run(Unknown Source)

2015-05-05 13:30:24,505 FATAL Unable to register shutdown hook because JVM is shutting down.
[INFO ] 2015-05-05 13:30:24.515 [Shutdown-thread] WindowsMidiCommunicator - Max receivers:0
[INFO ] 2015-05-05 13:30:24.516 [Shutdown-thread] WindowsMidiCommunicator - 2- Saffire 6USBobtained as inputDevice
[INFO ] 2015-05-05 13:30:24.516 [Shutdown-thread] WindowsMidiCommunicator - Max transmitters:-1
[INFO ] 2015-05-05 13:30:24.516 [Shutdown-thread] WindowsMidiCommunicator - Open receivers:
[INFO ] 2015-05-05 13:30:24.516 [Shutdown-thread] WindowsMidiCommunicator - No default receiver
[INFO ] 2015-05-05 13:30:24.516 [Shutdown-thread] WindowsMidiCommunicator - Open transmitters:
[INFO ] 2015-05-05 13:30:24.517 [Shutdown-thread] WindowsMidiCommunicator - Default transmitter: com.sun.media.sound.MidiInDevice$MidiInTransmitter@151107e
[INFO ] 2015-05-05 13:30:24.517 [Shutdown-thread] WindowsMidiCommunicator - Open transmitters now:
[INFO ] 2015-05-05 13:30:24.517 [Shutdown-thread] WindowsMidiCommunicator - com.sun.media.sound.MidiInDevice$MidiInTransmitter@151107e
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - Max receivers:-1
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - 2- Saffire 6USBobtained as outputDevice
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - Max transmitters:0
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - Open receivers:
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - Default receiver: com.sun.media.sound.MidiOutDevice$MidiOutReceiver@1716ab5
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - Open receivers now:
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - com.sun.media.sound.MidiOutDevice$MidiOutReceiver@1716ab5
[INFO ] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - Open transmitters:
[ERROR] 2015-05-05 13:30:24.518 [Shutdown-thread] WindowsMidiCommunicator - No default transmitter
[INFO ] 2015-05-05 13:30:24.571 [Shutdown-thread] WindowsMidiCommunicator - outputDevice closed
[INFO ] 2015-05-05 13:30:24.572 [Shutdown-thread] WindowsMidiCommunicator - inputDevice closed
2015-05-05 13:30:24,599 FATAL Unable to register shutdown hook because JVM is shutting down.
[INFO ] 2015-05-05 13:30:24.600 [Shutdown-thread] Starter$1 - Shutting down log4j2
退出时的
/***/
Runtime.getRuntime().addShutdownHook(新线程(新Runnable()){
公开募捐{
DeviceManager.getInstance().closeConnections();
//关闭日志4J2
if(LogManager.getContext()实例为LoggerContext){
LogManager.getLogger().info(“关闭log4j2”);
Configurator.shutdown((LoggerContext)LogManager.getContext());
}否则{
LogManager.getLogger().warn(“无法关闭log4j2”);}
}
}("关闭线程);;
错误状态记录器捕获java.lang.IllegalStateException:正在关闭
位于java.lang.ApplicationShutdownHooks.add(未知源)
位于java.lang.Runtime.addShutdownHook(未知源)
位于org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry.AddShutdowHook(DefaultShutdownCallbackRegistry.java:136)
位于org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry.start(DefaultShutdownCallbackRegistry.java:125)
位于org.apache.logging.log4j.core.impl.Log4jContextFactory.initializeShutdownCallbackRegistry(Log4jContextFactory.java:123)
位于org.apache.logging.log4j.core.impl.Log4jContextFactory(Log4jContextFactory.java:89)
位于org.apache.logging.log4j.core.impl.Log4jContextFactory(Log4jContextFactory.java:54)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源)
位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源)
位于java.lang.reflect.Constructor.newInstance(未知源)
位于java.lang.Class.newInstance(未知源)
位于org.apache.logging.log4j.LogManager.(LogManager.java:96)
位于com.guitarjwindows.repositories.midi.WindowsMidiCommunicator.init(WindowsMidiCommunicator.java:46)
位于com.guitarjwindows.repositories.midi.WindowsMidiCommunicator。(WindowsMidiCommunicator.java:33)
位于com.guitarjwindows.repositories.midi.WindowsMidiCommunicator.getInstance(WindowsMidiCommunicator.java:37)
位于com.guitarjwindows.service.DeviceManager.closeConnections(DeviceManager.java:35)
位于com.guitarjwindows.starter.starter$1.run(starter.java:33)
位于java.lang.Thread.run(未知源)
2015-05-05 13:30:24505致命无法注册关闭挂钩,因为JVM正在关闭。
[信息]2015-05-05 13:30:24.515[关机线程]WindowsMidiCommunicator-Max接收器:0
[信息]2015-05-05 13:30:24.516[关机线程]WindowsMidiCommunicator-2-Saffire 6U被指定为输入设备
[信息]2015-05-05 13:30:24.516[关机线程]Windows MIDI通讯器-最大变送器:-1
[信息]2015-05-05 13:30:24.516[关机线程]Windows MIDI通讯器-打开接收器:
[信息]2015-05-05 13:30:24.516[关机线程]WindowsMidiCommunicator-无默认接收器
[信息]2015-05-05 13:30:24.516[关机线程]Windows MIDI通讯器-打开变送器:
[信息]2015-05-05 13:30:24.517[关机线程]WindowsMidiCommunicator-默认发射机:com.sun.media.sound.MidiInDevice$MidiInTransmitter@151107e
[信息]2015-05-05 13:30:24.517[关机线程]Windows MIDI通讯器-立即打开变送器:
[信息]2015-05-05 13:30:24.517[关机线程]WindowsMidiCommunicator-com.sun.media.sound.MidiInDevice$MidiInTransmitter@151107e
[信息]2015-05-05 13:30:24.518[关机线程]Windows MIDI通讯器-最大接收器:-1
[信息]2015-05-05 13:30:24.518[关机线程]WindowsMidiCommunicator-2-Saffire 6USB作为输出设备
[信息]2015-05-05 13:30:24.518[关机线程]WindowsMidiCommunicator-Max变送器:0
[信息]2015-05-05 13:30:24.518[关机线程]Windows MIDI通讯器-打开接收器:
[信息]2015-05-05 13:30:24.518[关机线程]WindowsMidiCommunicator-默认接收器:com.sun.media.sound.MidiOutDevice$MidiOutReceiver@1716ab5
[信息]2015-05-05 13:30:24.518[关机线程]WindowsMidiCommunicator-立即打开接收器:
[信息]2015-05-05 13:30:24.518[关机线程]WindowsMidiCommunicator-com.sun.media.sound.MidiOutDevice$MidiOutReceiver@1716ab5
[信息]2015-05-05 13:30:24.518[关机线程]Windows MIDI通讯器-打开变送器:
[错误]2015-05-05 13:30:24.518[关机线程]WindowsMidiCommunicator-无默认变送器
[信息]2015-05-05 13:30:24.571[关机线程]WindowsMIDI通讯器-输出设备关闭
[信息]2015-05-05 13:30:24.572[关机线程]Windows MIDI通讯器-输入设备关闭
2015-05-05 13:30:24599致命无法注册关闭挂钩,因为JVM正在关闭。
[信息]2015-05-05 13:30:24.600[关闭线程]启动器$1-关闭日志4J2

我认为log4j也有一个shutdown钩子,shutdown钩子的执行顺序由JVM决定,所以您不能影响它。我可能可以禁用log4j shutdownhook并自己进行清理,但最好检查log4j的功能。

尝试获取LoggerContext状态并在关闭之前对其进行测试:(LoggerContext)LogManager.getContext().getState()@jMounir getState()方法不存在您建议的方式。很抱歉我弄错了,应该这样调用它:((LoggerContext)LogManager.getContext()).getState()@jMounir我已经在run方法的顶部添加了它。我开始了。原始帖子中显示的错误显示在“state=started”打印输出之前。您将此代码放在何处。因为我看到了关于退出的评论*/你是指退出事件吗?。因为当程序