Spring scheduler shutdown error

前端 未结 4 1499
青春惊慌失措
青春惊慌失措 2020-12-13 21:22

During development a SPRING based scheduler in a tomcat container, I always get this logoutput at undeploy webapp or shutdown server:

Apr 28, 2010 4:21:33 PM         


        
相关标签:
4条回答
  • 2020-12-13 21:28

    You need to add a shutdown hook - see Registering a shutdown hook in Spring 2.5.

    In your case, you probably should add a context listener to your webapp that does this (web.xml entry for the listener + implementing class).

    Use close, it's easiest.

    ((YourClass)yourObject).close();
    
    0 讨论(0)
  • 2020-12-13 21:38

    Here is my solution as none of the ones that I found online worked. This is specifically to shutdown the Quartz scheduler with Spring & Tomcat

    My explanation is here: http://forum.springsource.org/showthread.php?34672-Quartz-doesn-t-shutdown&p=370060#post370060

    Basically what the problem seemed to be is that Quartz doesn't have enough time to cleanly shutdown and the waitForJobsToCompleteOnShutdown argument doesn't seem to help. So I implemented a custom shutdown listener in the webapp, get a reference to the scheduler and shut it down manually. And then wait for 1 second before proceeding.

    public class ShutDownHook implements ServletContextListener
    {
    
        @Override
        public void contextDestroyed(ServletContextEvent arg0)
        {
            try
            {
                // Get a reference to the Scheduler and shut it down
                WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
                Scheduler scheduler = (Scheduler) context.getBean("quartzSchedulerFactory");
                scheduler.shutdown(true);
    
                // Sleep for a bit so that we don't get any errors
                Thread.sleep(1000);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        @Override
        public void contextInitialized(ServletContextEvent arg0)
        {
        }
    
    0 讨论(0)
  • 2020-12-13 21:41

    Imho this is an issue of the quartz scheduler. I filed a bug https://jira.terracotta.org/jira/browse/QTZ-192. As a workaround the sleep() solution suggested by Colin Peters works for me. To not trigger the shutdown twice one could also add the sleep to Spring's SchedulerFactoryBean:

    import org.quartz.SchedulerException;
    import org.springframework.scheduling.quartz.SchedulerFactoryBean;
    
    public class SchedulerFactoryBeanWithShutdownDelay extends SchedulerFactoryBean{
    
      @Override
      public void destroy() throws SchedulerException {
        super.destroy();
        // TODO: Ugly workaround for https://jira.terracotta.org/jira/browse/QTZ-192
        try {
          Thread.sleep( 1000 );
        } catch( InterruptedException e ) {
          throw new RuntimeException( e );
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-13 21:48

    The only way to ensure that threads are terminated, is to interrupt and join them.

    This can by done by implementing org.quartz.InterruptableJob, as described in the answer to the question How to prevent a memory leak in quartz [?].

    0 讨论(0)
提交回复
热议问题