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

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

Introduction

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

Prototype

public Collection<StepExecution> getStepExecutions() 

Source Link

Document

Accessor for the step executions.

Usage

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

@Override
@Transactional/*from   ww  w.  j  av a 2s . c o  m*/
public boolean stop(long executionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException {

    JobExecution jobExecution = findExecutionById(executionId);
    // Indicate the execution should be stopped by setting it's status to
    // 'STOPPING'. It is assumed that
    // the step implementation will check this status at chunk boundaries.
    BatchStatus status = jobExecution.getStatus();
    if (!(status == BatchStatus.STARTED || status == BatchStatus.STARTING)) {
        throw new JobExecutionNotRunningException(
                "JobExecution must be running so that it can be stopped: " + jobExecution);
    }
    jobExecution.setStatus(BatchStatus.STOPPING);
    jobRepository.update(jobExecution);

    try {
        Job job = jobRegistry.getJob(jobExecution.getJobInstance().getJobName());
        if (job instanceof StepLocator) {//can only process as StepLocator is the only way to get the step object
            //get the current stepExecution
            for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
                if (stepExecution.getStatus().isRunning()) {
                    try {
                        //have the step execution that's running -> need to 'stop' it
                        Step step = ((StepLocator) job).getStep(stepExecution.getStepName());
                        if (step instanceof TaskletStep) {
                            Tasklet tasklet = ((TaskletStep) step).getTasklet();
                            if (tasklet instanceof StoppableTasklet) {
                                StepSynchronizationManager.register(stepExecution);
                                ((StoppableTasklet) tasklet).stop();
                                StepSynchronizationManager.release();
                            }
                        }
                    } catch (NoSuchStepException e) {
                        logger.warn("Step not found", e);
                    }
                }
            }
        }
    } catch (NoSuchJobException e) {
        logger.warn("Cannot find Job object", e);
    }

    return true;
}

From source file:org.springframework.batch.core.repository.support.SimpleJobRepository.java

@Override
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {
    List<JobExecution> jobExecutions = jobExecutionDao.findJobExecutions(jobInstance);
    List<StepExecution> stepExecutions = new ArrayList<StepExecution>(jobExecutions.size());

    for (JobExecution jobExecution : jobExecutions) {
        stepExecutionDao.addStepExecutions(jobExecution);
        for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
            if (stepName.equals(stepExecution.getStepName())) {
                stepExecutions.add(stepExecution);
            }//from  w w  w  . ja v  a  2 s .co  m
        }
    }

    StepExecution latest = null;
    for (StepExecution stepExecution : stepExecutions) {
        if (latest == null) {
            latest = stepExecution;
        }
        if (latest.getStartTime().getTime() < stepExecution.getStartTime().getTime()) {
            latest = stepExecution;
        }
    }

    if (latest != null) {
        ExecutionContext stepExecutionContext = ecDao.getExecutionContext(latest);
        latest.setExecutionContext(stepExecutionContext);
        ExecutionContext jobExecutionContext = ecDao.getExecutionContext(latest.getJobExecution());
        latest.getJobExecution().setExecutionContext(jobExecutionContext);
    }

    return latest;
}

From source file:org.springframework.batch.core.repository.support.SimpleJobRepository.java

/**
 * @return number of executions of the step within given job instance
 *//*from   www .  ja v a2  s  . c o m*/
@Override
public int getStepExecutionCount(JobInstance jobInstance, String stepName) {
    int count = 0;
    List<JobExecution> jobExecutions = jobExecutionDao.findJobExecutions(jobInstance);
    for (JobExecution jobExecution : jobExecutions) {
        stepExecutionDao.addStepExecutions(jobExecution);
        for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
            if (stepName.equals(stepExecution.getStepName())) {
                count++;
            }
        }
    }
    return count;
}

From source file:org.springframework.batch.core.test.football.FootballJobIntegrationTests.java

@Test
public void testLaunchJob() throws Exception {
    JobExecution execution = jobLauncher.run(job,
            new JobParametersBuilder().addLong("commit.interval", 10L).toJobParameters());
    assertEquals(BatchStatus.COMPLETED, execution.getStatus());
    for (StepExecution stepExecution : execution.getStepExecutions()) {
        logger.info("Processed: " + stepExecution);
        if (stepExecution.getStepName().equals("playerload")) {
            // The effect of the retries
            assertEquals(new Double(Math.ceil(stepExecution.getReadCount() / 10. + 1)).intValue(),
                    stepExecution.getCommitCount());
        }//from  w ww.j a va2 s  .  c o  m
    }
}

From source file:org.springframework.batch.core.test.football.FootballJobSkipIntegrationTests.java

