2010-07-22 70 views
7

我取消部署使用從Drools規則引擎登錄其自己的線程的異步記錄器的Java EE應用程序。我使用它來記錄規則引擎所做的決定,但我不能讓它對吞吐量產生影響,因此它必須在自己的線程中運行。如何在取消部署Java EE應用程序時停止線程?

當我解除表示它沒有正確關閉時,我得到異常頁面。

while(true){ 
    log(something) 
    sleep(someTime); 
} 

它可能沒有時間作出反應時,正確爲睡覺和取消部署啓動:自記錄儀就是這樣的一個線程,可能是真實的。不幸的是,我不能從記錄器獲取線程本身,所以我無法通過@PreDestroy方法加入它。如何取消部署時如何避免異常?

的Excpetions:

[#|2010-07-19T15:50:10.123+0200|WARNING|sun-appserver2.1|javax.enterprise.system.core.classloading|_ThreadID=24;_ThreadName=Thread-258;_RequestID=22a7d379-0813-4248-9095-3fba7f4cb95a;|LDR5206: EJBClassLoader EJBClassLoader : 
doneCalled = true 
doneSnapshot = EJBClassLoader.done() called ON EJBClassLoader : 
urlSet = [URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/jaxp-api-1.3.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/mvel2-2.0.10.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/activation-1.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/stax-1.2.0.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/jms-1.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-lang-2.4.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/joda-time-1.6.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/drools-compiler-5.0.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-email-1.2.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-beanutils-core-1.8.0.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/Mailforwarder-pojo-1.0.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/xml-apis-1.0.b2.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-logging-1.1.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/janino-2.5.15.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-digester-1.8.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/drools-api-5.0.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-net-2.0.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-lang-2.5.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/core-3.4.2.v_883_R34x.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/xpp3_min-1.1.4c.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/stax-api-1.0.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-configuration-1.6.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-collections-3.2.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/xstream-1.3.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/commons-beanutils-1.7.0.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/core-pojo-1.0.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/core-common-1.0.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/xml-writer-0.2.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/antlr-runtime-3.1.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/mail-1.4.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/activation.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/lib/drools-core-5.0.1.jar, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/applications/j2ee-apps/mailforwarder/Mailforwarder-ejbjar-1.0_jar/, URLEntry : file:/home/arne/env/runtime/glassfish/domains/domain1/generated/ejb/j2ee-apps/mailforwarder/] 
doneCalled = false 
Parent -> EJBClassLoader : 
urlSet = [] 
doneCalled = false 
Parent -> [email protected] 


AT Mon Jul 19 15:47:21 CEST 2010 
BY :com.sun.enterprise.loader.EJBClassLoader.printStackTraceToString(EJBClassLoader.java:813) 
com.sun.enterprise.loader.EJBClassLoader.done(EJBClassLoader.java:173) 
com.sun.enterprise.server.AbstractLoader.done(AbstractLoader.java:355) 
com.sun.enterprise.server.ApplicationLoader.unload(ApplicationLoader.java:268) 
com.sun.enterprise.server.TomcatApplicationLoader.unload(TomcatApplicationLoader.java:218) 
com.sun.enterprise.server.ExtendedApplicationLoader.unload(ExtendedApplicationLoader.java:263) 
com.sun.enterprise.server.ApplicationManager.applicationUndeployed(ApplicationManager.java:525) 
com.sun.enterprise.server.ApplicationManager.applicationUndeployed(ApplicationManager.java:703) 
com.sun.enterprise.admin.event.AdminEventMulticaster.invokeApplicationDeployEventListener(AdminEventMulticaster.java:961) 
com.sun.enterprise.admin.event.AdminEventMulticaster.handleApplicationDeployEvent(AdminEventMulticaster.java:943) 
com.sun.enterprise.admin.event.AdminEventMulticaster.processEvent(AdminEventMulticaster.java:467) 
com.sun.enterprise.admin.event.AdminEventMulticaster.multicastEvent(AdminEventMulticaster.java:182) 
com.sun.enterprise.admin.server.core.DeploymentNotificationHelper.multicastEvent(DeploymentNotificationHelper.java:308) 
com.sun.enterprise.deployment.phasing.DeploymentServiceUtils.multicastEvent(DeploymentServiceUtils.java:231) 
com.sun.enterprise.deployment.phasing.ServerDeploymentTarget.sendStopEvent(ServerDeploymentTarget.java:332) 
com.sun.enterprise.deployment.phasing.ApplicationStopPhase.runPhase(ApplicationStopPhase.java:136) 
com.sun.enterprise.deployment.phasing.DeploymentPhase.executePhase(DeploymentPhase.java:108) 
com.sun.enterprise.deployment.phasing.PEDeploymentService.executePhases(PEDeploymentService.java:966) 
com.sun.enterprise.deployment.phasing.PEDeploymentService.undeploy(PEDeploymentService.java:333) 
com.sun.enterprise.deployment.phasing.PEDeploymentService.undeploy(PEDeploymentService.java:308) 
com.sun.enterprise.admin.mbeans.ApplicationsConfigMBean.undeploy(ApplicationsConfigMBean.java:667) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
java.lang.reflect.Method.invoke(Method.java:597) 
com.sun.enterprise.admin.MBeanHelper.invokeOperationInBean(MBeanHelper.java:390) 
com.sun.enterprise.admin.MBeanHelper.invokeOperationInBean(MBeanHelper.java:373) 
com.sun.enterprise.admin.config.BaseConfigMBean.invoke(BaseConfigMBean.java:477) 
com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836) 
com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761) 
sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
java.lang.reflect.Method.invoke(Method.java:597) 
com.sun.enterprise.admin.util.proxy.ProxyClass.invoke(ProxyClass.java:90) 
$Proxy1.invoke(Unknown Source) 
com.sun.enterprise.admin.server.core.jmx.SunoneInterceptor.invoke(SunoneInterceptor.java:304) 
com.sun.enterprise.interceptor.DynamicInterceptor.invoke(DynamicInterceptor.java:170) 
com.sun.enterprise.deployment.autodeploy.AutoDeployer.invokeUndeploymentService(AutoDeployer.java:903) 
com.sun.enterprise.deployment.autodeploy.AutoDeployer.undeployJavaEEArchive(AutoDeployer.java:399) 
com.sun.enterprise.deployment.autodeploy.AutoDeployer.undeployApplication(AutoDeployer.java:381) 
com.sun.enterprise.deployment.autodeploy.AutoDeployer.undeployAll(AutoDeployer.java:315) 
com.sun.enterprise.deployment.autodeploy.AutoDeployControllerImpl$AutoDeployTask.run(AutoDeployControllerImpl.java:375) 
java.util.TimerThread.mainLoop(Timer.java:512) 
java.util.TimerThread.run(Timer.java:462) 
Parent -> EJBClassLoader : 
urlSet = [] 
doneCalled = false 
Parent -> [email protected] 

