如何防止石英中的内存泄漏 [英] How to prevent a memory leak in quartz

查看:182
本文介绍了如何防止石英中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用石英。我的Web应用程序显然在停止时导致内存泄漏,错误是:

I'm using quartz in my project. My web application has apparently caused a memory leak when it stops, the error is :

SEVERE: A web application appears to have started a TimerThread named [Timer-12] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled. 
Jan 2, 2013 6:55:35 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [DefaultQuartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak.

我用 org.quartz.ee.servlet.QuartzInitializerServlet org.quartz.ee.servlet.QuartzInitializerListener 。我工厂的代码是:

I used org.quartz.ee.servlet.QuartzInitializerServlet and org.quartz.ee.servlet.QuartzInitializerListener. The code for my factory is:

StdSchedulerFactory factory = (StdSchedulerFactory) context.getAttribute(QuartzInitializerListener.QUARTZ_FACTORY_KEY );

和web.xml中石英的设置是:

and settings for quartz in web.xml is :

<servlet>
         <servlet-name>
             QuartzInitializer
         </servlet-name>
         <display-name>
             Quartz Initializer Servlet
         </display-name>
         <servlet-class>
             org.quartz.ee.servlet.QuartzInitializerServlet
         </servlet-class>
         <load-on-startup>
             1
         </load-on-startup>
         <init-param>
             <param-name>shutdown-on-unload</param-name>
             <param-value>true</param-value>
         </init-param>
         <init-param>
             <param-name>wait-on-shutdown</param-name>
             <param-value>true</param-value>
         </init-param>
         <init-param>
             <param-name>start-scheduler-on-load</param-name>
             <param-value>true</param-value>
         </init-param>
     </servlet>
     <context-param>
         <param-name>quartz:shutdown-on-unload</param-name>
         <param-value>true</param-value>
     </context-param>
     <context-param>
         <param-name>quartz:wait-on-shutdown</param-name>
         <param-value>true</param-value>
     </context-param>
     <context-param>
         <param-name>quartz:start-on-load</param-name>
         <param-value>true</param-value>
     </context-param>
     <listener>
         <listener-class>
             org.quartz.ee.servlet.QuartzInitializerListener
         </listener-class>
     </listener>

请帮我解决这个内存泄漏问题!

please help me to solve this memory leak !!

推荐答案

通过实施 org.quartz.InterruptableJob 您可以正确中断由servlet卸载触发的线程。

By implementing org.quartz.InterruptableJob you can properly interrupt threads triggered by servlet unloading.

@DisallowConcurrentExecution
public class Job implements InterruptableJob {

    private Thread thread;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        thread = Thread.currentThread();
        // ... do work
    }

    @Override
    public void interrupt() throws UnableToInterruptJobException {
        thread.interrupt();
        try {
            thread.join();
        } catch (InterruptedException e) {
            throw new UnableToInterruptJobException(e);
        } finally {
            // ... do cleanup
        }
    }
}

如果作业在中断之前尚未执行,则此示例可能会导致线程变量出现竞争条件错误。我将最终解决方案打开以获取建议,具体取决于目标应用程序的生命周期。如果您需要通过同一个作业实例进行并发执行,请扩充解决方案以处理多个线程并删除 @DisallowConcurrentExecution 注释。

This example may cause a race condition bug on the thread variable, if the job has not been executed before it is interrupted. I leave the final solution open for suggestions, depending on the life cycle of the target application. If you need concurrent execution through the same job instance, augment the solution to handle multiple threads and remove the @DisallowConcurrentExecution annotation.

为了使石英工作属性 org.quartz.scheduler.interruptJobsOnShutdownWithWait 必须设置为 true 。这可以通过为调度程序定义属性文件来完成,或者如果使用spring框架则通过bean引用来完成。

In order for this to work the quartz property org.quartz.scheduler.interruptJobsOnShutdownWithWait must be set to true. This can be done by defining a property file for the scheduler, or by a bean references if using spring framework.

示例 quartz.properties 档案:

org.quartz.scheduler.interruptJobsOnShutdownWithWait=true

注意如果调度程序配置为在关机时等待,则仅调度中断,从而调用 scheduler.shutdown(true )

Note that the interruption only is dispatched if the scheduler is configured to wait on shutdown, resulting in a call to scheduler.shutdown(true).

这篇关于如何防止石英中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