我正在实现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
}
}
当我取消TimerTask
s 时,我添加了在继续之前等待任何正在运行的任务完成的逻辑,以确保在我清理资源时没有任何内容仍在执行。
回答我的问题:当我取消部署我的应用程序时,我看到 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 创建的计算地图之一调用的ClassLoader
a打开的,该任务正在访问该地图。请注意,上面的堆栈跟踪不是异常,而是从正在运行的任务到流的实例化的实际跟踪。Finalizer
MapMaker
我需要帮助的是了解为什么InputStream
即使我正在等待所有任务完成,它仍然处于打开状态,以及我是否可以更好地处理它的清理。它似乎与您可以在堆栈跟踪中看到的 Guava 的计算图特别相关。
更新:如果我使用ScheduledThreadPoolExecutor
而不是,我仍然会收到相同的警告TimerTask
更新 2:风滚草