6

我正在实现ServletContextListener以便在我的应用服务器(GlassFish 3.1)上安排各种作业。我contextInitialized()用来安排重复任务,并 contextDestroyed()调用清理方法,例如关闭 c3p0:

public class JobScheduler implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //schedule TimerTasks
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //cancel TimerTasks
        //cleanup methods
    }
}

当我取消TimerTasks 时,我添加了在继续之前等待任何正在运行的任务完成的逻辑,以确保在我清理资源时没有任何内容仍在执行。

回答我的问题:当我取消部署我的应用程序时,我看到 GlassFish 输出中显示了其中的一两个警告:

WARNING: Input stream has been finalized or forced closed without being explicitly closed; stream instantiation reported in following stack trace
java.lang.Throwable
    at com.sun.enterprise.loader.ASURLClassLoader$SentinelInputStream.<init>(ASURLClassLoader.java:1230)
    at com.sun.enterprise.loader.ASURLClassLoader$InternalJarURLConnection.getInputStream(ASURLClassLoader.java:1338)
    at sun.misc.URLClassPath$Loader.getResource(URLClassPath.java:503)
    at sun.misc.URLClassPath.getResource(URLClassPath.java:169)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at com.google.common.base.FinalizableReferenceQueue$DecoupledLoader.loadFinalizer(FinalizableReferenceQueue.java:228)
    at com.google.common.base.FinalizableReferenceQueue.loadFinalizer(FinalizableReferenceQueue.java:155)
    at com.google.common.base.FinalizableReferenceQueue.<clinit>(FinalizableReferenceQueue.java:84)
    at com.google.common.collect.CustomConcurrentHashMap$QueueHolder.<clinit>(CustomConcurrentHashMap.java:651)
    at com.google.common.collect.CustomConcurrentHashMap$WeakValueReference.<init>(CustomConcurrentHashMap.java:1589)
    at com.google.common.collect.CustomConcurrentHashMap$Strength$3.referenceValue(CustomConcurrentHashMap.java:322)
    at com.google.common.collect.CustomConcurrentHashMap.newValueReference(CustomConcurrentHashMap.java:1731)
    at com.google.common.collect.CustomConcurrentHashMap$Segment.setValue(CustomConcurrentHashMap.java:2050)
    at com.google.common.collect.CustomConcurrentHashMap$Segment.put(CustomConcurrentHashMap.java:2430)
    at com.google.common.collect.CustomConcurrentHashMap.put(CustomConcurrentHashMap.java:3346)
    at MyProject.CacheEngine$MyCustomCache$1.apply(CacheEngine.java:244)
    at MyProject.CacheEngine$MyCustomCache$1.apply(CacheEngine.java:237)
    at com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:316)
    at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:140)
    at com.google.common.collect.ComputingConcurrentHashMap.apply(ComputingConcurrentHashMap.java:71)
    at com.google.common.collect.MapMaker$ComputingMapAdapter.get(MapMaker.java:848)

    //stacktrace of the Runnable called by TimerTask, leading up to a call to Guava ComputingMap

    at java.util.TimerThread.mainLoop(Timer.java:512)
    at java.util.TimerThread.run(Timer.java:462)

据我所知,GlassFish 抱怨一个InputStream从未明确关闭的,它是由我的 Guava 创建的计算地图之一调用的ClassLoadera打开的,该任务正在访问该地图。请注意,上面的堆栈跟踪不是异常,而是从正在运行的任务到流的实例化的实际跟踪。FinalizerMapMaker

我需要帮助的是了解为什么InputStream即使我正在等待所有任务完成,它仍然处于打开状态,以及我是否可以更好地处理它的清理。它似乎与您可以在堆栈跟踪中看到的 Guava 的计算图特别相关。

更新:如果我使用ScheduledThreadPoolExecutor而不是,我仍然会收到相同的警告TimerTask

更新 2:风滚草

4

1 回答 1

1

可能需要使用较新版本的库,以下来自 guava java docs:

FinalizableReferenceQueue() Deprecated.

FinalizableReferenceQueue 是一种清理引用的不健全机制,因为(1)它的单线程很容易过载,以及(2)它坚持运行后台线程在某些环境中是有问题的。该类计划于 2012 年 12 月删除。

于 2012-02-01T15:03:16.767 回答