@Test
public void testLaunchJob() throws Exception {
    try {/*from w ww .ja v  a2s.  c  om*/
        if (databaseType == DatabaseType.POSTGRES || databaseType == DatabaseType.ORACLE) {
            // Extra special test for these platforms (would have failed
            // the job with UNKNOWN status in Batch 2.0):
            jdbcTemplate.update("SET CONSTRAINTS ALL DEFERRED");
        }
    } catch (Exception e) {
        // Ignore (wrong platform)
    }
    JobExecution execution = jobLauncher.run(job,
            new JobParametersBuilder().addLong("skip.limit", 0L).toJobParameters());
    assertEquals(BatchStatus.COMPLETED, execution.getStatus());
    for (StepExecution stepExecution : execution.getStepExecutions()) {
        logger.info("Processed: " + stepExecution);
    }
    // They all skip on the second execution because of a primary key
    // violation
    long retryLimit = 2L;
    execution = jobLauncher.run(job, new JobParametersBuilder().addLong("skip.limit", 100000L)
            .addLong("retry.limit", retryLimit).toJobParameters());
    assertEquals(BatchStatus.COMPLETED, execution.getStatus());
    for (StepExecution stepExecution : execution.getStepExecutions()) {
        logger.info("Processed: " + stepExecution);
        if (stepExecution.getStepName().equals("playerload")) {
            // The effect of the retries is to increase the number of
            // rollbacks
            int commitInterval = stepExecution.getReadCount() / (stepExecution.getCommitCount() - 1);
            // Account for the extra empty commit if the read count is
            // commensurate with the commit interval
            int effectiveCommitCount = stepExecution.getReadCount() % commitInterval == 0
                    ? stepExecution.getCommitCount() - 1
                    : stepExecution.getCommitCount();
            long expectedRollbacks = Math.max(1, retryLimit) * effectiveCommitCount
                    + stepExecution.getReadCount();
            assertEquals(expectedRollbacks, stepExecution.getRollbackCount());
            assertEquals(stepExecution.getReadCount(), stepExecution.getWriteSkipCount());
        }
    }

}

From source file:org.springframework.batch.core.test.football.ParallelJobIntegrationTests.java

@Test
public void testLaunchJob() throws Exception {
    JobExecution execution = jobLauncher.run(job, new JobParametersBuilder().toJobParameters());
    assertEquals(BatchStatus.COMPLETED, execution.getStatus());
    for (StepExecution stepExecution : execution.getStepExecutions()) {
        logger.info("Processed: " + stepExecution);
    }/* w w  w.ja v a2 s  .c  o m*/
}

From source file:org.springframework.batch.integration.partition.JmsIntegrationTests.java

@Test
public void testLaunchJob() throws Exception {
    int before = jobExplorer.getJobInstances(job.getName(), 0, 100).size();
    assertNotNull(jobLauncher.run(job, new JobParameters()));
    List<JobInstance> jobInstances = jobExplorer.getJobInstances(job.getName(), 0, 100);
    int after = jobInstances.size();
    assertEquals(1, after - before);//  ww w  .  ja va2  s  .  co m
    JobExecution jobExecution = jobExplorer.getJobExecutions(jobInstances.get(jobInstances.size() - 1)).get(0);
    assertEquals(jobExecution.getExitStatus().getExitDescription(), BatchStatus.COMPLETED,
            jobExecution.getStatus());
    assertEquals(3, jobExecution.getStepExecutions().size());
    for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
        // BATCH-1703: we are using a map dao so the step executions in the job execution are old and we need to
        // pull them back out of the repository...
        stepExecution = jobExplorer.getStepExecution(jobExecution.getId(), stepExecution.getId());
        logger.debug("" + stepExecution);
        assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    }
}

From source file:simple.spring.batch.JobLauncherDetails.java

@SuppressWarnings("unchecked")
protected void executeInternal(JobExecutionContext context) {
    Map<String, Object> jobDataMap = context.getMergedJobDataMap();
    String jobName = (String) jobDataMap.get(JOB_NAME);

    log.info("Quartz trigger firing with Spring Batch jobName=" + jobName);

    // JobParameters?   
    // ?  ??  //from  w  w  w  .j  av a 2s.  co  m
    //jobDataMap.put("date", new Date());

    JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);
    try {
        Job job = jobLocator.getJob(jobName);

        JobExecution jobExecution = jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
        System.out.println("<<< " + jobExecution.getExitStatus().getExitCode());

        if ("COMPLETED".equals(jobExecution.getStatus())) {
            System.out.println("COMPLETED!!");
        }

        Collection<StepExecution> collection = jobExecution.getStepExecutions();

        for (StepExecution s : collection) {
            System.out.println(s);
            ExecutionContext excutionContext = s.getExecutionContext();

        }

    } catch (JobExecutionException e) {
        // log.error("Could not execute job.", e);
    }
}