Example usage for org.springframework.batch.item ExecutionContext put

List of usage examples for org.springframework.batch.item ExecutionContext put

Introduction

In this page you can find the example usage for org.springframework.batch.item ExecutionContext put.

Prototype

public void put(String key, @Nullable Object value) 

Source Link

Document

Add an Object value to the context.

Usage

From source file:org.springframework.batch.jsr.item.CheckpointSupport.java

@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
    try {//  w ww  . j a  va  2  s  .  c  om
        executionContext.put(getExecutionContextKey(checkpointKey), deepCopy(doCheckpoint()));
    } catch (Exception e) {
        throw new ItemStreamException(e);
    }
}

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  ww  w .  j a v  a  2s .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);
}

From source file:org.springframework.cloud.task.app.composedtaskrunner.TaskLauncherTasklet.java

/**
 * Executes the task as specified by the taskName with the associated
 * properties and arguments.//from w w w .j  a va2 s.  c  om
 * @param contribution mutable state to be passed back to update the current step execution
 * @param chunkContext contains the task-execution-id used by the listener.
 * @return Repeat status of FINISHED.
 */
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    String tmpTaskName = this.taskName.substring(0, this.taskName.lastIndexOf('_'));

    List<String> args = this.arguments;

    ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution()
            .getExecutionContext();
    if (stepExecutionContext.containsKey("task-arguments")) {
        args = (List<String>) stepExecutionContext.get("task-arguments");
    }

    long executionId = this.taskOperations.launch(tmpTaskName, this.properties, args);

    stepExecutionContext.put("task-execution-id", executionId);
    stepExecutionContext.put("task-arguments", args);

    if (!waitForTaskToComplete(executionId)) {
        throw new TaskExecutionTimeoutException(
                String.format("Timeout occurred while processing task with Execution Id %s", executionId));
    }
    return RepeatStatus.FINISHED;
}