From 8f9783f0d46adf763e484924ebcd958056da0687 Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Thu, 16 Jan 2014 16:24:19 -0600 Subject: [PATCH] Created JSR-352 specific SplitState to address resetting the JobExecution's ExitStatus after a split executes --- .../job/flow/support/state/SplitState.java | 5 +- .../jsr/configuration/xml/SplitParser.java | 2 +- .../job/flow/support/state/SplitState.java | 57 +++++++++++++++++++ .../configuration/xml/SplitParsingTests.java | 46 ++++++++------- .../batch-jobs}/SplitParsingTests-context.xml | 18 ++---- 5 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/support/state/SplitState.java rename spring-batch-core/src/test/resources/{org/springframework/batch/core/jsr/configuration/xml => META-INF/batch-jobs}/SplitParsingTests-context.xml (76%) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/SplitState.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/SplitState.java index a5e7ed3ae..cec44e9c3 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/SplitState.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/SplitState.java @@ -123,8 +123,11 @@ public class SplitState extends AbstractState implements FlowHolder { } } - return aggregator.aggregate(results); + return doAggregation(results, executor); + } + protected FlowExecutionStatus doAggregation(Collection results, FlowExecutor executor) { + return aggregator.aggregate(results); } /* 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 a84d6189d..4f06310c9 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 @@ -51,7 +51,7 @@ public class SplitParser { String idAttribute = element.getAttribute("id"); BeanDefinitionBuilder stateBuilder = BeanDefinitionBuilder - .genericBeanDefinition("org.springframework.batch.core.job.flow.support.state.SplitState"); + .genericBeanDefinition("org.springframework.batch.core.jsr.job.flow.support.state.SplitState"); List flowElements = DomUtils.getChildElementsByTagName(element, "flow"); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/support/state/SplitState.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/support/state/SplitState.java new file mode 100644 index 000000000..94d6aafa1 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/flow/support/state/SplitState.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 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.jsr.job.flow.support.state; + +import java.util.Collection; + +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.job.flow.Flow; +import org.springframework.batch.core.job.flow.FlowExecution; +import org.springframework.batch.core.job.flow.FlowExecutionStatus; +import org.springframework.batch.core.job.flow.FlowExecutor; + +/** + * JSR-352 states that artifacts cannot set the ExitStatus from within a split for a job. Because + * of this, this state will reset the exit status once the flows have completed (prior to aggregation + * of the results). + * + * @author Michael Minella + * @since 3.0 + */ +public class SplitState extends org.springframework.batch.core.job.flow.support.state.SplitState { + + /** + * @param flows {@link Flow}s to be executed in parallel + * @param name + */ + public SplitState(Collection flows, String name) { + super(flows, name); + } + + /** + * Resets the {@link JobExecution}'s exit status before aggregating the results of the flows within + * the split. + * + * @param results the {@link FlowExecution}s from each of the flows executed within this split + * @param executor the {@link FlowExecutor} used to execute the flows + */ + @Override + protected FlowExecutionStatus doAggregation(Collection results, FlowExecutor executor) { + executor.getJobExecution().setExitStatus(null); + + return super.doAggregation(results, executor); + } +} 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 f57bc05ae..b28854735 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 @@ -18,44 +18,40 @@ package org.springframework.batch.core.jsr.configuration.xml; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.springframework.batch.core.jsr.JsrTestUtils.runJob; + +import java.util.List; + +import javax.batch.api.AbstractBatchlet; +import javax.batch.runtime.BatchRuntime; +import javax.batch.runtime.StepExecution; +import javax.batch.runtime.context.JobContext; +import javax.inject.Inject; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -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.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() -@RunWith(SpringJUnit4ClassRunner.class) public class SplitParsingTests { - @Autowired - public Job job; - - @Autowired - public JobLauncher jobLauncher; @Rule public ExpectedException expectedException = ExpectedException.none(); @Test public void test() throws Exception { - JobExecution execution = jobLauncher.run(job, new JobParameters()); - assertEquals(BatchStatus.COMPLETED, execution.getStatus()); - assertEquals(5, execution.getStepExecutions().size()); + javax.batch.runtime.JobExecution execution = runJob("SplitParsingTests-context", null, 10000l); + assertEquals(javax.batch.runtime.BatchStatus.COMPLETED, execution.getBatchStatus()); + assertEquals("COMPLETED", execution.getExitStatus()); + + List stepExecutions = BatchRuntime.getJobOperator().getStepExecutions(execution.getExecutionId()); + assertEquals(5, stepExecutions.size()); } @Test @@ -88,4 +84,16 @@ public class SplitParsingTests { PropertyValue propertyValue = new SplitParser(null).getSplitTaskExecutorPropertyValue(registry); Assert.assertTrue("Task executor not an instance of SimpleAsyncTaskExecutor" , (propertyValue.getValue() instanceof SimpleAsyncTaskExecutor)); } + + public static class ExitStatusSettingBatchlet extends AbstractBatchlet { + + @Inject + JobContext jobContext; + + @Override + public String process() throws Exception { + jobContext.setExitStatus("Should be ignored"); + return null; + } + } } 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/META-INF/batch-jobs/SplitParsingTests-context.xml similarity index 76% rename from spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/SplitParsingTests-context.xml rename to spring-batch-core/src/test/resources/META-INF/batch-jobs/SplitParsingTests-context.xml index 9f297d0a3..48b43b1e6 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/META-INF/batch-jobs/SplitParsingTests-context.xml @@ -9,17 +9,17 @@ - + - + - + @@ -31,12 +31,10 @@ - + - - @@ -57,12 +55,4 @@ - - - - - - - -