Example usage for org.springframework.batch.core.scope.context StepContextRepeatCallback StepContextRepeatCallback

List of usage examples for org.springframework.batch.core.scope.context StepContextRepeatCallback StepContextRepeatCallback

Introduction

In this page you can find the example usage for org.springframework.batch.core.scope.context StepContextRepeatCallback StepContextRepeatCallback.

Prototype

public StepContextRepeatCallback(StepExecution stepExecution) 

Source Link

Usage

From source file:org.springframework.batch.core.step.tasklet.TaskletStep.java

/**
 * Process the step and update its context so that progress can be monitored
 * by the caller. The step is broken down into chunks, each one executing in
 * a transaction. The step and its execution and execution context are all
 * given an up to date {@link BatchStatus}, and the {@link JobRepository} is
 * used to store the result. Various reporting information are also added to
 * the current context governing the step execution, which would normally be
 * available to the caller through the step's {@link ExecutionContext}.<br>
 *
 * @throws JobInterruptedException if the step or a chunk is interrupted
 * @throws RuntimeException if there is an exception during a chunk
 * execution/*from   www . j a  v a 2  s .c  o m*/
 *
 */
@Override
protected void doExecute(StepExecution stepExecution) throws Exception {
    stepExecution.getExecutionContext().put(TASKLET_TYPE_KEY, tasklet.getClass().getName());
    stepExecution.getExecutionContext().put(STEP_TYPE_KEY, this.getClass().getName());

    stream.update(stepExecution.getExecutionContext());
    getJobRepository().updateExecutionContext(stepExecution);

    // Shared semaphore per step execution, so other step executions can run
    // in parallel without needing the lock
    final Semaphore semaphore = createSemaphore();

    stepOperations.iterate(new StepContextRepeatCallback(stepExecution) {

        @Override
        public RepeatStatus doInChunkContext(RepeatContext repeatContext, ChunkContext chunkContext)
                throws Exception {

            StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();

            // Before starting a new transaction, check for
            // interruption.
            interruptionPolicy.checkInterrupted(stepExecution);

            RepeatStatus result;
            try {
                result = new TransactionTemplate(transactionManager, transactionAttribute)
                        .execute(new ChunkTransactionCallback(chunkContext, semaphore));
            } catch (UncheckedTransactionException e) {
                // Allow checked exceptions to be thrown inside callback
                throw (Exception) e.getCause();
            }

            chunkListener.afterChunk(chunkContext);

            // Check for interruption after transaction as well, so that
            // the interrupted exception is correctly propagated up to
            // caller
            interruptionPolicy.checkInterrupted(stepExecution);

            return result;
        }

    });

}