diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/SplitParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/SplitParser.java index b8f1e0d35..a84d6189d 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/SplitParser.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/SplitParser.java @@ -18,10 +18,14 @@ package org.springframework.batch.core.jsr.configuration.xml; import java.util.Collection; import java.util.List; +import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.ManagedList; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; @@ -29,9 +33,12 @@ import org.w3c.dom.Element; * Parses a <split /> element as defined in JSR-352. * * @author Michael Minella + * @author Chris Schaefer * @since 3.0 */ public class SplitParser { + private static final String TASK_EXECUTOR_PROPERTY_NAME = "taskExecutor"; + private static final String JSR_352_SPLIT_TASK_EXECUTOR_BEAN_NAME = "jsr352splitTaskExecutor"; private String jobFactoryRef; @@ -63,6 +70,25 @@ public class SplitParser { stateBuilder.addConstructorArgValue(flows); stateBuilder.addConstructorArgValue(idAttribute); + PropertyValue propertyValue = getSplitTaskExecutorPropertyValue(parserContext.getRegistry()); + stateBuilder.addPropertyValue(propertyValue.getName(), propertyValue.getValue()); + return FlowParser.getNextElements(parserContext, null, stateBuilder.getBeanDefinition(), element); } + + protected PropertyValue getSplitTaskExecutorPropertyValue(BeanDefinitionRegistry beanDefinitionRegistry) { + PropertyValue propertyValue; + + if (hasBeanDefinition(beanDefinitionRegistry, JSR_352_SPLIT_TASK_EXECUTOR_BEAN_NAME)) { + propertyValue = new PropertyValue(TASK_EXECUTOR_PROPERTY_NAME, new RuntimeBeanReference(JSR_352_SPLIT_TASK_EXECUTOR_BEAN_NAME)); + } else { + propertyValue = new PropertyValue(TASK_EXECUTOR_PROPERTY_NAME, new SimpleAsyncTaskExecutor()); + } + + return propertyValue; + } + + private boolean hasBeanDefinition(BeanDefinitionRegistry beanDefinitionRegistry, String beanName) { + return beanDefinitionRegistry.containsBeanDefinition(beanName); + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests.java index 7116ef36c..f57bc05ae 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import org.junit.Ignore; +import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -29,16 +29,19 @@ import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -@ContextConfiguration({"SplitParsingTests-context.xml", "jsr-base-context.xml"}) +@ContextConfiguration() @RunWith(SpringJUnit4ClassRunner.class) public class SplitParsingTests { - @Autowired public Job job; @@ -49,7 +52,6 @@ public class SplitParsingTests { public ExpectedException expectedException = ExpectedException.none(); @Test - @Ignore public void test() throws Exception { JobExecution execution = jobLauncher.run(job, new JobParameters()); assertEquals(BatchStatus.COMPLETED, execution.getStatus()); @@ -57,15 +59,33 @@ public class SplitParsingTests { } @Test - @Ignore public void testOneFlowInSplit() { try { new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/invalid-split-context.xml"); } catch (BeanDefinitionParsingException bdpe) { - assertTrue(bdpe.getMessage().indexOf("A must contain at least two 'flow' elements.") >= 0); + assertTrue(bdpe.getMessage().contains("A must contain at least two 'flow' elements.")); return; } fail("Expected exception was not thrown"); } + + @Test + public void testUserSpecifiedTaskExecutor() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/user-specified-split-task-executor-context.xml"); + BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory(); + PropertyValue propertyValue = new SplitParser(null).getSplitTaskExecutorPropertyValue(registry); + + RuntimeBeanReference runtimeBeanReferenceValue = (RuntimeBeanReference) propertyValue.getValue(); + + Assert.assertTrue("RuntimeBeanReference should have a name of jsr352splitTaskExecutor" , "jsr352splitTaskExecutor".equals(runtimeBeanReferenceValue.getBeanName())); + } + + @Test + public void testDefaultTaskExecutor() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/default-split-task-executor-context.xml"); + BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory(); + PropertyValue propertyValue = new SplitParser(null).getSplitTaskExecutorPropertyValue(registry); + Assert.assertTrue("Task executor not an instance of SimpleAsyncTaskExecutor" , (propertyValue.getValue() instanceof SimpleAsyncTaskExecutor)); + } } diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests-context.xml index 308228a2f..9f297d0a3 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests-context.xml @@ -1,31 +1,68 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + One + Two + Three + Four + Five + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/default-split-task-executor-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/default-split-task-executor-context.xml new file mode 100644 index 000000000..5f565db86 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/default-split-task-executor-context.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + One + Two + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/invalid-split-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/invalid-split-context.xml index 17aba5786..2b160901a 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/invalid-split-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/invalid-split-context.xml @@ -1,19 +1,25 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/user-specified-split-task-executor-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/user-specified-split-task-executor-context.xml new file mode 100644 index 000000000..4d8f9a4e3 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/user-specified-split-task-executor-context.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + One + Two + + + + + + + + + + + + + + + + + + + + + + +