public static <T> T doInJobScope(JobExecution jobExecution, Callable<T> callable) throws Exception { try { JobSynchronizationManager.register(jobExecution); return callable.call(); } finally { JobSynchronizationManager.close(); } }
/** * @param testContext the current test context * @throws Exception if there is a problem * @see TestExecutionListener#afterTestMethod(TestContext) */ @Override public void afterTestMethod(TestContext testContext) throws Exception { if (testContext.hasAttribute(JOB_EXECUTION)) { JobSynchronizationManager.close(); } }
@After public void cleanUp() { JobSynchronizationManager.close(); // Check that all temporary bean definitions are cleaned up assertEquals(beanCount, beanFactory.getBeanDefinitionCount()); }
@After public void cleanUp() { JobSynchronizationManager.close(); // Check that all temporary bean definitions are cleaned up assertEquals(beanCount, beanFactory.getBeanDefinitionCount()); }
@Before @After public void start() { JobSynchronizationManager.close(); TestJob.reset(); }
@Before @After public void start() { while (JobSynchronizationManager.getContext() != null) { JobSynchronizationManager.close(); } }
@After public void stop() { JobSynchronizationManager.close(); // Check that all temporary bean definitions are cleaned up assertEquals(beanCount, beanFactory.getBeanDefinitionCount()); }
@Override public JobContext call() throws Exception { try { JobSynchronizationManager.register(jobExecution); JobContext context = JobSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { JobSynchronizationManager.close(); } } });
@Test public void testRegisterTwice() { JobSynchronizationManager.register(jobExecution); JobSynchronizationManager.register(jobExecution); JobSynchronizationManager.close(); // if someone registers you have to assume they are going to close, so // the last thing you want is for the close to remove another context // that someone else has registered assertNotNull(JobSynchronizationManager.getContext()); JobSynchronizationManager.close(); assertNull(JobSynchronizationManager.getContext()); }
@Override 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(); } } });
@Test public void testClose() throws Exception { final List<String> list = new ArrayList<>(); JobContext context = JobSynchronizationManager.register(jobExecution); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); JobSynchronizationManager.close(); assertNull(JobSynchronizationManager.getContext()); assertEquals(0, list.size()); }
@Test public void testMultithreaded() throws Exception { JobContext context = JobSynchronizationManager.register(jobExecution); ExecutorService executorService = Executors.newFixedThreadPool(2); FutureTask<JobContext> task = new FutureTask<>(new Callable<JobContext>() { @Override public JobContext call() throws Exception { try { JobSynchronizationManager.register(jobExecution); JobContext context = JobSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { JobSynchronizationManager.close(); } } }); executorService.execute(task); executorService.awaitTermination(1, TimeUnit.SECONDS); assertEquals(context.attributeNames().length, task.get().attributeNames().length); JobSynchronizationManager.close(); assertNull(JobSynchronizationManager.getContext()); }
@Override 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(); } } });
JobSynchronizationManager.close();
private void start(String foo) { JobSynchronizationManager.close(); jobExecution = new JobExecution(123L); ExecutionContext executionContext = new ExecutionContext(); executionContext.put("foo", foo); executionContext.put("parent", bar); jobExecution.setExecutionContext(executionContext); JobSynchronizationManager.register(jobExecution); beanCount = beanFactory.getBeanDefinitionCount(); }
@Before public void start() { JobSynchronizationManager.close(); jobExecution = new JobExecution(123L); ExecutionContext executionContext = new ExecutionContext(); executionContext.put("foo", "bar"); jobExecution.setExecutionContext(executionContext); JobSynchronizationManager.register(jobExecution); beanCount = beanFactory.getBeanDefinitionCount(); }
@Test public void testCompoundPropertyTwice() throws Exception { assertEquals("bar-bar", compound.getName()); JobSynchronizationManager.close(); jobExecution = new JobExecution(123L); ExecutionContext executionContext = new ExecutionContext(); executionContext.put("foo", "spam"); jobExecution.setExecutionContext(executionContext); JobSynchronizationManager.register(jobExecution); assertEquals("spam-bar", compound.getName()); }