Example usage for org.springframework.batch.core Job getJobParametersIncrementer

List of usage examples for org.springframework.batch.core Job getJobParametersIncrementer

Introduction

In this page you can find the example usage for org.springframework.batch.core Job getJobParametersIncrementer.

Prototype

@Nullable
JobParametersIncrementer getJobParametersIncrementer();

Source Link

Document

If clients need to generate new parameters for the next execution in a sequence they can use this incrementer.

Usage

From source file:admin.service.SimpleJobService.java

@Override
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;/*from www  .ja  v  a  2  s.com*/
        }
    }

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

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

    if (jobExecution.isRunning()) {
        activeExecutions.add(jobExecution);
    }
    return jobExecution;

}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobService.java

/**
 * Interface method implementation//from www  .j a  v a2 s  .  c o  m
 * @see org.springframework.batch.admin.service.JobService#launch(java.lang.String, org.springframework.batch.core.JobParameters)
 */
public JobExecution launch(String jobName, JobParameters jobParameters)
        throws NoSuchJobException, JobExecutionAlreadyRunningException, JobRestartException,
        JobInstanceAlreadyCompleteException, JobParametersInvalidException {

    Job job = this.jobRegistry.getJob(jobName);
    JobExecution lastJobExecution = this.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 = this.jobLauncher.run(job, jobParameters);
    if (jobExecution.isRunning()) {
        this.activeExecutions.add(jobExecution);
    }
    return jobExecution;
}

From source file:org.springframework.batch.core.launch.support.CommandLineJobRunner.java

/**
 * @param job the job that we need to find the next parameters for
 * @return the next job parameters if they can be located
 * @throws JobParametersNotFoundException if there is a problem
 *//*from www  .  j a  va 2s  . co  m*/
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 {
        List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
        jobParameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
    }
    return jobParameters;
}

From source file:org.springframework.batch.core.launch.support.SimpleJobOperator.java

@Override
public Long startNextInstance(String jobName) throws NoSuchJobException, JobParametersNotFoundException,
        UnexpectedJobExecutionException, JobParametersInvalidException {

    logger.info("Locating parameters for next instance of job with name=" + jobName);

    Job job = jobRegistry.getJob(jobName);
    List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobName, 0, 1);

    JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
    if (incrementer == null) {
        throw new JobParametersNotFoundException("No job parameters incrementer found for job=" + jobName);
    }//  w  ww . j a v  a 2  s. com

    JobParameters parameters;
    if (lastInstances.isEmpty()) {
        parameters = incrementer.getNext(new JobParameters());
        if (parameters == null) {
            throw new JobParametersNotFoundException("No bootstrap parameters found for job=" + jobName);
        }
    } else {
        List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
        parameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
    }

    logger.info(String.format("Attempting to launch job with name=%s and parameters=%s", jobName, parameters));
    try {
        return jobLauncher.run(job, parameters).getId();
    } catch (JobExecutionAlreadyRunningException e) {
        throw new UnexpectedJobExecutionException(
                String.format(ILLEGAL_STATE_MSG, "job already running", jobName, parameters), e);
    } catch (JobRestartException e) {
        throw new UnexpectedJobExecutionException(
                String.format(ILLEGAL_STATE_MSG, "job not restartable", jobName, parameters), e);
    } catch (JobInstanceAlreadyCompleteException e) {
        throw new UnexpectedJobExecutionException(
                String.format(ILLEGAL_STATE_MSG, "job instance already complete", jobName, parameters), e);
    }

}

From source file:org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.java

