public static <T> T doInJobScope(JobExecution jobExecution, Callable<T> callable) throws Exception { try { JobSynchronizationManager.register(jobExecution); return callable.call(); } finally { JobSynchronizationManager.close(); } }
@Around("execution(void org.springframework.batch.core.Job+.execute(*)) && target(job) && args(jobExecution)") public void execute(Job job, JobExecution jobExecution) { JobSynchronizationManager.register(jobExecution); try { job.execute(jobExecution); } finally { JobSynchronizationManager.release(); } }
/** * @param testContext the current test context * @throws Exception if there is a problem * @see TestExecutionListener#beforeTestMethod(TestContext) */ @Override public void beforeTestMethod(org.springframework.test.context.TestContext testContext) throws Exception { if (testContext.hasAttribute(JOB_EXECUTION)) { JobExecution jobExecution = (JobExecution) testContext.getAttribute(JOB_EXECUTION); JobSynchronizationManager.register(jobExecution); } }
@Before public void setUp() throws Exception { context = JobSynchronizationManager.register(jobExecution); }
@Test public void testXmlJobScopeWithInterface() throws Exception { context = new ClassPathXmlApplicationContext( "org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTestsInterface-context.xml"); JobSynchronizationManager.register(jobExecution); @SuppressWarnings("unchecked") Callable<String> value = context.getBean(Callable.class); assertEquals("JOB", value.call()); }
public void init(Class<?>... config) throws Exception { Class<?>[] configs = new Class<?>[config.length + 1]; System.arraycopy(config, 0, configs, 1, config.length); configs[0] = DataSourceConfiguration.class; AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(configs); context.refresh(); this.context = context; JobSynchronizationManager.register(jobExecution); }
@Test public void testXmlJobScopeWithProxyTargetClass() throws Exception { context = new ClassPathXmlApplicationContext( "org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTestsProxyTargetClass-context.xml"); JobSynchronizationManager.register(jobExecution); SimpleHolder value = context.getBean(SimpleHolder.class); assertEquals("JOB", value.call()); }
@Test public void testXmlJobScopeWithInheritance() throws Exception { context = new ClassPathXmlApplicationContext( "org/springframework/batch/core/configuration/annotation/JobScopeConfigurationTestsInheritance-context.xml"); JobSynchronizationManager.register(jobExecution); SimpleHolder value = (SimpleHolder) context.getBean("child"); assertEquals("JOB", value.call()); }
@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 testRelease() { JobContext context = JobSynchronizationManager.register(jobExecution); final List<String> list = new ArrayList<>(); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); // On release we expect the destruction callbacks to be called JobSynchronizationManager.release(); assertNull(JobSynchronizationManager.getContext()); assertEquals(1, list.size()); }
@Test public void testRegisterNull() { assertNull(JobSynchronizationManager.getContext()); JobSynchronizationManager.register(null); 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(); } } });
@Test public void testGetContext() { assertNull(JobSynchronizationManager.getContext()); JobSynchronizationManager.register(jobExecution); assertNotNull(JobSynchronizationManager.getContext()); }
@Test public void testSimpleProperty() throws Exception { JobExecution jobExecution = new JobExecution(11L); ExecutionContext executionContext = jobExecution.getExecutionContext(); executionContext.put("foo", "bar"); JobSynchronizationManager.register(jobExecution); assertEquals("bar", simple.getName()); }
@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(); }
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(); }
@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()); }