@Override
  public void run ()
  {
    boolean lastAcquireFailed = false;

    while (!m_aHalted.get ())
    {
      try
      {
        // check if we're supposed to pause...
        synchronized (m_aSigLock)
        {
          while (m_bPaused && !m_aHalted.get ())
          {
            try
            {
              // wait until togglePause(false) is called...
              m_aSigLock.wait (1000L);
            }
            catch (final InterruptedException ignore)
            {}
          }

          if (m_aHalted.get ())
          {
            break;
          }
        }

        final int availThreadCount = m_aQSRsrcs.getThreadPool ().blockForAvailableThreads ();
        if (availThreadCount > 0)
        { // will always be true, due to semantics of
          // blockForAvailableThreads...

          ICommonsList <IOperableTrigger> triggers = null;

          long now = System.currentTimeMillis ();

          clearSignaledSchedulingChange ();
          try
          {
            triggers = m_aQSRsrcs.getJobStore ()
                                 .acquireNextTriggers (now + m_nIdleWaitTime,
                                                       Math.min (availThreadCount, m_aQSRsrcs.getMaxBatchSize ()),
                                                       m_aQSRsrcs.getBatchTimeWindow ());
            lastAcquireFailed = false;
            if (LOGGER.isDebugEnabled ())
              LOGGER.debug ("batch acquisition of " + (triggers == null ? 0 : triggers.size ()) + " triggers");
          }
          catch (final JobPersistenceException jpe)
          {
            if (!lastAcquireFailed)
            {
              m_aQS.notifySchedulerListenersError ("An error occurred while scanning for the next triggers to fire.",
                                                   jpe);
            }
            lastAcquireFailed = true;
            continue;
          }
          catch (final RuntimeException e)
          {
            if (!lastAcquireFailed)
            {
              LOGGER.error ("quartzSchedulerThreadLoop: RuntimeException " + e.getMessage (), e);
            }
            lastAcquireFailed = true;
            continue;
          }

          if (triggers != null && !triggers.isEmpty ())
          {

            now = System.currentTimeMillis ();
            final long triggerTime = triggers.get (0).getNextFireTime ().getTime ();
            long timeUntilTrigger = triggerTime - now;
            while (timeUntilTrigger > 2)
            {
              synchronized (m_aSigLock)
              {
                if (m_aHalted.get ())
                {
                  break;
                }
                if (!isCandidateNewTimeEarlierWithinReason (triggerTime, false))
                {
                  try
                  {
                    // we could have blocked a long while
                    // on 'synchronize', so we must recompute
                    now = System.currentTimeMillis ();
                    timeUntilTrigger = triggerTime - now;
                    if (timeUntilTrigger >= 1)
                      m_aSigLock.wait (timeUntilTrigger);
                  }
                  catch (final InterruptedException ignore)
                  {}
                }
              }
              if (releaseIfScheduleChangedSignificantly (triggers, triggerTime))
              {
                break;
              }
              now = System.currentTimeMillis ();
              timeUntilTrigger = triggerTime - now;
            }

            // this happens if releaseIfScheduleChangedSignificantly decided to
            // release triggers
            if (triggers.isEmpty ())
              continue;

            // set triggers to 'executing'
            ICommonsList <TriggerFiredResult> bndles = new CommonsArrayList <> ();

            boolean goAhead = true;
            synchronized (m_aSigLock)
            {
              goAhead = !m_aHalted.get ();
            }
            if (goAhead)
            {
              try
              {
                final ICommonsList <TriggerFiredResult> res = m_aQSRsrcs.getJobStore ().triggersFired (triggers);
                if (res != null)
                  bndles = res;
              }
              catch (final SchedulerException se)
              {
                m_aQS.notifySchedulerListenersError ("An error occurred while firing triggers '" + triggers + "'", se);
                // QTZ-179 : a problem occurred interacting with the triggers
                // from the db
                // we release them and loop again
                for (int i = 0; i < triggers.size (); i++)
                {
                  m_aQSRsrcs.getJobStore ().releaseAcquiredTrigger (triggers.get (i));
                }
                continue;
              }

            }

            for (int i = 0; i < bndles.size (); i++)
            {
              final TriggerFiredResult result = bndles.get (i);
              final TriggerFiredBundle bndle = result.getTriggerFiredBundle ();
              final Exception exception = result.getException ();

              if (exception instanceof RuntimeException)
              {
                LOGGER.error ("RuntimeException while firing trigger " + triggers.get (i), exception);
                m_aQSRsrcs.getJobStore ().releaseAcquiredTrigger (triggers.get (i));
                continue;
              }

              // it's possible to get 'null' if the triggers was paused,
              // blocked, or other similar occurrences that prevent it being
              // fired at this time... or if the scheduler was shutdown (halted)
              if (bndle == null)
              {
                m_aQSRsrcs.getJobStore ().releaseAcquiredTrigger (triggers.get (i));
                continue;
              }

              JobRunShell shell = null;
              try
              {
                shell = m_aQSRsrcs.getJobRunShellFactory ().createJobRunShell (bndle);
                shell.initialize (m_aQS);
              }
              catch (final SchedulerException se)
              {
                m_aQSRsrcs.getJobStore ()
                          .triggeredJobComplete (triggers.get (i),
                                                 bndle.getJobDetail (),
                                                 ECompletedExecutionInstruction.SET_ALL_JOB_TRIGGERS_ERROR);
                continue;
              }

              if (m_aQSRsrcs.getThreadPool ().runInThread (shell) == false)
              {
                // this case should never happen, as it is indicative of the
                // scheduler being shutdown or a bug in the thread pool or
                // a thread pool being used concurrently - which the docs
                // say not to do...
                LOGGER.error ("ThreadPool.runInThread() return false!");
                m_aQSRsrcs.getJobStore ()
                          .triggeredJobComplete (triggers.get (i),
                                                 bndle.getJobDetail (),
                                                 ECompletedExecutionInstruction.SET_ALL_JOB_TRIGGERS_ERROR);
              }

            }

            continue; // while (!halted)
          }
        }
        else
        { // if(availThreadCount > 0)
          // should never happen, if threadPool.blockForAvailableThreads()
          // follows contract
          continue; // while (!halted)
        }

        final long now = System.currentTimeMillis ();
        final long waitTime = now + getRandomizedIdleWaitTime ();
        final long timeUntilContinue = waitTime - now;
        synchronized (m_aSigLock)
        {
          try
          {
            if (!m_aHalted.get ())
            {
              // QTZ-336 A job might have been completed in the mean time and we
              // might have
              // missed the scheduled changed signal by not waiting for the
              // notify() yet
              // Check that before waiting for too long in case this very job
              // needs to be
              // scheduled very soon
              if (!isScheduleChanged ())
              {
                m_aSigLock.wait (timeUntilContinue);
              }
            }
          }
          catch (final InterruptedException ignore)
          {}
        }

      }
      catch (final RuntimeException re)
      {
        LOGGER.error ("Runtime error occurred in main trigger firing loop.", re);
      }
    } // while (!halted)

    // drop references to scheduler stuff to aid garbage collection...
    m_aQS = null;
    m_aQSRsrcs = null;
  }