was requested to find resource META-INF/services/javax.xml.datatype.DatatypeFactory after done was invoked from the following stack trace 
java.lang.Throwable 
     at com.sun.enterprise.loader.EJBClassLoader.findResource(EJBClassLoader.java:459) 
     at java.lang.ClassLoader.getResource(ClassLoader.java:978) 
     at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:1168) 
     at com.sun.enterprise.loader.EJBClassLoader.getResourceAsStream(EJBClassLoader.java:796) 
     at javax.xml.datatype.SecuritySupport$4.run(SecuritySupport.java:92) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at javax.xml.datatype.SecuritySupport.getResourceAsStream(SecuritySupport.java:85) 
     at javax.xml.datatype.FactoryFinder.findJarServiceProvider(FactoryFinder.java:250) 
     at javax.xml.datatype.FactoryFinder.find(FactoryFinder.java:223) 
     at javax.xml.datatype.DatatypeFactory.newInstance(DatatypeFactory.java:131) 
     at com.thoughtworks.xstream.converters.extended.DurationConverter.<init>(DurationConverter.java:33) 
     at sun.reflect.GeneratedConstructorAccessor70.newInstance(Unknown Source) 
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
     at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
     at com.thoughtworks.xstream.XStream.dynamicallyRegisterConverter(XStream.java:725) 
     at com.thoughtworks.xstream.XStream.setupConverters(XStream.java:696) 
     at com.thoughtworks.xstream.XStream.<init>(XStream.java:445) 
     at com.thoughtworks.xstream.XStream.<init>(XStream.java:385) 
     at com.thoughtworks.xstream.XStream.<init>(XStream.java:323) 
     at org.drools.audit.WorkingMemoryFileLogger.writeToDisk(WorkingMemoryFileLogger.java:120) 
     at org.drools.audit.ThreadedWorkingMemoryFileLogger.writeToDisk(ThreadedWorkingMemoryFileLogger.java:38) 
     at org.drools.audit.ThreadedWorkingMemoryFileLogger$Writer.run(ThreadedWorkingMemoryFileLogger.java:50) 
     at java.lang.Thread.run(Thread.java:619) 
|#] 

我在這個問題上,遺憾對於想我沒有做足夠清晰。我問了一個新的question here

回答

12

而不是使用ScheduledExecutorService#scheduleAtFixedRate()而不是老式的Thread#sleep()。您可以使用ServletContextListener在webapp的啓動時運行它,並在webapp關閉時停止它。

@WebListener 
public class Config implements ServletContextListener { 

    private ScheduledExecutorService scheduler; 

    @Override 
    public void contextInitialized(ServletContextEvent event) { 
     scheduler = Executors.newSingleThreadScheduledExecutor(); 
     scheduler.scheduleAtFixedRate(new Task(), 0, 1, TimeUnit.MINUTES); // Schedule to run every minute. 
    } 

    @Override 
    public void contextDestroyed(ServletContextEvent event) { 
     scheduler.shutdown(); // Important! This stops the thread. 
    } 

} 

哪裏Task可以是這樣的:

public class Task implements Runnable { 

    @Override 
    public void run() { 
     log(something); 
    } 

} 

如果您的環境不支持Servlet 3.0 @WebListener,然後將其註冊爲web.xml如下讓它運行:

<listener> 
    <listener-class>com.example.Config</listener-class> 
</listener> 
+0

感謝您的回答。我沒有自己實現記錄器,它帶有Drools規則引擎。所以我不能改變實現。我也無法獲得它用於執行日誌記錄的線程。這沒有吸氣。我只是研究了drools類的源代碼,並看到了使用sleep函數的實現。所以我的問題依然存在:有什麼方法可以知道應用何時解除部署? finallize來得太晚,@PreDestroy在容器正常操作期間被調用。應用程序如何知道它何時未被部署? – Arne 2010-07-23 07:37:30

+0

澄清:異常說:「輸入流已被確定或強制關閉,沒有明確關閉」。我的問題是:我如何取消部署時關閉它?關閉是用close()方法完成的,但是什麼時候? – Arne 2010-07-23 07:49:51

相關問題