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

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

Introduction

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

Prototype

public JobExecutionAlreadyRunningException(String msg) 

Source Link

Usage

From source file:com.rsone.util.JobLauncherSynchronizer.java

@Before("execution(* org.springframework.batch..JobLauncher+.*(..)) && args(job,..)")
public void checkJobBeforeLaunch(Job job) throws JobExecutionAlreadyRunningException {
    String jobName = job.getName();
    logger.debug("Checking for synchronization on Job: " + jobName);
    if (!jobNames.contains(jobName)) {
        logger.debug("Not synchronizing Job: " + jobName);
        return;/*from www . j av a 2s.c o  m*/
    }
    Set<JobExecution> running = jobExplorer.findRunningJobExecutions(jobName);
    if (!running.isEmpty()) {
        throw new JobExecutionAlreadyRunningException("An instance of this job is already active: " + jobName);
    }
    logger.debug("Job checked and no duplicates detected: " + jobName);
}

From source file:com.rsone.util.JobLauncherSynchronizer.java

@AfterReturning(value = "execution(* org.springframework.batch..JobRepository+.createJobExecution(..)) && args(jobName,..)", returning = "jobExecution")
public void checkJobDuringLaunch(String jobName, JobExecution jobExecution)
        throws JobExecutionAlreadyRunningException {
    logger.debug("Re-checking for synchronization on JobExecution: " + jobExecution);
    if (!jobNames.contains(jobName)) {
        logger.debug("Not re-checking for synchronization of Job: " + jobName);
        return;//from   w  ww . ja v a 2  s. co m
    }
    Set<JobExecution> running = jobExplorer.findRunningJobExecutions(jobName);
    if (running.size() > 1) {
        jobExecution.setEndTime(new Date());
        jobExecution.upgradeStatus(BatchStatus.ABANDONED);
        jobExecution.setExitStatus(jobExecution.getExitStatus().and(ExitStatus.NOOP)
                .addExitDescription("Not executed because another execution was detected for the same Job."));
        jobRepository.update(jobExecution);
        throw new JobExecutionAlreadyRunningException("An instance of this job is already active: " + jobName);
    }
}

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

/**
 * Interface method implementation. //  w ww. ja va 2  s . co  m
 * @see org.springframework.batch.admin.service.JobService#abandon(java.lang.Long)
 */
public JobExecution abandon(Long jobExecutionId)
        throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException {
    JobExecution jobExecution = getJobExecution(jobExecutionId);
    if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
        throw new JobExecutionAlreadyRunningException(
                "JobExecution is running or complete and therefore cannot be aborted");
    }

    LOGGER.info("Aborting job execution: " + jobExecution);
    jobExecution.upgradeStatus(BatchStatus.ABANDONED);
    jobExecution.setEndTime(new Date());
    jobRepository.update(jobExecution);
    return jobExecution;
}

From source file:com.xchanging.support.batch.admin.service.SimpleJobService.java

public JobExecution abandon(Long jobExecutionId)
        throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException {

    JobExecution jobExecution = getJobExecution(jobExecutionId);
    if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
        throw new JobExecutionAlreadyRunningException(
                "JobExecution is running or complete and therefore cannot be aborted");
    }//from w  w w .j  av  a 2s  .c o m

    logger.info("Aborting job execution: " + jobExecution);
    jobExecution.upgradeStatus(BatchStatus.ABANDONED);
    jobExecution.setEndTime(new Date());
    jobRepository.update(jobExecution);
    return jobExecution;

}

From source file:admin.service.SimpleJobService.java

@Override
public JobExecution abandon(Long jobExecutionId)
        throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException {

    JobExecution jobExecution = getJobExecution(jobExecutionId);
    if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
        throw new JobExecutionAlreadyRunningException(
                "JobExecution is running or complete and therefore cannot be aborted");
    }/*from  w  w w  . j  a  va2  s  .  c o m*/

    logger.info("Aborting job execution: " + jobExecution);
    jobExecution.upgradeStatus(BatchStatus.ABANDONED);
    jobExecution.setEndTime(new Date());
    jobRepository.update(jobExecution);
    return jobExecution;

}

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