@ Override public void run ( ) { boolean lastAcquireFailed = false ; while ( ! m_aHalted . get ( ) ) { try { synchronized ( m_aSigLock ) { while ( m_bPaused && ! m_aHalted . get ( ) ) { try { m_aSigLock . wait ( 1000L ) ; } catch ( final InterruptedException ignore ) { } } if ( m_aHalted . get ( ) ) { break ; } } final int availThreadCount = m_aQSRsrcs . getThreadPool ( ) . blockForAvailableThreads ( ) ; if ( availThreadCount > 0 ) { ICommonsList < IOperableTrigger > triggers = null ; long now = System . currentTimeMillis ( ) ; clearSignaledSchedulingChange ( ) ; try { triggers = m_aQSRsrcs . getJobStore ( ) . acquireNextTriggers ( now + m_nIdleWaitTime , Math . min ( availThreadCount , m_aQSRsrcs . getMaxBatchSize ( ) ) , m_aQSRsrcs . getBatchTimeWindow ( ) ) ; lastAcquireFailed = false ; if ( LOGGER . isDebugEnabled ( ) ) LOGGER . debug ( "batch acquisition of " + ( triggers == null ? 0 : triggers . size ( ) ) + " triggers" ) ; } catch ( final JobPersistenceException jpe ) { if ( ! lastAcquireFailed ) { m_aQS . notifySchedulerListenersError ( "An error occurred while scanning for the next triggers to fire." , jpe ) ; } lastAcquireFailed = true ; continue ; } catch ( final RuntimeException e ) { if ( ! lastAcquireFailed ) { LOGGER . error ( "quartzSchedulerThreadLoop: RuntimeException " + e . getMessage ( ) , e ) ; } lastAcquireFailed = true ; continue ; } if ( triggers != null && ! triggers . isEmpty ( ) ) { now = System . currentTimeMillis ( ) ; final long triggerTime = triggers . get ( 0 ) . getNextFireTime ( ) . getTime ( ) ; long timeUntilTrigger = triggerTime - now ; while ( timeUntilTrigger > 2 ) { synchronized ( m_aSigLock ) { if ( m_aHalted . get ( ) ) { break ; } if ( ! isCandidateNewTimeEarlierWithinReason ( triggerTime , false ) ) { try { now = System . currentTimeMillis ( ) ; timeUntilTrigger = triggerTime - now ; if ( timeUntilTrigger >= 1 ) m_aSigLock . wait ( timeUntilTrigger ) ; } catch ( final InterruptedException ignore ) { } } } if ( releaseIfScheduleChangedSignificantly ( triggers , triggerTime ) ) { break ; } now = System . currentTimeMillis ( ) ; timeUntilTrigger = triggerTime - now ; } if ( triggers . isEmpty ( ) ) continue ; ICommonsList < TriggerFiredResult > bndles = new CommonsArrayList < > ( ) ; boolean goAhead = true ; synchronized ( m_aSigLock ) { goAhead = ! m_aHalted . get ( ) ; } if ( goAhead ) { try { final ICommonsList < TriggerFiredResult > res = m_aQSRsrcs . getJobStore ( ) . triggersFired ( triggers ) ; if ( res != null ) bndles = res ; } catch ( final SchedulerException se ) { m_aQS . notifySchedulerListenersError ( "An error occurred while firing triggers '" + triggers + "'" , se ) ; for ( int i = 0 ; i < triggers . size ( ) ; i ++ ) { m_aQSRsrcs . getJobStore ( ) . releaseAcquiredTrigger ( triggers . get ( i ) ) ; } continue ; } } for ( int i = 0 ; i < bndles . size ( ) ; i ++ ) { final TriggerFiredResult result = bndles . get ( i ) ; final TriggerFiredBundle bndle = result . getTriggerFiredBundle ( ) ; final Exception exception = result . getException ( ) ; if ( exception instanceof RuntimeException ) { LOGGER . error ( "RuntimeException while firing trigger " + triggers . get ( i ) , exception ) ; m_aQSRsrcs . getJobStore ( ) . releaseAcquiredTrigger ( triggers . get ( i ) ) ; continue ; } if ( bndle == null ) { m_aQSRsrcs . getJobStore ( ) . releaseAcquiredTrigger ( triggers . get ( i ) ) ; continue ; } JobRunShell shell = null ; try { shell = m_aQSRsrcs . getJobRunShellFactory ( ) . createJobRunShell ( bndle ) ; shell . initialize ( m_aQS ) ; } catch ( final SchedulerException se ) { m_aQSRsrcs . getJobStore ( ) . triggeredJobComplete ( triggers . get ( i ) , bndle . getJobDetail ( ) , ECompletedExecutionInstruction . SET_ALL_JOB_TRIGGERS_ERROR ) ; continue ; } if ( m_aQSRsrcs . getThreadPool ( ) . runInThread ( shell ) == false ) { LOGGER . error ( "ThreadPool.runInThread() return false!" ) ; m_aQSRsrcs . getJobStore ( ) . triggeredJobComplete ( triggers . get ( i ) , bndle . getJobDetail ( ) , ECompletedExecutionInstruction . SET_ALL_JOB_TRIGGERS_ERROR ) ; } } continue ; } } else { continue ; } final long now = System . currentTimeMillis ( ) ; final long waitTime = now + getRandomizedIdleWaitTime ( ) ; final long timeUntilContinue = waitTime - now ; synchronized ( m_aSigLock ) { try { if ( ! m_aHalted . get ( ) ) { if ( ! isScheduleChanged ( ) ) { m_aSigLock . wait ( timeUntilContinue ) ; } } } catch ( final InterruptedException ignore ) { } } } catch ( final RuntimeException re ) { LOGGER . error ( "Runtime error occurred in main trigger firing loop." , re ) ; } } m_aQS = null ; m_aQSRsrcs = null ; }
<p>
The main processing loop of the <code>QuartzSchedulerThread</code>.
</p>