BATCH-220: Half way to rename StepHandler -> Tasklet
This commit is contained in:
@@ -12,16 +12,22 @@ import org.springframework.batch.item.validator.ValidationException;
|
||||
public class ItemTrackingItemWriter<T> implements ItemWriter<T> {
|
||||
|
||||
private List<T> items = new ArrayList<T>();
|
||||
|
||||
private T failed = null;
|
||||
|
||||
private int failure = -1;
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
public void write(List<? extends T> item) throws Exception {
|
||||
items.addAll(item);
|
||||
public void write(List<? extends T> items) throws Exception {
|
||||
if (failed!=null && items.contains(failed)) {
|
||||
throw new ValidationException("validation failed");
|
||||
}
|
||||
this.items.addAll(items);
|
||||
int current = counter;
|
||||
counter += item.size();
|
||||
counter += items.size();
|
||||
if (current < failure && counter >= failure) {
|
||||
failed = items.get(failure-current-1);
|
||||
throw new ValidationException("validation failed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.batch.core.step.handler.StepHandler;
|
||||
import org.springframework.batch.core.step.tasklet.StepHandler;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
|
||||
import org.springframework.batch.core.step.handler.StepHandler;
|
||||
import org.springframework.batch.core.step.tasklet.StepHandler;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.springframework.batch.sample.tasklet;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.UnexpectedJobExecutionException;
|
||||
import org.springframework.batch.core.step.handler.StepHandler;
|
||||
import org.springframework.batch.core.step.tasklet.StepHandler;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
<bean id="jobExecutionContextSample" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<list>
|
||||
<bean id="step1" parent="handlerStep">
|
||||
<property name="tasklet" ref="sender" />
|
||||
<bean id="step1" parent="taskletStep">
|
||||
<property name="stepHandler" ref="sender" />
|
||||
</bean>
|
||||
<bean id="step2" parent="handlerStep">
|
||||
<property name="tasklet" ref="receiver" />
|
||||
<bean id="step2" parent="taskletStep">
|
||||
<property name="stepHandler" ref="receiver" />
|
||||
</bean>
|
||||
</list>
|
||||
</property>
|
||||
|
||||
@@ -13,19 +13,19 @@
|
||||
|
||||
The second step illustrates executing a system command.
|
||||
</description>
|
||||
<bean id="handlerJob" parent="simpleJob">
|
||||
<bean id="taskletJob" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<list>
|
||||
<bean id="deleteFilesInDir" parent="handlerStep">
|
||||
<property name="tasklet">
|
||||
<bean id="deleteFilesInDir" parent="taskletStep">
|
||||
<property name="stepHandler">
|
||||
<bean class="org.springframework.batch.sample.tasklet.FileDeletingStepHandler">
|
||||
<property name="resources" value="target/test-outputs/test-dir/*" />
|
||||
<property name="resources" value="file:target/test-outputs/test-dir/*" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
<bean id="executeSystemCommand" parent="handlerStep">
|
||||
<property name="tasklet">
|
||||
<bean class="org.springframework.batch.core.step.handler.SystemCommandStepHandler">
|
||||
<bean id="executeSystemCommand" parent="taskletStep">
|
||||
<property name="stepHandler">
|
||||
<bean class="org.springframework.batch.core.step.tasklet.SystemCommandStepHandler">
|
||||
<property name="command" value="java -version" />
|
||||
<!-- 5 second timeout for the command to complete -->
|
||||
<property name="timeout" value="5000" />
|
||||
@@ -2,17 +2,17 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
|
||||
<bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob" abstract="true">
|
||||
<property name="jobRepository" ref="jobRepository" />
|
||||
<property name="restartable" value="true" />
|
||||
</bean>
|
||||
|
||||
<bean id="handlerStep" class="org.springframework.batch.core.step.handler.StepHandlerStep" abstract="true">
|
||||
<bean id="taskletStep" class="org.springframework.batch.core.step.tasklet.StepHandlerStep" abstract="true">
|
||||
<property name="transactionManager" ref="transactionManager" />
|
||||
<property name="jobRepository" ref="jobRepository" />
|
||||
<property name="allowStartIfComplete" value="true" />
|
||||
|
||||
@@ -17,7 +17,7 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration()
|
||||
public class HandlerJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
public class TaskletJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private Resource directory = new FileSystemResource("target/test-outputs/test-dir");
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.sample.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.validator.ValidationException;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class ItemTrackingItemWriterTests {
|
||||
|
||||
private ItemTrackingItemWriter<String> writer = new ItemTrackingItemWriter<String>();
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.sample.support.ItemTrackingItemWriter#write(java.util.List)}.
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testWrite() throws Exception {
|
||||
assertEquals(0, writer.getItems().size());
|
||||
writer.write(Arrays.asList("a", "b", "c"));
|
||||
assertEquals(3, writer.getItems().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidationFailure() throws Exception {
|
||||
writer.setValidationFailure(2);
|
||||
try {
|
||||
writer.write(Arrays.asList("a", "b", "c"));
|
||||
fail("Expected ValidationException");
|
||||
}
|
||||
catch (ValidationException e) {
|
||||
// expected
|
||||
}
|
||||
assertEquals(3, writer.getItems().size());
|
||||
writer.write(Arrays.asList("a", "e", "c"));
|
||||
assertEquals(6, writer.getItems().size());
|
||||
try {
|
||||
writer.write(Arrays.asList("f", "b", "g"));
|
||||
fail("Expected ValidationException");
|
||||
}
|
||||
catch (ValidationException e) {
|
||||
// expected
|
||||
}
|
||||
assertEquals(6, writer.getItems().size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,6 @@
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
|
||||
<import resource="classpath:/simple-job-launcher-context.xml" />
|
||||
<import resource="classpath:/jobs/handlerJob.xml" />
|
||||
<import resource="classpath:/jobs/taskletJob.xml" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user