public void schedule(TimerEntity timer) {
    Date duedate = timer.getDuedate();
    if (duedate == null) {
      throw new ActivitiException("duedate is null");
    }

    CommandContext commandContext = Context.getCommandContext();

    commandContext.getDbSqlSession().insert(timer);

    // Check if this timer fires before the next time the job executor will check for new timers to
    // fire.
    // This is highly unlikely because normally waitTimeInMillis is 5000 (5 seconds)
    // and timers are usually set further in the future

    JobExecutor jobExecutor = Context.getProcessEngineConfiguration().getJobExecutor();
    int waitTimeInMillis = jobExecutor.getWaitTimeInMillis();
    if (duedate.getTime() < (ClockUtil.getCurrentTime().getTime() + waitTimeInMillis)) {
      // then notify the job executor.
      commandContext
          .getTransactionContext()
          .addTransactionListener(
              TransactionState.COMMITTED, new MessageAddedNotification(jobExecutor));
    }
  }
 protected void closeProcessEngine() {
   ProcessEngines.unregister(activitiProcessEngine);
   JobExecutor jobExecutor = processEngineConfiguration.getJobExecutor();
   if (jobExecutor != null && jobExecutor.isActive()) {
     jobExecutor.shutdown();
   }
 }
  public void waitForJobExecutorOnCondition(
      long maxMillisToWait, long intervalMillis, Callable<Boolean> condition) {
    JobExecutor jobExecutor = processEngineConfiguration.getJobExecutor();
    jobExecutor.start();

    try {
      Timer timer = new Timer();
      InteruptTask task = new InteruptTask(Thread.currentThread());
      timer.schedule(task, maxMillisToWait);
      boolean conditionIsViolated = true;
      try {
        while (conditionIsViolated) {
          Thread.sleep(intervalMillis);
          conditionIsViolated = !condition.call();
        }
      } catch (InterruptedException e) {
      } catch (Exception e) {
        throw new ActivitiException("Exception while waiting on condition: " + e.getMessage(), e);
      } finally {
        timer.cancel();
      }
      if (conditionIsViolated) {
        throw new ActivitiException("time limit of " + maxMillisToWait + " was exceeded");
      }

    } finally {
      jobExecutor.shutdown();
    }
  }
  public void waitForJobExecutorToProcessAllJobs(long maxMillisToWait, long intervalMillis) {
    JobExecutor jobExecutor = processEngineConfiguration.getJobExecutor();
    jobExecutor.start();

    try {
      Timer timer = new Timer();
      InteruptTask task = new InteruptTask(Thread.currentThread());
      timer.schedule(task, maxMillisToWait);
      boolean areJobsAvailable = true;
      try {
        while (areJobsAvailable && !task.isTimeLimitExceeded()) {
          Thread.sleep(intervalMillis);
          areJobsAvailable = areJobsAvailable();
        }
      } catch (InterruptedException e) {
      } finally {
        timer.cancel();
      }
      if (areJobsAvailable) {
        throw new ActivitiException("time limit of " + maxMillisToWait + " was exceeded");
      }

    } finally {
      jobExecutor.shutdown();
    }
  }
 public void setActivateJobExecutor(boolean activateJobExecutor) {
   this.activateJobExecutor = activateJobExecutor;
   if (isActive) {
     log.info("Process engine configuration 'activateJobExecutor' set to " + activateJobExecutor);
     JobExecutor jobExecutor = processEngineConfiguration.getJobExecutor();
     if (activateJobExecutor && !jobExecutor.isActive()) {
       jobExecutor.start();
     } else if (!activateJobExecutor && jobExecutor.isActive()) {
       jobExecutor.shutdown();
     }
   }
 }
  protected void initProcessEngine() {
    activitiProcessEngine = processEngineConfiguration.buildProcessEngine();
    log.info(
        "Using IdGenerator["
            + processEngineConfiguration.getIdGenerator().getClass().getName()
            + "]");

    JobExecutor jobExecutor = processEngineConfiguration.getJobExecutor();
    jobExecutor.setLockTimeInMillis(jobExecutor_lockTimeInMillis);
    jobExecutor.setMaxJobsPerAcquisition(jobExecutor_maxJobsPerAcquisition);
    jobExecutor.setWaitTimeInMillis(jobExecutor_waitTimeInMillis);
  }