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

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

Introduction

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

Prototype

public ChunkContext(StepContext stepContext) 

Source Link

Usage

From source file:de.langmi.spring.batch.examples.complex.file.split.GetLineCountTaskletTest.java

@Test
public void testExecute() throws Exception {
    // setup/*from w  ww.  j a  v  a2  s. co m*/
    tasklet = new GetLineCountTasklet();
    tasklet.setResource(new FileSystemResource(INPUT));
    StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution();

    // execute
    RepeatStatus status = tasklet.execute(new StepContribution(stepExecution),
            new ChunkContext(new StepContext(stepExecution)));
    // assertions
    assertEquals(RepeatStatus.FINISHED, status);
    assertEquals(EXPECTED_COUNT, stepExecution.getExecutionContext().get("line.count"));
}

From source file:org.springframework.batch.core.scope.context.StepContextRepeatCallback.java

/**
 * Manage the {@link StepContext} lifecycle. Business processing should be
 * delegated to {@link #doInChunkContext(RepeatContext, ChunkContext)}. This
 * is to ensure that the current thread has a reference to the context, even
 * if the callback is executed in a pooled thread. Handles the registration
 * and unregistration of the step context, so clients should not duplicate
 * those calls./*w w  w .  j av  a  2  s . c om*/
 *
 * @see RepeatCallback#doInIteration(RepeatContext)
 */
@Override
public RepeatStatus doInIteration(RepeatContext context) throws Exception {

    // The StepContext has to be the same for all chunks,
    // otherwise step-scoped beans will be re-initialised for each chunk.
    StepContext stepContext = StepSynchronizationManager.register(stepExecution);
    if (logger.isDebugEnabled()) {
        logger.debug("Preparing chunk execution for StepContext: " + ObjectUtils.identityToString(stepContext));
    }

    ChunkContext chunkContext = attributeQueue.poll();
    if (chunkContext == null) {
        chunkContext = new ChunkContext(stepContext);
    }

    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Chunk execution starting: queue size=" + attributeQueue.size());
        }
        return doInChunkContext(context, chunkContext);
    } finally {
        // Still some stuff to do with the data in this chunk,
        // pass it back.
        if (!chunkContext.isComplete()) {
            attributeQueue.add(chunkContext);
        }
        StepSynchronizationManager.close();
    }
}

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

@Test
public void testStopped() throws Exception {
    initializeTasklet();//  w w w  . jav a  2  s.com
    tasklet.setJobExplorer(jobExplorer);
    tasklet.afterPropertiesSet();
    tasklet.beforeStep(stepExecution);

    JobExecution stoppedJobExecution = new JobExecution(stepExecution.getJobExecution());
    stoppedJobExecution.setStatus(BatchStatus.STOPPING);

    when(jobExplorer.getJobExecution(1L)).thenReturn(stepExecution.getJobExecution(),
            stepExecution.getJobExecution(), stoppedJobExecution);

    String command = System.getProperty("os.name").toLowerCase().contains("win") ? "ping 1.1.1.1 -n 1 -w 5000"
            : "sleep 15";
    tasklet.setCommand(command);
    tasklet.setTerminationCheckInterval(10);
    tasklet.afterPropertiesSet();

    StepContribution contribution = stepExecution.createStepContribution();
    StepContext stepContext = new StepContext(stepExecution);
    ChunkContext chunkContext = new ChunkContext(stepContext);
    tasklet.execute(contribution, chunkContext);

    assertEquals(contribution.getExitStatus().getExitCode(), ExitStatus.STOPPED.getExitCode());
}