Example usage for org.springframework.batch.core StepExecution setExitStatus

List of usage examples for org.springframework.batch.core StepExecution setExitStatus

Introduction

In this page you can find the example usage for org.springframework.batch.core StepExecution setExitStatus.

Prototype

public void setExitStatus(ExitStatus exitStatus) 

Source Link

Usage

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

/**
 * Returns a copy of {@link StepExecution}, by adding new Objects for every field(no references are passed)
 * /*w w  w.  j a v a  2s  . c  om*/
 * @param original StepExecution to be copied
 * @return StepExecution copy
 */
private static StepExecution copy(StepExecution original) {
    StepExecution copy = new StepExecution(original.getStepName(), original.getJobExecution());
    copy.setCommitCount(original.getCommitCount());
    if (original.getEndTime() != null) {
        copy.setEndTime((Date) original.getEndTime().clone());
    }
    //Warning: no deep copy
    if (original.getExitStatus() != null) {
        copy.setExitStatus(new ExitStatus(original.getExitStatus().getExitCode(),
                original.getExitStatus().getExitDescription()));
    }
    copy.setFilterCount(original.getFilterCount());
    copy.setId(original.getId());
    if (original.getLastUpdated() != null) {
        copy.setLastUpdated((Date) original.getLastUpdated().clone());
    }
    copy.setProcessSkipCount(original.getProcessSkipCount());
    copy.setReadCount(original.getReadCount());
    copy.setReadSkipCount(original.getReadSkipCount());
    copy.setRollbackCount(original.getRollbackCount());
    if (original.getStartTime() != null) {
        copy.setStartTime((Date) original.getStartTime().clone());
    }
    if (original.getStatus() != null) {
        copy.setStatus(BatchStatus.valueOf(original.getStatus().name()));
    }
    if (original.isTerminateOnly()) {
        copy.setTerminateOnly();
    }
    copy.setVersion(original.getVersion());
    copy.setWriteCount(original.getWriteCount());
    copy.setWriteSkipCount(original.getWriteSkipCount());
    return copy;
}

From source file:org.obiba.onyx.core.etl.participant.impl.AppointmentListUpdateListenerTest.java

@Test
public void testAfterUpdateCompleted() {
    Map<String, JobParameter> jobParameterMap = new HashMap<String, JobParameter>();
    jobParameterMap.put("date", new JobParameter(new Date()));
    JobInstance job = new JobInstance(1l, new JobParameters(jobParameterMap), "jobTest");
    StepExecution stepExecution = new StepExecution("compltion", new JobExecution(job));
    stepExecution.setExitStatus(ExitStatus.COMPLETED);
    ExecutionContext context = new ExecutionContext();
    context.put("fileName", "fileName.xls");
    stepExecution.setExecutionContext(context);

    appointmentManagementServiceMock.saveAppointmentUpdateStats((AppointmentUpdateStats) EasyMock.anyObject());

    replay(appointmentManagementServiceMock);
    appointmentListUpdateListener.afterUpdateCompleted(stepExecution);
    verify(appointmentManagementServiceMock);

}

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

public void forceRunningJobsToFail() {
    if (logger.isInfoEnabled()) {
        logger.info("Reseting jobs...");
    }//from  ww  w. j a  va2s.c  o 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.gopivotal.spring.xd.module.jdbc.JdbcTasklet.java

/**
 * Execute the {@link #setSql(String) SQL query} provided. If the query starts with "select" (case insensitive) the
 * result is a list of maps, which is logged and added to the step execution exit status. Otherwise the query is
 * executed and the result is an indication, also in the exit status, of the number of rows updated.
 *//*from  w ww .  j ava 2 s . co  m*/
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();
    ExitStatus exitStatus = stepExecution.getExitStatus();

    String msg = "";
    if (StringUtils.hasText(sql)) {
        msg = runCommand(chunkContext.getStepContext(), sql);
    } else if (!CollectionUtils.isEmpty(scripts)) {
        msg = runScripts(chunkContext, scripts, null);
    }

    stepExecution.setExitStatus(exitStatus.addExitDescription(msg));
    return RepeatStatus.FINISHED;
}

From source file:com.springdeveloper.data.jdbc.batch.JdbcTasklet.java

/**
 * Execute the {@link #setSql(String) SQL query} provided. If the query
 * starts with "select" (case insensitive) the result is a list of maps,
 * which is logged and added to the step execution exit status. Otherwise
 * the query is executed and the result is an indication, also in the exit
 * status, of the number of rows updated.
 *//*from ww  w. java 2s. co m*/
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();
    ExitStatus exitStatus = stepExecution.getExitStatus();

    if (sql.trim().toUpperCase().startsWith("SELECT")) {
        logger.debug("Executing: " + sql);
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql,
                new BeanPropertySqlParameterSource(chunkContext.getStepContext()));
        String msg = "Result: " + result;
        logger.debug(msg);
        stepExecution.setExitStatus(exitStatus.addExitDescription(msg));
    } else {
        logger.debug("Updating : " + sql);
        int updated = jdbcTemplate.update(sql,
                new BeanPropertySqlParameterSource(chunkContext.getStepContext()));
        String msg = "Updated: " + updated + " rows";
        logger.debug(msg);
        stepExecution.setExitStatus(exitStatus.addExitDescription(msg));
    }
    return RepeatStatus.FINISHED;

}

