title: Wait in Main Thread for Quartz Scheduler to Finish
body: <p>I have a Java application that makes use of a Quartz Scheduler in the guise of a <a href="http://static.springsource.org/spring/docs/3.2.2.RELEASE/javadoc-api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html" rel="noreferrer">SchedulerFactoryBean</a>. The <code>main()</code> method gets the application context, retrieves the root bean, and commences scheduling jobs.</p>
  
  <p>The issue is that the Scheduler runs in its own thread, so when the main thread is done submitting jobs, it returns and the Scheduler goes on without it. When the Scheduler is finally done (or even if you explicitly call <code>shutdown()</code> on it), the application just hangs there for all eternity.</p>
  
  <p>I have two solutions:</p>
  
  <ol>
  <li>Keep track of the job/trigger count, incrementing it whenever you add a job to the Scheduler. Attach a simple <a href="http://quartz-scheduler.org/api/2.0.0/org/quartz/SchedulerListener.html" rel="noreferrer">SchedulerListener</a> to the Scheduler that decrements this count with every call to <code>triggerFinalized()</code>, and set up a <code>while</code> loop with a <code>Thread.sleep()</code> inside it that constantly checks to see if the count has hit 0. When it does, it will return up to the <code>main()</code> method and the application will exit normally.</li>
  <li>Take the custom SchedulerListener from option 1, and keep track of the job count inside of it. Increment for every call to <code>jobAdded()</code>, and decrement for every call to <code>triggerFinalized()</code>. When the count hits 0, call <code>shutdown()</code> on the Scheduler (or not, it doesn't actually matter) and then call <code>System.exit(0)</code>.</li>
  </ol>
  
  <p>I have implemented both of these independently in turn, so I know they both actually function. The problem is that they are both terrible. An infinite <code>while</code> loop polling a value? <code>System.exit(0)</code>? Bleargh.</p>
  
  <p>Does someone have a better way, or are these seriously my only options here?</p>
  
  <p><strong>Edit:</strong> While thinking about this on the way home, I came to the conclusion that this may be caused by the fact that I'm using SchedulerFactoryBean. This auto-starts when Spring initializes the application context - that seems to put it outside the scope of the main thread. If I went with a slightly different Scheduler that I manually initialized and called <code>start()</code> on in the code, would this run the Scheduler in the main thread, thus blocking it until the Scheduler completes running all jobs? Or would I still have this problem?</p>
  
  <p><strong>Edit:</strong> Son of a...<a href="http://quartz-scheduler.org/documentation/quartz-2.x/examples/Example1" rel="noreferrer">http://quartz-scheduler.org/documentation/quartz-2.x/examples/Example1</a> </p>
  
  <blockquote>
    <p>To let the program have an opportunity to run the job, we then sleep for 90 seconds. The scheduler is running in the background and should fire off the job during those 90 seconds.</p>
  </blockquote>
  
  <p>Apparently, that will not work, because the scheduler seems to always run in the background.</p>