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

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

Introduction

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

Prototype

public void setStatus(BatchStatus status) 

Source Link

Document

Sets the current status of this step

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)
 * //from  w w  w.ja v a 2 s. c o  m
 * @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:fr.acxio.tools.agia.admin.StaleRunningJobsService.java

public void forceRunningJobsToFail() {
    if (logger.isInfoEnabled()) {
        logger.info("Reseting jobs...");
    }/*w w w.  ja v  a 2s . 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:es.fcs.batch.integration.chunk.MyChunkMessageChannelItemWriter.java

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    if (!(stepExecution.getStatus() == BatchStatus.COMPLETED)) {
        return ExitStatus.EXECUTING;
    }/*w w  w . j  ava2  s . c  om*/
    long expecting = localState.getExpecting();
    boolean timedOut;
    try {
        logger.debug("Waiting for results in step listener...");
        timedOut = !waitForResults();
        logger.debug("Finished waiting for results in step listener.");
    } catch (RuntimeException e) {
        logger.debug("Detected failure waiting for results in step listener.", e);
        stepExecution.setStatus(BatchStatus.FAILED);
        return ExitStatus.FAILED.addExitDescription(e.getClass().getName() + ": " + e.getMessage());
    } finally {
        for (StepContribution contribution : getStepContributions()) {
            stepExecution.apply(contribution);
        }
    }
    if (timedOut) {
        stepExecution.setStatus(BatchStatus.FAILED);
        throw new ItemStreamException("Timed out waiting for back log at end of step");
    }
    return ExitStatus.COMPLETED.addExitDescription("Waited for " + expecting + " results.");
}

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

public Collection<StepExecution> getStepExecutions(Long jobExecutionId) throws NoSuchJobExecutionException {

    JobExecution jobExecution = jobExecutionDao.getJobExecution(jobExecutionId);
    if (jobExecution == null) {
        throw new NoSuchJobExecutionException("No JobExecution with id=" + jobExecutionId);
    }/*  w  w  w.j  a  va 2 s  .  co m*/

    stepExecutionDao.addStepExecutions(jobExecution);

    String jobName = jobExecution.getJobInstance() == null ? null : jobExecution.getJobInstance().getJobName();
    Collection<String> missingStepNames = new LinkedHashSet<String>();

    if (jobName != null) {
        missingStepNames.addAll(stepExecutionDao.findStepNamesForJobExecution(jobName, "*:partition*"));
        logger.debug("Found step executions in repository: " + missingStepNames);
    }

    Job job = null;
    try {
        job = jobLocator.getJob(jobName);
    } catch (NoSuchJobException e) {
        // expected
    }
    if (job instanceof StepLocator) {
        Collection<String> stepNames = ((StepLocator) job).getStepNames();
        missingStepNames.addAll(stepNames);
        logger.debug("Added step executions from job: " + missingStepNames);
    }

    for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
        String stepName = stepExecution.getStepName();
        if (missingStepNames.contains(stepName)) {
            missingStepNames.remove(stepName);
        }
        logger.debug("Removed step executions from job execution: " + missingStepNames);
    }

    for (String stepName : missingStepNames) {
        StepExecution stepExecution = jobExecution.createStepExecution(stepName);
        stepExecution.setStatus(BatchStatus.UNKNOWN);
    }

    return jobExecution.getStepExecutions();

}

From source file:admin.service.SimpleJobService.java

