Spring ApplicationContext.close()不会关闭应用程序

时间:2018-08-01 07:46:06

标签: java spring

我正在尝试关闭我的应用程序(这是一个jar,是手动运行的)

我已阅读:http://forum.spring.io/forum/spring-projects/container/98741-how-to-applicationcontext-memory-leaks-in-spring-3-0-x

Spring ApplicationContext - Resource leak: 'context' is never closed

就像我在用3.2.18弹簧

我的代码看起来像

private static ConfigurableApplicationContext parentContext ;
    private static ConfigurableApplicationContext processContext ;
    private final String  processApplicationContextChild; // filename: childContext.xml

    public void run() {

        log.debug("Loading parent context");

            parentContext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
            parentContext.registerShutdownHook();

            try {
                runProcess(parentContext);
            } catch (Throwable e) {
                log.error("Unexpected error: " + e.getMessage(), e);


            } finally {
                log.info("Closing Core Context ");
                if (parentContext.isActive()){
                    parentContext.close();

                }
                log.info("Core Closed");

            }


    }

    protected void runProcess(ConfigurableApplicationContext parentContext) {
        log.info("Loading " + Arrays.asList(processApplicationContextChild));
         processContext = new ClassPathXmlApplicationContext(processApplicationContextChild, parentContext);
        processContext.registerShutdownHook();
        Runnable processor = (Runnable) processContext.getBean("processorRunnable", Runnable.class);

        try {
            processor.run();
        } finally {
            log.info("Closing child  Context ");
            processContext.close();
            log.info(" Child Closed");
        }
    }

在打印出“ Core Closed”之后。它只是挂着,什么也不做。我希望JVM退出。

在某些资源中,子项或父项未正确关闭。我如何查看该资源。或这里的问题可能是什么。 当我调试时,在每个.close()调用上,上下文仍然是一个带有close = true和isActive = false的实例。

我什至使它们成为非静态,并尝试使用资源块。

1 个答案:

答案 0 :(得分:0)

问题是资源泄漏,在此描述了解决方案 RestEasyClient JAX RS possible resource leak if not done correctly