Results 1 to 7 of 7

Thread: Cleanly stopping a Quartz SchedulerFactoryBean

  1. #1

    Default Cleanly stopping a Quartz SchedulerFactoryBean

    I have a quartz job that runs continuously. It looks for a new file every 5 minutes, processes it (whenever it finds it) and then keeps going forever. In the event that we want to do system maintenance, or shut it down cleanly, I want to allow the job in process to finish, and then stop the scheduler. Using the SchedulerFactoryBean with a CronTriggerBean, I'm not sure how I would allow the scheduler to know that after completing a job, it's time to quit. Here is my configuration:

    Code:
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
        <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
          <property name="jobDetail" ref="jobDetail" />
          <property name="cronExpression" value="0 0/5 * * * ?" />
        </bean>
      </property>
    </bean>
    
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass" value="my.JobLauncherDetails" />
      <property name="group" value="batch" />
      <property name="jobDataAsMap">
        <map>
          <entry key="jobName" value="myJob"/>
          <entry key="jobLocator" value-ref="jobRegistry"/>
          <entry key="jobLauncher" value-ref="jobLauncher"/>
          <entry key="fileWatcher" value-ref="myWatcher"/>
        </map>
      </property>
    </bean>
    Once I'm in the executeInternal() method of the JobLauncherDetails class, could trap something (or listen to something) to tell the scheduler to stop, since I have access to it via:

    Code:
    protected void executeInternal(JobExecutionContext context) {
      if (shutdownSignalReceivedSomehow) {
        context.getScheduler().shutdown()
      }
    }
    What I was curious to know is what others have used, or I could use to listen to. I hate to create a message queue and have it running for just this purpose. Any other ideas?

    Brian

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Not sure if I've misunderstood this but can't you just setWaitForJobsToCompleteOnShutdown(true)?

    http://static.springsource.org/sprin...tdown(boolean)
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3

    Default setWaitForJobsToCompleteOnShutdown(true)

    OK, that's a useful parameter.

    Now, if I have the scheduler running in an application, do you have any ideas how I would signal the shutdown from outside that app? These processes are started as background processes (a daemon) and run forever in a UNIX environment. If I kill the process, that's really not the same as calling scheduler.stop().

    Thanks for responding,
    Brian

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Not sure how it works in a linux environment, but on windows if you use wrapper you can trap the process being shutdown and then signal that to the ApplicationContext.
    http://wrapper.tanukisoftware.org/do...h/download.jsp
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #5
    Join Date
    Mar 2008
    Posts
    4

    Default

    Hi all,
    any news on this subject?

    I believe the propper way to ask this is: Is there any way to stop the JobRegistryBackgroundJobRunner?

    There is a method in the runner:

    Code:
    public class JobRegistryBackgroundJobRunner {
    
      ...
    
      public static void stop() {
        synchronized (JobRegistryBackgroundJobRunner.class) {
          JobRegistryBackgroundJobRunner.class.notify();
        }
      }
    
    }
    Is there any support class to invoke this? I can't find a place for a hook or something similar to stop the runner gracefully.

    Thanks in advance!
    Juan

  6. #6

    Default Update on shutting down scheduler

    I came up with a simple enough way to handle this. I created a table with all the jobName's of my Spring Batch jobs, along with a status field and a time to stop. You can open a web page, update the status to "STOP" and submit it.

    The executeInternal method checks that table, calls
    Code:
    context.getScheduler().shutdown()
    if it's stop time, and then the waitForJobsToCompleteOnShutdown setting should allow the currently running jobs to finish. Right now we get 10 threads at a time on this guy, so hopefully those threads can complete before the actual shutdown occurs, and new jobs should not start (a single threaded job takes about a minute, and the job fires every 20 seconds, so there are often 10 threads at a time).

    I'll let you know how it works once I get this running in our test environment.

    Brian

  7. #7
    Join Date
    May 2007
    Posts
    13

    Default

    I'll post a link to this thread as I had the same problem and a lot of the solutions didn't work for me

    http://forum.springsource.org/showth...060#post370060

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •