फिर स्रोत पर देख रहे हैं, समाधान एक प्रणाली संपत्ति java.util.logging.manager
जो LogManager का एक उपवर्ग जो reset();
विधि ओवरराइड करता है तो संग्रह करने वालों शटडाउन पर काम करना जारी है परिभाषित करने के लिए प्रतीत होता है।
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class Main {
static {
// must be called before any Logger method is used.
System.setProperty("java.util.logging.manager", MyLogManager.class.getName());
}
public static class MyLogManager extends LogManager {
static MyLogManager instance;
public MyLogManager() { instance = this; }
@Override public void reset() { /* don't reset yet. */ }
private void reset0() { super.reset(); }
public static void resetFinally() { instance.reset0(); }
}
public static void main(String... args) {
Logger logger1 = Logger.getLogger("Main1");
logger1.info("Before shutdown");
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
try {
Logger logger2 = Logger.getLogger("Main2");
logger2.info("Shutting down 2");
} finally {
MyLogManager.resetFinally();
}
}
}));
}
}
प्रिंट
Dec 11, 2012 5:56:55 PM Main main
INFO: Before shutdown
Dec 11, 2012 5:56:55 PM Main$1 run
INFO: Shutting down 2
LogManager के लिए इस कोड से, आप देख ही बंद हुक जो संचालकों dismantles और उन्हें बंद कर देता है वहाँ देख सकते हैं। लॉगर केवल शट डाउन में काम करता है अगर इसका उपयोग पहले नहीं किया गया है, इसलिए यह कोड नहीं चला है।
// This private class is used as a shutdown hook.
// It does a "reset" to close all open handlers.
private class Cleaner extends Thread {
private Cleaner() {
/* Set context class loader to null in order to avoid
* keeping a strong reference to an application classloader.
*/
this.setContextClassLoader(null);
}
public void run() {
// This is to ensure the LogManager.<clinit> is completed
// before synchronized block. Otherwise deadlocks are possible.
LogManager mgr = manager;
// If the global handlers haven't been initialized yet, we
// don't want to initialize them just so we can close them!
synchronized (LogManager.this) {
// Note that death is imminent.
deathImminent = true;
initializedGlobalHandlers = true;
}
// Do a reset to close all active handlers.
reset();
}
}
/**
* Protected constructor. This is protected so that container applications
* (such as J2EE containers) can subclass the object. It is non-public as
* it is intended that there only be one LogManager object, whose value is
* retrieved by calling Logmanager.getLogManager.
*/
protected LogManager() {
// Add a shutdown hook to close the global handlers.
try {
Runtime.getRuntime().addShutdownHook(new Cleaner());
} catch (IllegalStateException e) {
// If the VM is already shutting down,
// We do not need to register shutdownHook.
}
}
मेरे अपने परीक्षण से
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
try {
Logger logger2 = Logger.getLogger("Main2");
logger2.info("Shutting down 2");
} catch (Throwable t) {
t.printStackTrace();
}
}
}));
प्रिंट
Dec 11, 2012 5:40:15 PM Main$1 run
INFO: Shutting down 2
लेकिन अगर आप इस ब्लॉक के बाहर
Logger logger1 = Logger.getLogger("Main1");
जोड़ने आप कुछ भी नहीं मिलता है।
स्रोत
2012-12-11 17:41:57
मुझे यकीन नहीं है कि यह सच है या नहीं, लेकिन आपके पास शटडाउन हुक होने के क्रम पर कोई नियंत्रण नहीं है। –
यह सच है, वे [समवर्ती] चलाते हैं (http://docs.oracle.com/javase/1.5.0/docs/guide/lang/hook-design.html) –