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

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

Introduction

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

Prototype

public void setStartTime(Date startTime) 

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)
 * /*from   www  .  ja  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: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)/*from   w w w .  j av  a2  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();
        }

    }

}