/**
 * Run the provided job with the given {@link JobParameters}. The
 * {@link JobParameters} will be used to determine if this is an execution
 * of an existing job instance, or if a new one should be created.
 *
 * @param job the job to be run./*  w ww  . j a v  a2 s.com*/
 * @param jobParameters the {@link JobParameters} for this particular
 * execution.
 * @return JobExecutionAlreadyRunningException if the JobInstance already
 * exists and has an execution already running.
 * @throws JobRestartException if the execution would be a re-start, but a
 * re-start is either not allowed or not needed.
 * @throws JobInstanceAlreadyCompleteException if this instance has already
 * completed successfully
 * @throws JobParametersInvalidException
 */
@Override
public JobExecution run(final Job job, final JobParameters jobParameters)
        throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
        JobParametersInvalidException {

    Assert.notNull(job, "The Job must not be null.");
    Assert.notNull(jobParameters, "The JobParameters must not be null.");

    final JobExecution jobExecution;
    JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
    if (lastExecution != null) {
        if (!job.isRestartable()) {
            throw new JobRestartException("JobInstance already exists and is not restartable");
        }
        /*
         * validate here if it has stepExecutions that are UNKNOWN, STARTING, STARTED and STOPPING
         * retrieve the previous execution and check
         */
        for (StepExecution execution : lastExecution.getStepExecutions()) {
            BatchStatus status = execution.getStatus();
            if (status.isRunning() || status == BatchStatus.STOPPING) {
                throw new JobExecutionAlreadyRunningException(
                        "A job execution for this job is already running: " + lastExecution);
            } else if (status == BatchStatus.UNKNOWN) {
                throw new JobRestartException("Cannot restart step [" + execution.getStepName()
                        + "] 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.");
            }
        }
    }

    // Check the validity of the parameters before doing creating anything
    // in the repository...
    job.getJobParametersValidator().validate(jobParameters);

    /*
     * There is a very small probability that a non-restartable job can be
     * restarted, but only if another process or thread manages to launch
     * <i>and</i> fail a job execution for this instance between the last
     * assertion and the next method returning successfully.
     */
    jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);

    try {
        taskExecutor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters
                            + "]");
                    job.execute(jobExecution);
                    logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters
                            + "] and the following status: [" + jobExecution.getStatus() + "]");
                } catch (Throwable t) {
                    logger.info("Job: [" + job
                            + "] failed unexpectedly and fatally with the following parameters: ["
                            + jobParameters + "]", t);
                    rethrow(t);
                }
            }

            private void rethrow(Throwable t) {
                if (t instanceof RuntimeException) {
                    throw (RuntimeException) t;
                } else if (t instanceof Error) {
                    throw (Error) t;
                }
                throw new IllegalStateException(t);
            }
        });
    } catch (TaskRejectedException e) {
        jobExecution.upgradeStatus(BatchStatus.FAILED);
        if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
            jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e));
        }
        jobRepository.update(jobExecution);
    }

    return jobExecution;
}

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

@Override
public JobExecution abandon(long jobExecutionId)
        throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException {
    JobExecution jobExecution = findExecutionById(jobExecutionId);

    if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
        throw new JobExecutionAlreadyRunningException(
                "JobExecution is running or complete and therefore cannot be aborted");
    }//from w  ww  .j a v  a2 s  .  c o m

    logger.info("Aborting job execution: " + jobExecution);
    jobExecution.upgradeStatus(BatchStatus.ABANDONED);
    jobExecution.setEndTime(new Date());
    jobRepository.update(jobExecution);

    return jobExecution;
}

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.");

    /*//from 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;

}

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

@Override
public JobExecution abandon(Long jobExecutionId)
        throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException {

    JobExecution jobExecution = getJobExecution(jobExecutionId);
    if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
        throw new JobExecutionAlreadyRunningException(
                "JobExecution is running or complete and therefore cannot be aborted");
    }//from  ww w .j  av a2 s  .co m

    logger.info("Aborting job execution: " + jobExecution);

    Collection<String> jsrJobNames = getJsrJobNames();

    JobInstance jobInstance = jobExecution.getJobInstance();
    if (jsrJobOperator != null && jsrJobNames.contains(jobInstance.getJobName())) {
        jsrJobOperator.abandon(jobExecutionId);
        jobExecution = getJobExecution(jobExecutionId);
    } else {
        jobExecution.upgradeStatus(BatchStatus.ABANDONED);
        jobExecution.setEndTime(new Date());
        jobRepository.update(jobExecution);
    }

    return jobExecution;

}