public JobExecution launch(String jobName, JobParameters jobParameters)
      throws NoSuchJobException, JobExecutionAlreadyRunningException, JobRestartException,
          JobInstanceAlreadyCompleteException, JobParametersInvalidException {

    Job job = jobLocator.getJob(jobName);

    JobExecution lastJobExecution = jobRepository.getLastJobExecution(jobName, jobParameters);
    boolean restart = false;
    if (lastJobExecution != null) {
      BatchStatus status = lastJobExecution.getStatus();
      if (status.isUnsuccessful() && status != BatchStatus.ABANDONED) {
        restart = true;
      }
    }

    if (job.getJobParametersIncrementer() != null && !restart) {
      jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
    }

    JobExecution jobExecution = jobLauncher.run(job, jobParameters);

    if (jobExecution.isRunning()) {
      activeExecutions.add(jobExecution);
    }
    return jobExecution;
  }
 private JobParameters getNextJobParameters(Job job) throws JobParametersNotFoundException {
   String jobIdentifier = job.getName();
   JobParameters jobParameters;
   List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1);
   JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
   if (incrementer == null) {
     throw new JobParametersNotFoundException(
         "No job parameters incrementer found for job=" + jobIdentifier);
   }
   if (lastInstances.isEmpty()) {
     jobParameters = incrementer.getNext(new JobParameters());
     if (jobParameters == null) {
       throw new JobParametersNotFoundException(
           "No bootstrap parameters found from incrementer for job=" + jobIdentifier);
     }
   } else {
     jobParameters = incrementer.getNext(lastInstances.get(0).getJobParameters());
   }
   return jobParameters;
 }