How to prevent a memory leak in quartz

后端 未结 4 675
不知归路
不知归路 2021-02-05 20:30

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          


        
4条回答
  •  鱼传尺愫
    2021-02-05 21:08

    If you are using your own implementation of the ServletContextListener interface for your web application, you can shutdown Quartz gracefully in the contextDestroyed method. Please find below the sample code for Quartz version 2.1.7.

    Your job:

    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    public class CronJob implements Job {
        public void execute(JobExecutionContext context)
                throws JobExecutionException {
            // TODO: do you job
        }
    }
    

    Your job scheduler:

    import org.quartz.CronScheduleBuilder;
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.JobKey;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.Trigger;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
    
    public class CronJobScheduler {
    
        private static CronJobScheduler instance = new CronJobScheduler();  
        private Scheduler scheduler;
    
        private CronJobScheduler() {    
            try {
                scheduler = new StdSchedulerFactory().getScheduler();
            } catch (SchedulerException e) {
                // TODO
            }
        }
    
        public static CronJobScheduler getInstance() {
            return instance;
        }
    
        public void trigger() {
            JobKey jobKey = JobKey.jobKey("myJobName", "myJobGroup");       
            JobDetail job = JobBuilder.newJob(CronJob.class).withIdentity(jobKey).build();
    
            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("myTriggerName", "myJobGroup")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0 0 1,13 * * ?"))
                    .build();
    
            try {
                scheduler.start();
                scheduler.scheduleJob(job, trigger);
            } catch (SchedulerException e) {    
                // TODO
            }
        }
    
        public void shutdown(boolean waitForJobsToComplete) {
            try {
                scheduler.shutdown(waitForJobsToComplete);
            } catch (SchedulerException e) {
                // TODO
            }
        }
    
    }
    

    Your implementation of the ServletContextListener interface:

    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    public class MyServletContextListener implements ServletContextListener {
    
        @Override
        public void contextDestroyed(ServletContextEvent arg0) {
            CronJobScheduler.getInstance().shutdown(true);
        }
    
        @Override
        public void contextInitialized(ServletContextEvent arg0) {
            CronJobScheduler.getInstance().trigger();
        }
    
    }
    

    Your web.xml

    
        my.package.name.MyServletContextListener
    
    

提交回复
热议问题