Example usage for org.springframework.batch.core.scope.context StepSynchronizationManager close

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

Introduction

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

Prototype

public static void close() 

Source Link

Document

Method for unregistering the current context - should always and only be used by in conjunction with a matching #register(StepExecution) to ensure that #getContext() always returns the correct value.

Usage

From source file:org.springframework.batch.core.scope.AsyncStepScopeIntegrationTests.java

@After
public void cleanUp() {
    StepSynchronizationManager.close();
    // Check that all temporary bean definitions are cleaned up
    assertEquals(beanCount, beanFactory.getBeanDefinitionCount());
}

From source file:org.springframework.batch.core.scope.AsyncStepScopeIntegrationTests.java

@Test
public void testGetMultipleInMultipleThreads() throws Exception {

    List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();

    for (int i = 0; i < 12; i++) {
        final String value = "foo" + i;
        final Long id = 123L + i;
        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
            @Override/* w  w w .j a v  a 2  s. c om*/
            public String call() throws Exception {
                StepExecution stepExecution = new StepExecution(value, new JobExecution(0L), id);
                ExecutionContext executionContext = stepExecution.getExecutionContext();
                executionContext.put("foo", value);
                StepContext context = StepSynchronizationManager.register(stepExecution);
                logger.debug("Registered: " + context.getStepExecutionContext());
                try {
                    return simple.getName();
                } finally {
                    StepSynchronizationManager.close();
                }
            }
        });
        tasks.add(task);
        taskExecutor.execute(task);
    }

    int i = 0;
    for (FutureTask<String> task : tasks) {
        assertEquals("foo" + i, task.get());
        i++;
    }

}

From source file:org.springframework.batch.core.scope.AsyncStepScopeIntegrationTests.java

@Test
public void testGetSameInMultipleThreads() throws Exception {

    List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();
    final StepExecution stepExecution = new StepExecution("foo", new JobExecution(0L), 123L);
    ExecutionContext executionContext = stepExecution.getExecutionContext();
    executionContext.put("foo", "foo");
    StepSynchronizationManager.register(stepExecution);
    assertEquals("foo", simple.getName());

    for (int i = 0; i < 12; i++) {
        final String value = "foo" + i;
        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
            @Override/*from  w ww  .  j ava  2 s.  c o m*/
            public String call() throws Exception {
                ExecutionContext executionContext = stepExecution.getExecutionContext();
                executionContext.put("foo", value);
                StepContext context = StepSynchronizationManager.register(stepExecution);
                logger.debug("Registered: " + context.getStepExecutionContext());
                try {
                    return simple.getName();
                } finally {
                    StepSynchronizationManager.close();
                }
            }
        });
        tasks.add(task);
        taskExecutor.execute(task);
    }

    for (FutureTask<String> task : tasks) {
        assertEquals("foo", task.get());
    }

    // Don't close the outer scope until all tasks are finished. This should
    // always be the case if using an AbstractStep
    StepSynchronizationManager.close();

}

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  ww  .  ja v  a2 s . co  m*/
 *
 * @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.scope.StepScopePerformanceTests.java

@Before
public void start() throws Exception {
    int count = doTest("vanilla", "warmup");
    logger.info("Item count: " + count);
    StepSynchronizationManager.close();
    StepSynchronizationManager.register(new StepExecution("step", new JobExecution(0L), 1L));
}

From source file:org.springframework.batch.core.scope.StepScopePerformanceTests.java

@After
public void cleanup() {
    StepSynchronizationManager.close();
}