@Override
public Collection<StepExecution> getStepExecutions(Long jobExecutionId) throws NoSuchJobExecutionException {

    JobExecution jobExecution = jobExecutionDao.getJobExecution(jobExecutionId);
    if (jobExecution == null) {
        throw new NoSuchJobExecutionException("No JobExecution with id=" + jobExecutionId);
    }/*w  ww .ja  v  a  2  s  . c  om*/

    stepExecutionDao.addStepExecutions(jobExecution);

    String jobName = jobExecution.getJobInstance() == null
            ? jobInstanceDao.getJobInstance(jobExecution).getJobName()
            : jobExecution.getJobInstance().getJobName();
    Collection<String> missingStepNames = new LinkedHashSet<String>();

    if (jobName != null) {
        missingStepNames.addAll(stepExecutionDao.findStepNamesForJobExecution(jobName, "*:partition*"));
        logger.debug("Found step executions in repository: " + missingStepNames);
    }

    Job job = null;
    try {
        job = jobLocator.getJob(jobName);
    } catch (NoSuchJobException e) {
        // expected
    }
    if (job instanceof StepLocator) {
        Collection<String> stepNames = ((StepLocator) job).getStepNames();
        missingStepNames.addAll(stepNames);
        logger.debug("Added step executions from job: " + missingStepNames);
    }

    for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
        String stepName = stepExecution.getStepName();
        if (missingStepNames.contains(stepName)) {
            missingStepNames.remove(stepName);
        }
        logger.debug("Removed step executions from job execution: " + missingStepNames);
    }

    for (String stepName : missingStepNames) {
        StepExecution stepExecution = jobExecution.createStepExecution(stepName);
        stepExecution.setStatus(BatchStatus.UNKNOWN);
    }

    return jobExecution.getStepExecutions();

}

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)})./*from  ww  w.  j  a  va 2  s  . co 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());
        }
    }
}

From source file:org.springframework.batch.integration.chunk.ChunkMessageChannelItemWriter.java

@Override
public ExitStatus afterStep(StepExecution stepExecution) {
    if (!(stepExecution.getStatus() == BatchStatus.COMPLETED)) {
        return ExitStatus.EXECUTING;
    }//from w  w  w. j  a v  a 2s .com
    long expecting = localState.getExpecting();
    boolean timedOut;
    try {
        logger.debug("Waiting for results in step listener...");
        timedOut = !waitForResults();
        logger.debug("Finished waiting for results in step listener.");
    } catch (RuntimeException e) {
        logger.debug("Detected failure waiting for results in step listener.", e);
        stepExecution.setStatus(BatchStatus.FAILED);
        return ExitStatus.FAILED.addExitDescription(e.getClass().getName() + ": " + e.getMessage());
    } finally {

        if (logger.isDebugEnabled()) {
            logger.debug("Finished waiting for results in step listener.  Still expecting: "
                    + localState.getExpecting());
        }

        for (StepContribution contribution : getStepContributions()) {
            stepExecution.apply(contribution);
        }
    }
    if (timedOut) {
        stepExecution.setStatus(BatchStatus.FAILED);
        return ExitStatus.FAILED.addExitDescription(
                "Timed out waiting for " + localState.getExpecting() + " backlog at end of step");
    }
    return ExitStatus.COMPLETED.addExitDescription("Waited for " + expecting + " results.");
}

From source file:org.springframework.cloud.task.batch.partition.DeployerStepExecutionHandler.java

@Override
public void run(String... args) throws Exception {

    validateRequest();//  w w w  .  j  a  va 2 s  . c  om

    Long jobExecutionId = Long
            .parseLong(environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
    Long stepExecutionId = Long
            .parseLong(environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
    StepExecution stepExecution = jobExplorer.getStepExecution(jobExecutionId, stepExecutionId);

    if (stepExecution == null) {
        throw new NoSuchStepException(String.format(
                "No StepExecution could be located for step execution id %s within job execution %s",
                stepExecutionId, jobExecutionId));
    }

    String stepName = environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME);
    Step step = stepLocator.getStep(stepName);

    try {
        logger.debug(String.format("Executing step %s with step execution id %s and job execution id %s",
                stepExecution.getStepName(), stepExecutionId, jobExecutionId));

        step.execute(stepExecution);
    } catch (JobInterruptedException e) {
        stepExecution.setStatus(BatchStatus.STOPPED);
        jobRepository.update(stepExecution);
    } catch (Throwable e) {
        stepExecution.addFailureException(e);
        stepExecution.setStatus(BatchStatus.FAILED);
        jobRepository.update(stepExecution);
    }
}

From source file:org.springframework.yarn.batch.container.DefaultBatchYarnContainer.java

@Override
protected void runInternal() {

    Long jobExecutionId = safeParse(getEnvironment(YarnSystemConstants.AMSERVICE_BATCH_JOBEXECUTIONID));
    Long stepExecutionId = safeParse(getEnvironment(YarnSystemConstants.AMSERVICE_BATCH_STEPEXECUTIONID));
    String stepName = getEnvironment(YarnSystemConstants.AMSERVICE_BATCH_STEPNAME);

    if (log.isDebugEnabled()) {
        log.debug("Requesting StepExecution: " + jobExecutionId + " / " + stepExecutionId);
    }/*from   w w w  .j  a  v a  2 s .  c om*/

    StepExecution stepExecution = getJobExplorer().getStepExecution(jobExecutionId, stepExecutionId);
    if (stepExecution == null) {
        throw new NoSuchStepException("No StepExecution could be located for this request: ");
    }

    if (log.isDebugEnabled()) {
        log.debug("Got StepExecution: " + stepExecution);
        log.debug("Locating Step: " + stepName);
    }

    Step step = getStepLocator().getStep(stepName);
    if (log.isDebugEnabled()) {
        log.debug("Located step: " + step);
    }

    if (step == null) {
        throw new NoSuchStepException(String.format("No Step with name [%s] could be located.", stepName));
    }

    try {
        if (log.isDebugEnabled()) {
            log.debug("Executing step: " + step + " / " + stepExecution);
        }
        step.execute(stepExecution);
    } catch (JobInterruptedException e) {
        log.error("error executing step 1", e);
        stepExecution.setStatus(BatchStatus.STOPPED);
    } catch (Throwable e) {
        log.error("error executing step 2", e);
        stepExecution.addFailureException(e);
        stepExecution.setStatus(BatchStatus.FAILED);
    }

    if (log.isDebugEnabled()) {
        log.debug("Finished remote step run, status is " + stepExecution.getStatus());
    }

    MindAppmasterServiceClient client = (MindAppmasterServiceClient) getIntegrationServiceClient();
    PartitionedStepExecutionStatusReq req = new PartitionedStepExecutionStatusReq();
    req.stepExecution = JobRepositoryRpcFactory.convertStepExecutionType(stepExecution);
    BaseResponseObject doMindRequest = client.doMindRequest(req);
    log.info("got response for status update: " + doMindRequest);
}