Example usage for org.springframework.batch.core JobExecution setEndTime

List of usage examples for org.springframework.batch.core JobExecution setEndTime

Introduction

In this page you can find the example usage for org.springframework.batch.core JobExecution setEndTime.

Prototype

public void setEndTime(Date endTime) 

Source Link

Usage

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

/**
 * Returns a copy of {@link JobExecution}, by adding new Objects for every field(no references are passed)
 * //  w w  w  . j a v a  2 s. c  o m
 * @param original JobExecution to be copied
 * @return JobExecution copy
 */
private static JobExecution copy(JobExecution original) {
    JobInstance jobInstance = original.getJobInstance();
    JobExecution copy;
    if (jobInstance == null) {
        copy = new JobExecution(original.getId());
    }
    copy = new JobExecution(jobInstance, original.getId());
    if (original.getStartTime() != null) {
        copy.setStartTime((Date) original.getStartTime().clone());
    }
    if (original.getEndTime() != null) {
        copy.setEndTime((Date) original.getEndTime().clone());
    }
    if (original.getStatus() != null) {
        copy.setStatus(BatchStatus.valueOf(original.getStatus().name()));
    }
    if (original.getExitStatus() != null) {
        copy.setExitStatus(new ExitStatus(original.getExitStatus().getExitCode(),
                original.getExitStatus().getExitDescription()));
    }
    if (original.getCreateTime() != null) {
        copy.setCreateTime((Date) original.getCreateTime().clone());
    }
    if (original.getLastUpdated() != null) {
        copy.setLastUpdated((Date) original.getLastUpdated().clone());
    }
    copy.setVersion(original.getVersion());
    return copy;
}

From source file:fr.acxio.tools.agia.admin.StaleRunningJobsService.java

public void forceRunningJobsToFail() {
    if (logger.isInfoEnabled()) {
        logger.info("Reseting jobs...");
    }/* ww w .  j  ava2  s . co  m*/

    List<String> aJobNames = jobExplorer.getJobNames();
    for (String aJobName : aJobNames) {
        Set<JobExecution> aJobExecutions = jobExplorer.findRunningJobExecutions(aJobName);
        for (JobExecution aJobExecution : aJobExecutions) {
            if (logger.isInfoEnabled()) {
                logger.info("  " + aJobName + " (" + aJobExecution.getId() + ")");
            }
            aJobExecution.setEndTime(new Date());
            aJobExecution.setStatus(BatchStatus.FAILED);
            aJobExecution.setExitStatus(ExitStatus.FAILED);
            jobRepository.update(aJobExecution);
            for (StepExecution aStepExecution : aJobExecution.getStepExecutions()) {
                if (aStepExecution.getStatus().isGreaterThan(BatchStatus.COMPLETED)) {
                    if (logger.isInfoEnabled()) {
                        logger.info("    " + aStepExecution.getStepName());
                    }
                    aStepExecution.setEndTime(new Date());
                    aStepExecution.setStatus(BatchStatus.FAILED);
                    aStepExecution.setExitStatus(ExitStatus.FAILED);
                    jobRepository.update(aStepExecution);
                }
            }
        }
    }
    if (logger.isInfoEnabled()) {
        logger.info("Done.");
    }
}

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   ww w . j  a v a2 s.c  om
    }
    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: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 a  va2s  .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  ww.j  av a 2  s .c om*/

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

}

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

/**
 * Interface method implementation. //  w  ww  . j  av a  2  s .c  om
 * @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:org.geoserver.backuprestore.Backup.java

/**
 * Stop a running Backup/Restore Execution
 * /*from ww  w. jav  a2  s. co m*/
 * @param executionId
 * @return
 * @throws NoSuchJobExecutionException
 * @throws JobExecutionNotRunningException
 */