private JobParameters getNextJobParameters(Job job, JobParameters additionalParameters) {
    String name = job.getName();//w  w  w  .  java  2  s  .co m
    JobParameters parameters = new JobParameters();
    List<JobInstance> lastInstances = this.jobExplorer.getJobInstances(name, 0, 1);
    JobParametersIncrementer incrementer = job.getJobParametersIncrementer();
    Map<String, JobParameter> additionals = additionalParameters.getParameters();
    if (lastInstances.isEmpty()) {
        // Start from a completely clean sheet
        if (incrementer != null) {
            parameters = incrementer.getNext(new JobParameters());
        }
    } else {
        List<JobExecution> previousExecutions = this.jobExplorer.getJobExecutions(lastInstances.get(0));
        JobExecution previousExecution = previousExecutions.get(0);
        if (previousExecution == null) {
            // Normally this will not happen - an instance exists with no executions
            if (incrementer != null) {
                parameters = incrementer.getNext(new JobParameters());
            }
        } else if (isStoppedOrFailed(previousExecution) && job.isRestartable()) {
            // Retry a failed or stopped execution
            parameters = previousExecution.getJobParameters();
            // Non-identifying additional parameters can be removed to a retry
            removeNonIdentifying(additionals);
        } else if (incrementer != null) {
            // New instance so increment the parameters if we can
            parameters = incrementer.getNext(previousExecution.getJobParameters());
        }
    }
    return merge(parameters, additionals);
}

From source file:org.springframework.cloud.dataflow.server.batch.SimpleJobService.java

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

    JobExecution jobExecution = null;// w  ww .  j  a  v a2 s . c o m

    if (jobLocator.getJobNames().contains(jobName)) {
        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 = jobLauncher.run(job, jobParameters);

        if (jobExecution.isRunning()) {
            activeExecutions.add(jobExecution);
        }
    } else {
        if (jsrJobOperator != null) {
            jobExecution = new JobExecution(jsrJobOperator.start(jobName, jobParameters.toProperties()));
        } else {
            throw new NoSuchJobException(
                    String.format("Unable to find job %s to launch", String.valueOf(jobName)));
        }
    }

    return jobExecution;
}

From source file:org.springframework.xd.dirt.plugins.job.JobLaunchRequestTransformer.java

@Transformer
public JobLaunchRequest toJobLaunchRequest(Message<?> message) {
    Job job;
    try {//  w w  w  . j  av  a  2 s .  c  o m
        job = jobRegistry.getJob(jobName);
    } catch (NoSuchJobException e) {
        throw new IllegalArgumentException("The job " + jobName + " doesn't exist. Is it deployed?");
    }
    final Object payload = message.getPayload();
    JobParameters jobParameters;

    if (logger.isDebugEnabled()) {
        logger.debug(String.format(
                "JobParameters are provided as '%s'. " + "Convertering to Spring Batch JobParameters...",
                payload.getClass().getSimpleName()));
    }

    if (payload instanceof File) {
        jobParameters = jobParametersConverter.getJobParametersForFile((File) message.getPayload());
    } else if (payload instanceof String) {
        jobParameters = jobParametersConverter.getJobParametersForJsonString((String) payload);
    } else if (payload instanceof Properties) {
        jobParameters = jobParametersConverter.getJobParameters((Properties) payload);
    } else if (payload instanceof Map<?, ?>) {
        jobParameters = jobParametersConverter.getJobParametersForMap((Map) payload);
    } else if (payload instanceof Tuple) {

        final Tuple tuple = (Tuple) payload;
        final List<Object> tupleValues = tuple.getValues();

        final Map<String, Object> map = new LinkedHashMap<String, Object>(tupleValues.size());
        for (int i = 0; i < tupleValues.size(); i++) {
            map.put(tuple.getFieldNames().get(i), tupleValues.get(i));
        }

        jobParameters = jobParametersConverter.getJobParametersForMap(map);

    } else {
        throw new IllegalArgumentException(
                "This transformer does not support payloads of type " + payload.getClass().getSimpleName());
    }

    final boolean isRestart = Boolean
            .valueOf(jobParameters.getString(ExpandedJobParametersConverter.IS_RESTART_JOB_PARAMETER_KEY));

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

    jobParameters = jobParametersConverter.removeRestartParameterIfExists(jobParameters);

    return new JobLaunchRequest(job, jobParameters);
}

From source file:org.springframework.yarn.batch.support.YarnJobLauncher.java

/**
 * Get next job parameters for a job using {@link JobParametersIncrementer}.
 *
 * @param job the job that we need to find the next parameters for
 * @param fail suppress exception/*from  w  w w  .  ja  v  a 2  s .c  o m*/
 * @return the next job parameters if they can be located
 * @throws JobParametersNotFoundException if there is a problem
 */
private JobParameters getNextJobParameters(Job job, boolean fail) 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 {
        List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
        jobParameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
    }
    return jobParameters;
}