Created JSR-352 specific SplitState to address resetting the JobExecution's ExitStatus after a split executes

This commit is contained in:
Michael Minella
2014-01-16 16:24:19 -06:00
parent ea7a704299
commit 8f9783f0d4
5 changed files with 93 additions and 35 deletions

View File

@@ -123,8 +123,11 @@ public class SplitState extends AbstractState implements FlowHolder {
}
}
return aggregator.aggregate(results);
return doAggregation(results, executor);
}
protected FlowExecutionStatus doAggregation(Collection<FlowExecution> results, FlowExecutor executor) {
return aggregator.aggregate(results);
}
/*

View File

@@ -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<Element> flowElements = DomUtils.getChildElementsByTagName(element, "flow");

View File

@@ -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<Flow> 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<FlowExecution> results, FlowExecutor executor) {
executor.getJobExecution().setExitStatus(null);
return super.doAggregation(results, executor);
}
}

View File

@@ -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<StepExecution> 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;
}
}
}

View File

@@ -9,17 +9,17 @@
<job id="job1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
<step id="step1" next="step2">
<batchlet ref="step1Ref"/>
<batchlet ref="org.springframework.batch.core.jsr.configuration.xml.SplitParsingTests$ExitStatusSettingBatchlet"/>
</step>
<split id="step2" next="step3">
<flow id="step2a">
<step id="step2aStep1">
<batchlet ref="step1Ref"/>
<batchlet ref="org.springframework.batch.core.jsr.configuration.xml.SplitParsingTests$ExitStatusSettingBatchlet"/>
</step>
</flow>
<flow id="step2b">
<step id="step2bStep1" next="step2bStep2">
<batchlet ref="step1Ref"/>
<batchlet ref="org.springframework.batch.core.jsr.configuration.xml.SplitParsingTests$ExitStatusSettingBatchlet"/>
</step>
<step id="step2bStep2">
<chunk checkpoint-policy="item" item-count="3">
@@ -31,12 +31,10 @@
</flow>
</split>
<step id="step3">
<batchlet ref="step1Ref"/>
<batchlet ref="org.springframework.batch.core.step.tasklet.TaskletSupport"/>
</step>
</job>
<bean id="step1Ref" class="org.springframework.batch.core.step.tasklet.TaskletSupport"/>
<bean id="generatingItemReader1" class="org.springframework.batch.item.support.ListItemReader">
<constructor-arg>
<list>
@@ -57,12 +55,4 @@
</property>
<property name="targetMethod" value="println"/>
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
</beans>