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

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

Introduction

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

Prototype

public ExecutionContext getExecutionContext() 

Source Link

Document

Returns the ExecutionContext for this execution.

Usage

From source file:org.springframework.batch.core.test.repository.JdbcJobRepositoryTests.java

@Test
public void testFindOrCreateJobWithExecutionContext() throws Exception {
    job.setName("foo");
    int before = 0;
    JobExecution execution = repository.createJobExecution(job.getName(), new JobParameters());
    execution.getExecutionContext().put("foo", "bar");
    repository.updateExecutionContext(execution);
    int after = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM BATCH_JOB_EXECUTION_CONTEXT", Integer.class);
    assertEquals(before + 1, after);/*from  w w  w  .  j  av  a 2  s.  co m*/
    assertNotNull(execution.getId());
    JobExecution last = repository.getLastJobExecution(job.getName(), new JobParameters());
    assertEquals(execution, last);
    assertEquals(execution.getExecutionContext(), last.getExecutionContext());
}

From source file:org.springframework.batch.integration.x.IncrementalColumnRangePartitioner.java

@Override
public void beforeStep(StepExecution stepExecution) {
    if (StringUtils.hasText(checkColumn)) {

        if (overrideValue != null && overrideValue >= 0) {
            this.incrementalMin = overrideValue;
        } else {//w  w  w.  j a  v a2  s.  c  o m
            String jobName = stepExecution.getJobExecution().getJobInstance().getJobName();

            // Get the last jobInstance...not the current one
            List<JobInstance> jobInstances = jobExplorer.getJobInstances(jobName, 1, 1);

            if (jobInstances.size() > 0) {
                JobInstance lastInstance = jobInstances.get(jobInstances.size() - 1);

                List<JobExecution> executions = jobExplorer.getJobExecutions(lastInstance);

                JobExecution lastExecution = executions.get(0);

                for (JobExecution execution : executions) {
                    if (lastExecution.getEndTime().getTime() < execution.getEndTime().getTime()) {
                        lastExecution = execution;
                    }
                }

                if (lastExecution.getExecutionContext().containsKey(BATCH_INCREMENTAL_MAX_ID)) {
                    this.incrementalMin = lastExecution.getExecutionContext().getLong(BATCH_INCREMENTAL_MAX_ID);
                } else {
                    this.incrementalMin = Long.MIN_VALUE;
                }
            } else {
                this.incrementalMin = Long.MIN_VALUE;
            }
        }

        long newMin = jdbcTemplate.queryForObject(String.format("select max(%s) from %s", checkColumn, table),
                Integer.class);

        stepExecution.getExecutionContext().put(BATCH_INCREMENTAL_MAX_ID, newMin);
    }

    if (StringUtils.hasText(column) && StringUtils.hasText(table)) {
        if (StringUtils.hasText(checkColumn)) {
            Long minResult = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") from " + table + " where "
                    + checkColumn + " > " + this.incrementalMin, Long.class);
            Long maxResult = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") from " + table + " where "
                    + checkColumn + " > " + this.incrementalMin, Long.class);
            this.partitionMin = minResult != null ? minResult : Long.MIN_VALUE;
            this.partitionMax = maxResult != null ? maxResult : Long.MAX_VALUE;
        } else {
            Long minResult = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") from " + table,
                    Long.class);
            Long maxResult = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") from " + table,
                    Long.class);
            this.partitionMin = minResult != null ? minResult : Long.MIN_VALUE;
            this.partitionMax = maxResult != null ? maxResult : Long.MAX_VALUE;
        }
    }
}

From source file:org.springframework.batch.test.StepRunner.java

/**
 * Launch just the specified step as its own job. An IllegalStateException
 * is thrown if there is no Step with the given name.
 *
 * @param step The step to launch/*from  w w  w.jav a 2 s  .  c  o  m*/
 * @param jobParameters The JobParameters to use during the launch
 * @param jobExecutionContext An ExecutionContext whose values will be
 * loaded into the Job ExecutionContext prior to launching the step.
 * @return JobExecution
 */
public JobExecution launchStep(Step step, JobParameters jobParameters,
        final ExecutionContext jobExecutionContext) {
    //
    // Create a fake job
    //
    SimpleJob job = new SimpleJob();
    job.setName("TestJob");
    job.setJobRepository(this.jobRepository);

    List<Step> stepsToExecute = new ArrayList<Step>();
    stepsToExecute.add(step);
    job.setSteps(stepsToExecute);

    //
    // Dump the given Job ExecutionContext using a listener
    //
    if (jobExecutionContext != null && !jobExecutionContext.isEmpty()) {
        job.setJobExecutionListeners(new JobExecutionListener[] { new JobExecutionListenerSupport() {
            @Override
            public void beforeJob(JobExecution jobExecution) {
                ExecutionContext jobContext = jobExecution.getExecutionContext();
                for (Map.Entry<String, Object> entry : jobExecutionContext.entrySet()) {
                    jobContext.put(entry.getKey(), entry.getValue());
                }
            }
        } });
    }

    //
    // Launch the job
    //
    return this.launchJob(job, jobParameters);
}