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

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

Introduction

In this page you can find the example usage for org.springframework.batch.core.scope.context JobSynchronizationManager 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(JobExecution) to ensure that #getContext() always returns the correct value.

Usage

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

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

From source file:org.springframework.batch.core.scope.AsyncJobScopeIntegrationTests.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/*from w ww . ja v  a 2s . c om*/
            public String call() throws Exception {
                JobExecution jobExecution = new JobExecution(id);
                ExecutionContext executionContext = jobExecution.getExecutionContext();
                executionContext.put("foo", value);
                JobContext context = JobSynchronizationManager.register(jobExecution);
                logger.debug("Registered: " + context.getJobExecutionContext());
                try {
                    return simple.getName();
                } finally {
                    JobSynchronizationManager.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.AsyncJobScopeIntegrationTests.java

@Test
public void testGetSameInMultipleThreads() throws Exception {

    List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>();
    final JobExecution jobExecution = new JobExecution(11L);
    ExecutionContext executionContext = jobExecution.getExecutionContext();
    executionContext.put("foo", "foo");
    JobSynchronizationManager.register(jobExecution);
    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 www.  j a v a  2  s. com*/
            public String call() throws Exception {
                ExecutionContext executionContext = jobExecution.getExecutionContext();
                executionContext.put("foo", value);
                JobContext context = JobSynchronizationManager.register(jobExecution);
                logger.debug("Registered: " + context.getJobExecutionContext());
                try {
                    return simple.getName();
                } finally {
                    JobSynchronizationManager.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 AbstractJob
    JobSynchronizationManager.close();

}