From source file:org.springframework.batch.core.step.AbstractStep.java

/**
 * Template method for step execution logic - calls abstract methods for resource initialization (
 * {@link #open(ExecutionContext)}), execution logic ({@link #doExecute(StepExecution)}) and resource closing (
 * {@link #close(ExecutionContext)})./* www . ja va 2  s .c o  m*/
 */
@Override
public final void execute(StepExecution stepExecution)
        throws JobInterruptedException, UnexpectedJobExecutionException {

    if (logger.isDebugEnabled()) {
        logger.debug("Executing: id=" + stepExecution.getId());
    }
    stepExecution.setStartTime(new Date());
    stepExecution.setStatus(BatchStatus.STARTED);
    getJobRepository().update(stepExecution);

    // Start with a default value that will be trumped by anything
    ExitStatus exitStatus = ExitStatus.EXECUTING;

    doExecutionRegistration(stepExecution);

    try {
        getCompositeListener().beforeStep(stepExecution);
        open(stepExecution.getExecutionContext());

        try {
            doExecute(stepExecution);
        } catch (RepeatException e) {
            throw e.getCause();
        }
        exitStatus = ExitStatus.COMPLETED.and(stepExecution.getExitStatus());

        // Check if someone is trying to stop us
        if (stepExecution.isTerminateOnly()) {
            throw new JobInterruptedException("JobExecution interrupted.");
        }

        // Need to upgrade here not set, in case the execution was stopped
        stepExecution.upgradeStatus(BatchStatus.COMPLETED);
        if (logger.isDebugEnabled()) {
            logger.debug("Step execution success: id=" + stepExecution.getId());
        }
    } catch (Throwable e) {
        stepExecution.upgradeStatus(determineBatchStatus(e));
        exitStatus = exitStatus.and(getDefaultExitStatusForFailure(e));
        stepExecution.addFailureException(e);
        if (stepExecution.getStatus() == BatchStatus.STOPPED) {
            logger.info(String.format("Encountered interruption executing step %s in job %s : %s", name,
                    stepExecution.getJobExecution().getJobInstance().getJobName(), e.getMessage()));
            if (logger.isDebugEnabled()) {
                logger.debug("Full exception", e);
            }
        } else {
            logger.error(String.format("Encountered an error executing step %s in job %s", name,
                    stepExecution.getJobExecution().getJobInstance().getJobName()), e);
        }
    } finally {

        try {
            // Update the step execution to the latest known value so the
            // listeners can act on it
            exitStatus = exitStatus.and(stepExecution.getExitStatus());
            stepExecution.setExitStatus(exitStatus);
            exitStatus = exitStatus.and(getCompositeListener().afterStep(stepExecution));
        } catch (Exception e) {
            logger.error(String.format("Exception in afterStep callback in step %s in job %s", name,
                    stepExecution.getJobExecution().getJobInstance().getJobName()), e);
        }

        try {
            getJobRepository().updateExecutionContext(stepExecution);
        } catch (Exception e) {
            stepExecution.setStatus(BatchStatus.UNKNOWN);
            exitStatus = exitStatus.and(ExitStatus.UNKNOWN);
            stepExecution.addFailureException(e);
            logger.error(String.format(
                    "Encountered an error saving batch meta data for step %s in job %s. "
                            + "This job is now in an unknown state and should not be restarted.",
                    name, stepExecution.getJobExecution().getJobInstance().getJobName()), e);
        }

        stepExecution.setEndTime(new Date());
        stepExecution.setExitStatus(exitStatus);

        try {
            getJobRepository().update(stepExecution);
        } catch (Exception e) {
            stepExecution.setStatus(BatchStatus.UNKNOWN);
            stepExecution.setExitStatus(exitStatus.and(ExitStatus.UNKNOWN));
            stepExecution.addFailureException(e);
            logger.error(String.format(
                    "Encountered an error saving batch meta data for step %s in job %s. "
                            + "This job is now in an unknown state and should not be restarted.",
                    name, stepExecution.getJobExecution().getJobInstance().getJobName()), e);
        }

        try {
            close(stepExecution.getExecutionContext());
        } catch (Exception e) {
            logger.error(String.format("Exception while closing step execution resources in step %s in job %s",
                    name, stepExecution.getJobExecution().getJobInstance().getJobName()), e);
            stepExecution.addFailureException(e);
        }

        doExecutionRelease();

        if (logger.isDebugEnabled()) {
            logger.debug("Step execution complete: " + stepExecution.getSummary());
        }
    }
}