public void stopExecution(Long executionId)
        throws NoSuchJobExecutionException, JobExecutionNotRunningException {
    LOGGER.info("Stopping execution id [" + executionId + "]");

    JobExecution jobExecution = null;
    try {
        if (this.backupExecutions.get(executionId) != null) {
            jobExecution = this.backupExecutions.get(executionId).getDelegate();
        } else if (this.restoreExecutions.get(executionId) != null) {
            jobExecution = this.restoreExecutions.get(executionId).getDelegate();
        }

        jobOperator.stop(executionId);
    } finally {
        if (jobExecution != null) {
            final BatchStatus status = jobExecution.getStatus();

            if (!status.isGreaterThan(BatchStatus.STARTED)) {
                jobExecution.setStatus(BatchStatus.STOPPING);
                jobExecution.setEndTime(new Date());
                jobRepository.update(jobExecution);
            }
        }

        // Release locks on GeoServer Configuration:
        try {
            List<BackupRestoreCallback> callbacks = GeoServerExtensions.extensions(BackupRestoreCallback.class);
            for (BackupRestoreCallback callback : callbacks) {
                callback.onEndRequest();
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Could not unlock GeoServer Catalog Configuration!", e);
        }
    }
}

From source file:org.geoserver.backuprestore.Backup.java

/**
 * Abort a running Backup/Restore Execution
 * /*w w  w .j a  va2 s .co m*/
 * @param executionId
 * @throws NoSuchJobExecutionException
 * @throws JobExecutionAlreadyRunningException
 */
public void abandonExecution(Long executionId)
        throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException {
    LOGGER.info("Aborting execution id [" + executionId + "]");

    JobExecution jobExecution = null;
    try {
        if (this.backupExecutions.get(executionId) != null) {
            jobExecution = this.backupExecutions.get(executionId).getDelegate();
        } else if (this.restoreExecutions.get(executionId) != null) {
            jobExecution = this.restoreExecutions.get(executionId).getDelegate();
        }

        jobOperator.abandon(executionId);
    } finally {
        if (jobExecution != null) {
            jobExecution.setStatus(BatchStatus.ABANDONED);
            jobExecution.setEndTime(new Date());
            jobRepository.update(jobExecution);
        }

        // Release locks on GeoServer Configuration:
        try {
            List<BackupRestoreCallback> callbacks = GeoServerExtensions.extensions(BackupRestoreCallback.class);
            for (BackupRestoreCallback callback : callbacks) {
                callback.onEndRequest();
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Could not unlock GeoServer Catalog Configuration!", e);
        }
    }
}

From source file:org.springframework.batch.core.job.AbstractJob.java

/**
 * Run the specified job, handling all listener and repository calls, and
 * delegating the actual processing to {@link #doExecute(JobExecution)}.
 *
 * @see Job#execute(JobExecution)// ww  w . j av a  2 s  . com
 * @throws StartLimitExceededException
 *             if start limit of one of the steps was exceeded
 */
@Override
public final void execute(JobExecution execution) {

    if (logger.isDebugEnabled()) {
        logger.debug("Job execution starting: " + execution);
    }

    JobSynchronizationManager.register(execution);

    try {

        jobParametersValidator.validate(execution.getJobParameters());

        if (execution.getStatus() != BatchStatus.STOPPING) {

            execution.setStartTime(new Date());
            updateStatus(execution, BatchStatus.STARTED);

            listener.beforeJob(execution);

            try {
                doExecute(execution);
                if (logger.isDebugEnabled()) {
                    logger.debug("Job execution complete: " + execution);
                }
            } catch (RepeatException e) {
                throw e.getCause();
            }
        } else {

            // The job was already stopped before we even got this far. Deal
            // with it in the same way as any other interruption.
            execution.setStatus(BatchStatus.STOPPED);
            execution.setExitStatus(ExitStatus.COMPLETED);
            if (logger.isDebugEnabled()) {
                logger.debug("Job execution was stopped: " + execution);
            }

        }

    } catch (JobInterruptedException e) {
        logger.info("Encountered interruption executing job: " + e.getMessage());
        if (logger.isDebugEnabled()) {
            logger.debug("Full exception", e);
        }
        execution.setExitStatus(getDefaultExitStatusForFailure(e, execution));
        execution.setStatus(BatchStatus.max(BatchStatus.STOPPED, e.getStatus()));
        execution.addFailureException(e);
    } catch (Throwable t) {
        logger.error("Encountered fatal error executing job", t);
        execution.setExitStatus(getDefaultExitStatusForFailure(t, execution));
        execution.setStatus(BatchStatus.FAILED);
        execution.addFailureException(t);
    } finally {
        try {
            if (execution.getStatus().isLessThanOrEqualTo(BatchStatus.STOPPED)
                    && execution.getStepExecutions().isEmpty()) {
                ExitStatus exitStatus = execution.getExitStatus();
                ExitStatus newExitStatus = ExitStatus.NOOP
                        .addExitDescription("All steps already completed or no steps configured for this job.");
                execution.setExitStatus(exitStatus.and(newExitStatus));
            }

            execution.setEndTime(new Date());

            try {
                listener.afterJob(execution);
            } catch (Exception e) {
                logger.error("Exception encountered in afterStep callback", e);
            }

            jobRepository.update(execution);
        } finally {
            JobSynchronizationManager.release();
        }

    }

}

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   ww  w  .jav a  2s.  c o  m

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

    return jobExecution;
}