Example usage for org.springframework.batch.core.repository JobInstanceAlreadyCompleteException JobInstanceAlreadyCompleteException

List of usage examples for org.springframework.batch.core.repository JobInstanceAlreadyCompleteException JobInstanceAlreadyCompleteException

Introduction

In this page you can find the example usage for org.springframework.batch.core.repository JobInstanceAlreadyCompleteException JobInstanceAlreadyCompleteException.

Prototype

public JobInstanceAlreadyCompleteException(String string) 

Source Link

Usage

From source file:org.springframework.batch.core.repository.support.SimpleJobRepository.java

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

    Assert.notNull(jobName, "Job name must not be null.");
    Assert.notNull(jobParameters, "JobParameters must not be null.");

    /*/*  ww w. j av a 2s  .c o  m*/
     * Find all jobs matching the runtime information.
     *
     * If this method is transactional, and the isolation level is
     * REPEATABLE_READ or better, another launcher trying to start the same
     * job in another thread or process will block until this transaction
     * has finished.
     */

    JobInstance jobInstance = jobInstanceDao.getJobInstance(jobName, jobParameters);
    ExecutionContext executionContext;

    // existing job instance found
    if (jobInstance != null) {

        List<JobExecution> executions = jobExecutionDao.findJobExecutions(jobInstance);

        // check for running executions and find the last started
        for (JobExecution execution : executions) {
            if (execution.isRunning() || execution.isStopping()) {
                throw new JobExecutionAlreadyRunningException(
                        "A job execution for this job is already running: " + jobInstance);
            }
            BatchStatus status = execution.getStatus();
            if (status == BatchStatus.UNKNOWN) {
                throw new JobRestartException("Cannot restart job from UNKNOWN status. "
                        + "The last execution ended with a failure that could not be rolled back, "
                        + "so it may be dangerous to proceed. Manual intervention is probably necessary.");
            }
            if (execution.getJobParameters().getParameters().size() > 0
                    && (status == BatchStatus.COMPLETED || status == BatchStatus.ABANDONED)) {
                throw new JobInstanceAlreadyCompleteException(
                        "A job instance already exists and is complete for parameters=" + jobParameters
                                + ".  If you want to run this job again, change the parameters.");
            }
        }
        executionContext = ecDao.getExecutionContext(jobExecutionDao.getLastJobExecution(jobInstance));
    } else {
        // no job found, create one
        jobInstance = jobInstanceDao.createJobInstance(jobName, jobParameters);
        executionContext = new ExecutionContext();
    }

    JobExecution jobExecution = new JobExecution(jobInstance, jobParameters, null);
    jobExecution.setExecutionContext(executionContext);
    jobExecution.setLastUpdated(new Date(System.currentTimeMillis()));

    // Save the JobExecution so that it picks up an ID (useful for clients
    // monitoring asynchronous executions):
    jobExecutionDao.saveJobExecution(jobExecution);
    ecDao.saveExecutionContext(jobExecution);

    return jobExecution;

}