RESOLVED - BATCH-1016: Create listener to promote items from Step ExecutionContext to Job ExecutionContext
applied patch and added unit test in core
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.listener;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.sun.org.apache.xerces.internal.impl.xpath.XPath.Step;
|
||||
|
||||
/**
|
||||
* This class can be used to automatically promote items from the {@link Step}
|
||||
* {@link ExecutionContext} to the {@link Job} {@link ExecutionContext} at the
|
||||
* end of a step. A list of keys should be provided that correspond to the items
|
||||
* in the {@link Step} {@link ExecutionContext} that should be promoted.
|
||||
*
|
||||
* @author Dan Garrette
|
||||
* @since 2.0
|
||||
*/
|
||||
public class ExecutionContextPromotionListener extends StepExecutionListenerSupport implements InitializingBean {
|
||||
|
||||
private List<String> keys = null;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.core.domain.StepListener#afterStep(StepExecution
|
||||
* stepExecution)
|
||||
*/
|
||||
public ExitStatus afterStep(StepExecution stepExecution) {
|
||||
ExecutionContext stepContext = stepExecution.getExecutionContext();
|
||||
ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext();
|
||||
for (String key : keys) {
|
||||
jobContext.put(key, stepContext.get(key));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(this.keys, "The 'keys' property must be provided");
|
||||
}
|
||||
|
||||
public void setKeys(List<String> keys) {
|
||||
this.keys = keys;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.springframework.batch.core.listener;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Tests for {@link ExecutionContextPromotionListener}.
|
||||
*/
|
||||
public class ExecutionContextPromotionListenerTests {
|
||||
|
||||
private ExecutionContextPromotionListener tested = new ExecutionContextPromotionListener();
|
||||
|
||||
private static final String key = "testKey";
|
||||
|
||||
private static final String value = "testValue";
|
||||
|
||||
@Test
|
||||
public void promoteEntryFromStepToJobExectutionContext() throws Exception {
|
||||
|
||||
JobExecution jobExecution = new JobExecution(1L);
|
||||
StepExecution stepExecution = jobExecution.createStepExecution("step1");
|
||||
|
||||
Assert.state(jobExecution.getExecutionContext().isEmpty());
|
||||
Assert.state(stepExecution.getExecutionContext().isEmpty());
|
||||
|
||||
stepExecution.getExecutionContext().putString(key, value);
|
||||
|
||||
tested.setKeys(Collections.singletonList(key));
|
||||
tested.afterPropertiesSet();
|
||||
|
||||
tested.afterStep(stepExecution);
|
||||
|
||||
assertEquals(value, jobExecution.getExecutionContext().getString(key));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void keysMustBeSet() throws Exception {
|
||||
// didn't set the keys, same as tested.setKeys(null);
|
||||
tested.afterPropertiesSet();
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
<listeners>
|
||||
<listener ref="skipCheckingListener"/>
|
||||
<listener ref="promotionListener"/>
|
||||
</listeners>
|
||||
</step>
|
||||
|
||||
@@ -33,12 +34,13 @@
|
||||
<tasklet reader="tradeSqlItemReader" processor="tradeProcessor" writer="itemTrackingWriter"
|
||||
commit-interval="2" skip-limit="1">
|
||||
<skippable-exception-classes>
|
||||
org.springframework.batch.item.validator.ValidationException,
|
||||
org.springframework.batch.item.validator.ValidationException
|
||||
java.lang.RuntimeException
|
||||
</skippable-exception-classes>
|
||||
</tasklet>
|
||||
<listeners>
|
||||
<listener ref="skipCheckingListener"/>
|
||||
<listener ref="promotionListener"/>
|
||||
</listeners>
|
||||
</step>
|
||||
|
||||
@@ -58,6 +60,14 @@
|
||||
|
||||
<beans:bean id="skipCheckingListener" class="org.springframework.batch.sample.common.SkipCheckingListener"/>
|
||||
|
||||
<beans:bean id="promotionListener" class="org.springframework.batch.core.listener.ExecutionContextPromotionListener">
|
||||
<beans:property name="keys">
|
||||
<beans:list>
|
||||
<beans:value>stepName</beans:value>
|
||||
</beans:list>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
<beans:bean id="fileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
|
||||
<beans:property name="resource" value="classpath:/data/skipJob/input/input#{jobParameters[run.id]}.txt" />
|
||||
<beans:property name="lineMapper">
|
||||
|
||||
@@ -16,6 +16,6 @@ public class SkipCheckingListener implements StepExecutionListener {
|
||||
}
|
||||
|
||||
public void beforeStep(StepExecution stepExecution) {
|
||||
stepExecution.getJobExecution().getExecutionContext().put("stepName", stepExecution.getStepName());
|
||||
stepExecution.getExecutionContext().put("stepName", stepExecution.getStepName());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user