diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java index d23a0263b..5dd75f059 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java @@ -66,6 +66,7 @@ import org.springframework.batch.core.step.StepLocator; import org.springframework.batch.core.step.tasklet.StoppableTasklet; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.core.step.tasklet.TaskletStep; +import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.access.BeanFactoryLocator; @@ -75,6 +76,7 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.context.access.ContextSingletonBeanFactoryLocator; import org.springframework.core.convert.converter.Converter; import org.springframework.core.io.ClassPathResource; @@ -140,7 +142,7 @@ import org.springframework.util.Assert; * @author Chris Schaefer * @since 3.0 */ -public class JsrJobOperator implements JobOperator, InitializingBean { +public class JsrJobOperator implements JobOperator, ApplicationContextAware, InitializingBean { private static final String JSR_JOB_CONTEXT_BEAN_NAME = "jsr_jobContext"; private final Log logger = LogFactory.getLog(getClass()); @@ -765,6 +767,11 @@ public class JsrJobOperator implements JobOperator, InitializingBean { } } + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + baseContext = applicationContext; + } + private static class ExecutingJobRegistry { private Map registry = new ConcurrentHashMap(); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java index 47af692b9..c0ac82d24 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java @@ -44,6 +44,7 @@ import javax.batch.operations.NoSuchJobExecutionException; import javax.batch.operations.NoSuchJobInstanceException; import javax.batch.runtime.BatchRuntime; import javax.batch.runtime.BatchStatus; +import javax.sql.DataSource; import org.junit.Before; import org.junit.Test; @@ -55,6 +56,8 @@ import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobInstance; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.configuration.annotation.DataSourceConfiguration; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.converter.JobParametersConverter; import org.springframework.batch.core.converter.JobParametersConverterSupport; import org.springframework.batch.core.explore.JobExplorer; @@ -66,9 +69,15 @@ import org.springframework.batch.core.step.JobRepositorySupport; import org.springframework.batch.support.transaction.ResourcelessTransactionManager; import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.access.ContextSingletonBeanFactoryLocator; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.core.task.SyncTaskExecutor; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.transaction.PlatformTransactionManager; public class JsrJobOperatorTests extends AbstractJsrTestCase { @@ -125,7 +134,7 @@ public class JsrJobOperatorTests extends AbstractJsrTestCase { } @Test - public void testCustomBaseContext() throws Exception { + public void testCustomBaseContextJsrCompliant() throws Exception { System.setProperty("JSR-352-BASE-CONTEXT", "META-INF/alternativeJsrBaseContext.xml"); JobOperator jobOperator = BatchRuntime.getJobOperator(); @@ -145,6 +154,27 @@ public class JsrJobOperatorTests extends AbstractJsrTestCase { System.getProperties().remove("JSR-352-BASE-CONTEXT"); } + @Test + public void testCustomBaseContextCustomWired() throws Exception { + + GenericApplicationContext context = new AnnotationConfigApplicationContext(BatchConfgiuration.class); + + JobOperator jobOperator = (JobOperator) context.getBean("jobOperator"); + + assertEquals(context, ReflectionTestUtils.getField(jobOperator, "baseContext")); + + long executionId = jobOperator.start("longRunningJob", null); + // Give the job a chance to get started + Thread.sleep(1000L); + jobOperator.stop(executionId); + // Give the job the chance to finish stopping + Thread.sleep(1000L); + + assertEquals(BatchStatus.STOPPED, jobOperator.getJobExecution(executionId).getBatchStatus()); + + System.getProperties().remove("JSR-352-BASE-CONTEXT"); + } + private void resetBaseContext() throws NoSuchFieldException, IllegalAccessException { Field instancesField = ContextSingletonBeanFactoryLocator.class.getDeclaredField("instances"); instancesField.setAccessible(true); @@ -647,4 +677,19 @@ public class JsrJobOperatorTests extends AbstractJsrTestCase { return null; } } + + @Configuration + @Import(DataSourceConfiguration.class) + @EnableBatchProcessing + public static class BatchConfgiuration { + + @Bean + public JsrJobOperator jobOperator(JobExplorer jobExplorer, JobRepository jobrepository, DataSource dataSource, + PlatformTransactionManager transactionManager) throws Exception{ + + JsrJobParametersConverter jobParametersConverter = new JsrJobParametersConverter(dataSource); + jobParametersConverter.afterPropertiesSet(); + return new JsrJobOperator(jobExplorer, jobrepository, jobParametersConverter, transactionManager); + } + } }