diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBean.java index 90d489cef..83b15cd29 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBean.java @@ -284,10 +284,7 @@ class StepParserStepFactoryBean implements FactoryBean, BeanNameAware { Assert.state(partitioner != null, "A Partitioner must be provided for a partition step"); configureAbstractStep(ts); - PartitionHandler handler; - if (partitionHandler != null) { - handler = partitionHandler; ts.setPartitionHandler(partitionHandler); } else { @@ -299,29 +296,23 @@ class StepParserStepFactoryBean implements FactoryBean, BeanNameAware { partitionHandler.setGridSize(gridSize); partitionHandler.setTaskExecutor(taskExecutor); ts.setPartitionHandler(partitionHandler); - handler = partitionHandler; } - PartitionHandler targetHandler = (PartitionHandler) extractTarget(handler, PartitionHandler.class); // BATCH-1659 - if (targetHandler instanceof TaskExecutorPartitionHandler) { - // Only for a local partition handler is the step required - Assert.state(step != null, - "A Step must be provided for a partition step with a TaskExecutorPartitionHandler"); - try { - TaskExecutorPartitionHandler taskExecutorPartitionHandler = (TaskExecutorPartitionHandler) targetHandler; - taskExecutorPartitionHandler.setStep(step); - taskExecutorPartitionHandler.afterPropertiesSet(); + if (partitionHandler instanceof TaskExecutorPartitionHandler) { + TaskExecutorPartitionHandler taskExecutorPartitionHandler = (TaskExecutorPartitionHandler) partitionHandler; + if (taskExecutorPartitionHandler.getStep() == null) { + // Only for a local partition handler is the step required + Assert.state(step != null, + "A Step must be provided for a partition step with a TaskExecutorPartitionHandler"); + try { + taskExecutorPartitionHandler.setStep(step); + taskExecutorPartitionHandler.afterPropertiesSet(); + } + catch (Exception e) { + throw new BeanCreationException("Could not configure TaskExecutorPartitionHandler", e); + } } - catch (Exception e) { - throw new BeanCreationException("Could not configure TaskExecutorPartitionHandler", e); - } - } - else { - // Only for a local partition handler is the step required - Assert.state(step == null, - "A Step must not be provided for a custom partition handler (unless it extends TaskExecutorPartitionHandler). " - + "It should be configured with its own step reference."); } boolean allowStartIfComplete = this.allowStartIfComplete != null ? this.allowStartIfComplete : false; @@ -671,7 +662,7 @@ class StepParserStepFactoryBean implements FactoryBean, BeanNameAware { public void setPartitioner(Partitioner partitioner) { this.partitioner = partitioner; } - + /** * @param stepExecutionAggregator the stepExecutionAggregator to set */ diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/PartitionStepWithLateBindingParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/PartitionStepWithLateBindingParserTests.java new file mode 100644 index 000000000..88c1bd077 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/PartitionStepWithLateBindingParserTests.java @@ -0,0 +1,93 @@ +/* + * Copyright 2006-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.configuration.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Dave Syer + * @author Josh Long + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class PartitionStepWithLateBindingParserTests { + + @Autowired + @Qualifier("job1") + private Job job1; + + @Autowired + @Qualifier("nameStoringTasklet") + private NameStoringTasklet nameStoringTasklet; + + @Autowired + private JobRepository jobRepository; + + @Autowired + private MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean; + + private List savedStepNames = new ArrayList(); + + @Before + public void setUp() { + nameStoringTasklet.setStepNamesList(savedStepNames); + mapJobRepositoryFactoryBean.clear(); + } + + @Test + public void testExplicitHandlerStep() throws Exception { + assertNotNull(job1); + JobExecution jobExecution = jobRepository.createJobExecution(job1.getName(), new JobParametersBuilder() + .addLong("gridSize", 1L).toJobParameters()); + job1.execute(jobExecution); + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); + Collections.sort(savedStepNames); + assertEquals("[s1:partition0]", savedStepNames.toString()); + List stepNames = getStepNames(jobExecution); + assertEquals(2, stepNames.size()); + assertEquals("[s1, s1:partition0]", stepNames.toString()); + } + + private List getStepNames(JobExecution jobExecution) { + List list = new ArrayList(); + for (StepExecution stepExecution : jobExecution.getStepExecutions()) { + list.add(stepExecution.getStepName()); + } + Collections.sort(list); + return list; + } + +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBeanTests.java index e4a49608d..694d43ae7 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepParserStepFactoryBeanTests.java @@ -272,8 +272,9 @@ public class StepParserStepFactoryBeanTests { SimplePartitioner partitioner = new SimplePartitioner(); fb.setPartitioner(partitioner); - fb.setStep(new StepSupport("foo")); - ProxyFactory factory = new ProxyFactory(new TaskExecutorPartitionHandler()); + TaskExecutorPartitionHandler partitionHandler = new TaskExecutorPartitionHandler(); + partitionHandler.setStep(new StepSupport("foo")); + ProxyFactory factory = new ProxyFactory(partitionHandler); fb.setPartitionHandler((PartitionHandler) factory.getProxy()); Object step = fb.getObject(); diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/PartitionStepWithLateBindingParserTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/PartitionStepWithLateBindingParserTests-context.xml new file mode 100644 index 000000000..703c45683 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/PartitionStepWithLateBindingParserTests-context.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file